I'm trying to send a notification with Firebase. However, in some cases, the notification does not called the onReceive method. Foregound = Call and navigate. Background and Kill App = onReceive not called. I can not get the data and not redirect.
https://gist.github.com/Omurtekinn/fcbd3ba2e83925c459b22d5614c3ea00 MyFirebaseMessaging class.
If you check the documentation you'll see that Android handle notification messages differently depending on the payload and if your app is on the foreground or background:
If your app is in the foreground you'll always receive the notification in the onMessageReceived
method. Not matter the payload of the notification.
If it is on the background you have two scenarios:
Notification payload: Message is delivered to system tray and when you click it it'll be delivered to the Launch Activity through intent extras.
Data payload:
Message will be delivered to onMessageReceived
.
Looking into your scenario it seems you have a notification payload. You have two options:
1) Change the payload type to data. If you're using the FCM console you can only do option 2 because it will only send Notification messages.
2) Implement the receiving of the message in the launcher activity onCreate method.
The notification basically looks like this:
{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
}
}
If you're adding key/value pairs it will look like this:
{
"to" : "APA91bHun4MxP5egoKMwt2KZFBaFUH-1RYqx...",
"notification" : {
"body" : "great match!",
"title" : "Portugal vs. Denmark",
"icon" : "myicon"
},
"data" : {
"Nick" : "Mario",
"Room" : "PortugalVSDenmark"
}
}
In your launcher activity you get the notification in the intent extras like any other activity, in onCreate():
Bundle extras = getIntent().getExtras();
if(extras!=null){
String notification = extras.getString("notification");
String dats = extras.getString("data");
}
Note that if you're using android O you need to configure the channels as well.