I am using FCM for Simple notification
When the app is in foreground, everything working properly. I am getting a notification plus the data message inside the onMessageReceived
method.
But when app is in background, I am getting notification in system tray. And when I click on the control, it goes to the main activity. And When I parse intent.getExtras();
, I am getting only this key data - google.sent_time
, from
, google.message_id
, collapse_key
.
How to get the notification message title and Message which is visible in system tray from intent.getExtras()
?
I am using FCM console for sending notification I don't have my dedicated server to do this.
Code for receiving the message:
final Bundle extras = intent.getExtras();
final Set<String> keySet = extras.keySet();
final Iterator<String> iterator = keySet.iterator();
while (iterator.hasNext()) {
final String key = iterator.next();
final Object o = extras.get(key);
System.out.println(key + ":" + o);
}
When your app is in the background, Android directs notification messages to the system tray. A user tap on the notification opens the app launcher by default. This includes messages that contain both notification and data payload (and all messages sent from the Notifications console).
Notification messages When your app is in the foreground, the FCM notification message is delivered to the onMessageReceived() handler and you can handle it by posting a notification if needed or update the app content with the FCM payload data (Max 4KB) or fetch content from app server.
As seen in the Handling Messages for Android FCM docs, if the payload you sent has both notification and data, it will be handled separately. The notification part will be handled by the Notification Tray, while the data part will be in the extras of the intent.
AFAIK, there is no way to get the notification payload when the app is in background (always handled by the Notification Tray). However, what you could do is add custom key-value pairs to your data payload instead, like so:
{
"data": {
"notification_title": "title here",
"notification_message": "message here"
}
}
Of course you'll have to make sure that the data value for notification_title
and notification_message
is the same as to what you set it in the notification payload. Then just retrieve it from the Intent extras like usual.
Firebase Notfication will behave as Data Message when your app is in background or is killed. In these Scenarios, if you want to retrieve your notification Message then you must define it in Key Value pair under
Advanced Option of FCM Console
and then retrieve this message by using this key in your activity which will be open by tabbing the Notification.
if (getIntent().getExtras() != null) {
Object value ;
for (String key : getIntent().getExtras().keySet()) {
if(key.equals("Message Key")) {
value = getIntent().getExtras().get(key); // value will represend your message body... Enjoy It
Log.d("NotificationTag" , key+"____" + value);
}
}
}
Just override the handleIntent() method of FirebaseMessagingService .class it will called in both foreground and background mode and here your can get and parse notification key and payload data
public void handleIntent(Intent intent)
{
String title = bundle.getString("gcm.notification.title");
String body = bundle.getString("gcm.notification.body");
}
Note: it will worked for play service 11
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