0

Hi custom FCM notifications have stopped showing up on Android. they used to work before but after I updated all the gradle libraries notifications have stopped showing up on the deivce.

The code is executed with breakpoints and no errors are shown but notifications are not shown.

I am clueless on how to investigate this.

any pointers would be very very useful.

This is the code that should show custom notifications with a two layouts for small and large notifications sizes.

    private fun showCustomPushNotification(message: RemoteMessage, chargerId: String) {
        val notificationLayout = RemoteViews(
            packageName,
            R.layout.pgin_requires_approval_notification_small
        )
        val notificationLayoutExpanded = RemoteViews(
            packageName,
            R.layout.pgin_requires_approval_notification_large
        )

        val title = message.data[MSG_TITLE]
        val subTitle = message.data[MSG_SUB_TITLE]

        notificationLayout.setTextViewText(R.id.tvTitle, title)
        notificationLayout.setTextViewText(R.id.tvSubTitle, subTitle)

        notificationLayoutExpanded.setTextViewText(R.id.tvTitle, title)
        notificationLayoutExpanded.setTextViewText(R.id.tvSubTitle, subTitle)

        val intent = Intent(this, MainActivity::class.java).apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        }

        val pendingIntent: PendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_IMMUTABLE)

        val customNotification = NotificationCompat.Builder(
            this,
            CarInfoProcessingService.APPROVE_EACH_PGIN_NOTIFICATION_CHANNEL_ID
        )
            .setSmallIcon(R.mipmap.ic_launcher)
            .setStyle(NotificationCompat.DecoratedCustomViewStyle())
            .setCustomContentView(notificationLayout)
            .setCustomBigContentView(notificationLayoutExpanded)
            .setContentIntent(pendingIntent)
            .setAutoCancel(true)
            .setPriority(NotificationCompat.PRIORITY_MAX)
            .build()

        val approveIntent = Intent(this, CustomNotificationListener::class.java)
        approveIntent.putExtra("onClickListener", "approve")
        approveIntent.putExtra("chargerId", chargerId)
        val pendingApproveIntent = PendingIntent.getBroadcast(
            this,
            0,
            approveIntent,
            PendingIntent.FLAG_IMMUTABLE
        )
        notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnApprove, pendingApproveIntent)

        val denyIntent = Intent(this, CustomNotificationListener::class.java)
        denyIntent.putExtra("onClickListener", "deny")
        denyIntent.putExtra("chargerId", chargerId)
        val pendingDenyIntent = PendingIntent.getBroadcast(
            this,
            1,
            denyIntent,
            PendingIntent.FLAG_IMMUTABLE
        )

        notificationLayoutExpanded.setOnClickPendingIntent(R.id.btnDeny, pendingDenyIntent)

        val notificationManager =
            getSystemService(NOTIFICATION_SERVICE) as NotificationManager
        notificationManager.notify(CustomNotificationListener.WHAT_NOTIFICATION_ID, customNotification)
}

Only change that I can think of is updating libraries in gradle but as it does not show any error I am clueless.

Thanks for your help in advance. R

BRDroid
  • 3,920
  • 8
  • 65
  • 143

1 Answers1

0

You say before update it was working. So after update i think you need to check versions for your code :
Define your pending intent like below :

val pendingIntent: PendingIntent? = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_IMMUTABLE)
        } else {
            PendingIntent.getActivity(applicationContext, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        }

You might wanna set priority for your notification, Build.VERSION_CODES.O and above will ignore that but older devices uses this:

customNotification.priority = NotificationCompat.PRIORITY_MAX

And for Build.VERSION_CODES.O and above notification channel is needed:

       if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            customNotification.setChannelId(Constants.NOTIFICATION_CHANNEL)     

            val channel =
                NotificationChannel(
                    Constants.NOTIFICATION_CHANNEL,
                    Constants.NOTIFICATION_NAME,
                    NotificationManager.IMPORTANCE_HIGH
                )

            notificationManager.createNotificationChannel(channel)
        }

For the constans i used they are just strings:

    const val NOTIFICATION_NAME = "YourApp"
    const val NOTIFICATION_CHANNEL = "YourApp_channel_01"

Just change your code regarding to this and it will work. If you can not manage to do that please let me know.

Mert
  • 904
  • 4
  • 9
  • 21