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