13

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); 
} 
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
mayur rahatekar
  • 4,410
  • 12
  • 37
  • 51
  • 1
    Do include the code you have so far. – AL. Jan 04 '17 at 07:28
  • post your onReceive() method's code! – android_griezmann Jan 04 '17 at 07:47
  • " final Bundle extras = intent.getExtras(); final Set keySet = extras.keySet(); final Iterator iterator = keySet.iterator(); while (iterator.hasNext()) { final String key = iterator.next(); final Object o = extras.get(key); System.out.println(key + ":" + o); } " – mayur rahatekar Jan 04 '17 at 07:54
  • First of all be sure that you are receiving notifiaction message or payload message or Combination of both. Google has provided sample application please follow that or else please paste your code here. – taman neupane Jan 04 '17 at 07:30
  • Dude thanks for your advice. I already mentioned in question that I am getting both notification and data message when app is in the foreground. And When is in background when notification came in system tray - when clicked on it. from intent.getExtras(); I am getting only mentioned keys. How to retrieve notification payload – mayur rahatekar Jan 04 '17 at 07:40
  • this solution worked for me https://stackoverflow.com/a/44150822/6632278 hope helps. Good luck – Tony Barajas Oct 18 '17 at 12:11

3 Answers3

16

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.

AL.
  • 36,815
  • 10
  • 142
  • 281
  • I am using firebase console for sending notification. When I click on notification. I am getting only google.sent_time, from, google.message_id, collapse_key. this keys. I want message and title key – mayur rahatekar Jan 04 '17 at 07:51
  • 1
    Sorry. What you're saying is a bit confusing. As I understand, you want to retrieve the `title` and `message` from the message you sent using the Firebase Console when your app is in background. Seeing as you sent the message from the console, it will be treated as a `notification` payload. What you have to do is add in a **custom** key-value pair (as I mentioned in my answer) in the ***Advanced Option*** section, then parse it in the Intent extras with the key you used. – AL. Jan 04 '17 at 07:56
  • Currently I am doing same. But is there any other way. Because I have to add title and message twice. In firebase console. – mayur rahatekar Jan 04 '17 at 07:59
  • 1
    I see. Unfortunately, I don't think there is any way to retrieve the notification payload values when the is in background. It will always be handled by the Notification Tray. – AL. Jan 04 '17 at 08:02
  • Thank you for your reply. So I have to set my own server and send only data payload so even if my app is in background onMessageReceived get called. In this case generation of notification we have to take care. Correct – mayur rahatekar Jan 04 '17 at 08:08
7

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

enter image description here

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);
                }
          }
   } 
Bibin Johny
  • 3,157
  • 1
  • 13
  • 16
Sheharyar Ejaz
  • 2,808
  • 1
  • 14
  • 15
1

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

pix
  • 1,264
  • 19
  • 32
  • 1
    FirebaseMessagingService does not have a method called handleIntent. See doc: https://firebase.google.com/docs/reference/android/com/google/firebase/messaging/FirebaseMessagingService. This comment needs to be reviewed – voghDev Jan 15 '18 at 10:53