3

I'm using FirebaseMessagingService for my push notifications. It is working as intented in app. onMessageReceived is called, I can get notification title and body + data payload and send reaction depends on payload data to my Activity using LocalBroadcastManager.

But there is problem with background push notification. This will only show notification in background, but it is not created in onMessageReceived because this function cannot be called if app is in background.

According to documentation, data part of push notification is stored in extras and can be found when Activity is resumed. I have function for this already and its working. But problem is that I dont have title and message, because it was not send to onMessageReceived and I cannot catch this information.

Is there any way how to obtain it? Because I need to show it in my dialog window inside app. Push notification is not only text and title and it is not just information for user, but it will do some action.

Receiving notification title, body and payload in FirebaseMessagingService:

override fun onMessageReceived(msg: RemoteMessage?) {
        val pNotification = msg?.notification
        val payload = msg?.data
        if (pNotification != null){
            val ntitle = pNotification.title
            val nMsg = pNotification.body
        }
        //working with payload - creating notification and Intent for LocalBroadcastmanager
}

Catching payload from extras inside onResume()

private fun maybeFindBackgroundPayload(extras: Bundle?){
        if (extras != null){
            extras.keySet().forEach { key->
                val keyValue = extras[key]
                App.log("BackgroundKeyValues: $keyValue")
                if (key.equals("gcm.notification.body", ignoreCase = true) && keyValue != null){
                    ...
                    //Working with payload data key value - dont have title and body
                }
            }
        }
    }
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
martin1337
  • 2,384
  • 6
  • 38
  • 85
  • Not possible through firebase, as when app is in background, firebase handles the received notification & displays it, user is expected to click on it & then only data can be passed to your app. – Sudarshan Vidhate Aug 16 '19 at 18:51
  • I think you should pass the same info in data payload....Notification title and body is not stored in extras. – Naitik Soni Aug 19 '19 at 06:14
  • have you found any solution for this issue? Because I have also the same issue. and I have checked that some Device gets working on message received method but some are not allowed if the user kills our app manually by swiping or clearing from recent. – Gulab Sagevadiya Feb 16 '22 at 13:29

2 Answers2

6

Unfortunately, it is not possible the way you want it to work.

If the push notification has a notification part in the payload and your app is in the background, onMessageReceived will never be called. The notification is automatically created and displayed by firebase. There is no way to intercept this and modify the notification or do any other operation for that matter. Your app will only get control once the notification is clicked.

When the app is in foreground, as you mentioned, onMessageReceived will always be called.

The only way to ensure that onMessageReceived is called in all situations(foreground, background and app killed) is to send a data-only payload from firebase. That is, remove the notification part of the firebase payload body and put the content(title,body etc) in the data part of the payload. And in your onMessageReceived assign this title, body etc to the notification you are creating.

Here is how your payload from firebase will need to be:

{
"registration_ids":[
  "firebase_token"
],
"data":{
    "body":"body of notification",
    "title":"title of notification",
    "otherdatakey":"otherdatavalue"
}
}

Then in your onMessageReceived function, you need to explicitly extract the title and body and assign it to the notification(along with any other operations you want to do of course):

@Override
public void onMessageReceived(RemoteMessage message) {
Map<String, String> dataMap = message.getData();
String title = dataMap.get("title");
String body = dataMap.get("body");
String otherdatavalue = dataMap.get("otherdatakey");
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setContentTitle(title)
            .setContentText(body)
            .setContentIntent(pendingIntent);//To handle click of notification

}
Derryl Thomas
  • 1,324
  • 12
  • 22
  • Yes it works but not in every OS because i have 2 device one is honor7x which allows us to receive data in killed state and second is oppoF5 which don't allow in killed state. is there any solution for this issue? – Gulab Sagevadiya Feb 16 '22 at 13:31
0

Have you tried grabbing you notification and data from the public void handleIntent(Intent intent) method, it works perfect for me. I struggled to consistently get notifications and/or data from notifications using just onMessageRecieved

Robby Lebotha
  • 1,211
  • 11
  • 9