3

When user clicks a Firebase Notification in Android, you can get the following keys

  • google.sent_time (long)
  • from (string)
  • google.message_id (String)
  • collapse_key (String)

with the following code:

if (getIntent().getExtras() != null) {
            for (String key : getIntent().getExtras().keySet()) {
                String value = getIntent().getExtras().getString(key);
                Log.d(TAG, "Key: " + key + " Value: " + value);
            }
        }

However, I need to get the Body from the message. Is it possible?

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Machado
  • 14,105
  • 13
  • 56
  • 97

2 Answers2

2

Not if the app is in the background, the values defined in the notification payload are really for the purposes of generating a notification. If you want access to the values that are defined in the notification payload of the message then you should repeat them in the data payload so when the user taps on the notification you will be able to retrieve them from the extras of the intent. Something like:

{
  "to": "/topics/sometopic",
  "notification": {
    "title": "some title",
    "body": "some body"
  },
  "data": {
    "noti_title": "some title",
    "noti_body": "some body",
    "other_key": "other value"
  }
}
Arthur Thompson
  • 9,087
  • 4
  • 29
  • 33
  • Actually, switching the whole application into a `data` based message solved the issue. The only workaround I had to do was to handle the application state when receiving the notification. – Machado Aug 10 '16 at 18:03
  • 1
    That is a fair choice, for most control data messages are the way to go – Arthur Thompson Aug 10 '16 at 18:04
-1

You have to handle the notification manually, just follow this guide.

To get the notification body, call remoteMessage.getNotification().getBody() in the onMessageReceived method.

And then when showing the notification (look at this file), add the body as extra to the intent.

intent.putExtra("body", notificationBody);
intent.putExtra("title", notificationTitle);

Hope this helps :)

Wilik
  • 7,630
  • 3
  • 29
  • 35
  • It only works if the user has the application opened by the time it receives the notification. If he receives the notification at the notification bar and clicks at it, it won't get the body. – Machado Aug 09 '16 at 10:48
  • 1
    looks like you're sending the notification via the console. [this answer](http://stackoverflow.com/a/37845174/5292961) might be the answer for your question. :) – Wilik Aug 09 '16 at 12:28