1

I am using the following code to receive and display push notifications:

override fun onMessageReceived(remoteMessage: RemoteMessage) {

    if (remoteMessage.getNotification() != null) {

        var title : String = remoteMessage.notification!!.title!!
        var message : String = remoteMessage.notification!!.body!!

        val intent = Intent(this, LoginCommonActivity::class.java)
        intent.addflags(intent.FLAG_ACTIVITY_CLEAR_TOP)

        val pendingIntent = PendingIntent.getActivity(this, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        var builder: NotificationCompat.Builder;

        val notificationManager =
        getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
        //val notificationManager = NotificationManagerCompat.from(applicationContext)
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            val notificationChannel =
            NotificationChannel(channelID, channelName, NotificationManager.IMPORTANCE_HIGH)
            notificationManager.createNotificationChannels(notificationChannel)
            builder = NotificationCompat.Builder(applicationContext, notificationChannel.id)
        } else {
            builder = NotificationCompat.Builder(applicationContext)
        }

        builder = builder.setSmallIcon(R.drawable.ic_app_logo_black)
            .setContentTitle(title)
            .setContentText(message)
            .setAutoCancel(true)
            .setVisibility(Notification.VISIBILITY_PUBLIC)
            .setDefaults(Notification.DEFAULT_ALL)
            .setVibrate(longArrayOf(1000, 1000, 1000, 1000))
            .setOnlyAlertOnce(true)
            .setContentIntent(pendingIntent)

        notificationManager.notify(System.currentTimeMillis().toInt(), builder.build())
    }
}

But PNs' are only displayed when app is in background or closed. Debugging the FCMMessagingService, I can see that PNs' are being sent by the server and received in onMessageReceive(). It appears that the notify() method is not working, or something else in the code is failing.

As per this article, FCM notifications are handled by the internal Firebase service when the app is in background, else they are received in onMessageReceived() of FirebaseMessagingService when the app is in the foreground, and we have to manually display them with notify(). But this isn't working.

We've already seen this problem before here, here, here and here. But that was maybe when the old GCM features were still present in FCM. By this time FCM must have been completely overhauled.

My questions are:

  • What is the default protocol for handling push notifications if they arrive when the app is in the foreground?
  • Looking at other popular apps - WhatsApp, Google Pay, Zomato - I do not ever recall seeing a push notification when the app is in the foreground. Does this mean that push notifications never appear in the tray when the app is in the foreground?
  • If so, then what is the reason for having the NotificationManager.notify() function in the first place? If push notifications only appear when the app is in the background, and they are auto-handled by the Firebase service, then why is this method there at all? Is this just a relic of the old GCM library?

Can someone please point out where the problem is?

Yash Sampat
  • 30,051
  • 12
  • 94
  • 120
  • Yes, push notifications can only display if the app is in background because that is the purpose of push notification. To get the attention of te user if they are not using the app. notification manager and notify are used for local push notifications that is why the method is there. – hfarhanahmed Aug 11 '22 at 05:49
  • There might be a workaround to get the notification while using the app – hfarhanahmed Aug 11 '22 at 05:50
  • It seems to be a problem with the notification channel. Calling notify() manually *should* display PNs' in any case. – Yash Sampat Aug 11 '22 at 05:59

2 Answers2

4

I will try to answer all your questions

First notification are of two types 1): Notification Payload ( Your's case) 2): Data Payload

1): Notification Payload

In notification payload -If App in Background ( FCM will handle push notifications it self ) -If App in Foreground ( payload will receive in onMessageReceived we have to write our own logic to weather show notification or not )

2): Data Payload In data payload -If App in Background ( payload will receive in onMessageReceived we have to write our own logic to weather show notification or not ) -If App in Foreground ( payload will receive in onMessageReceived we have to write our own logic to weather show notification or not )

For Notification Please try using updated code from android guide using Compat library

https://developer.android.com/training/notify-user/build-notification#groovy

Usman Nazir
  • 106
  • 1
  • 6
0

Just remove "notification" key from payload and provide only "data" key.

Application handles notification messages only if the app is in foreground but it handles data messages even if the application is in background or closed.

more info: https://stackoverflow.com/a/38850030/19892188