Is there any way how to get data from push notification using FirebaseMessagingService in background?

十年热恋 提交于 2020-01-16 10:11:52

问题


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
                }
            }
        }
    }

回答1:


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

}


来源:https://stackoverflow.com/questions/57523895/is-there-any-way-how-to-get-data-from-push-notification-using-firebasemessagings

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!