I am developing an app where I am getting FCM notification, in that when the app is open onMessageRecieved()
method triggers and I am notifying message based on tag and id and deleting notification based on id and tag. But when the app is in background onMesasageRecived()
is not calling. How to attach id and tag notification or how to delete single notification based on some id when I am getting notification from the background.
see onMessageReceived
@Override
public void onMessageReceived(RemoteMessage remoteMessage) {
............
notificationManager.notify("tag", notificationId, notification);
.......
}
for deleting the message
private void clearNotifications() {
NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancel("tag",notificationId);
}
this is working but when the app is closed code is not working. can we notify or attach tag and id from the serverside payload
If you check out the docs and here you'll see that if you have a notification
payload your notification will be delivered to the system tray directly when your app is in the background, there's no way to intercept that. The same will happen if you have a notification payload with an option data payload, the notification will go straight to the tray and the data payload will be delivered to the intent of the launcher activity.
{
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification":{
"title":"Portugal vs. Denmark",
"body":"great match!"
}
}
}
As you're checking the id/tag in the onMessageReceive
method, what you can do to guarantee that it will always call the onMessageReceived
method is to remove the notification payload from your notification and add just a data payload. All data
payloads are delivered to the onMessageReceived
method.
"message":{
"token":"bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"data":{
"Nick" : "Mario",
"body" : "great match!",
"Room" : "PortugalVSDenmark"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With