Machado
Machado

Reputation: 14499

How can I get Firebase Notification Body when user clicks?

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

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?

Upvotes: 3

Views: 2180

Answers (2)

Arthur Thompson
Arthur Thompson

Reputation: 9225

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"
  }
}

Upvotes: 3

Wilik
Wilik

Reputation: 7720

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 :)

Upvotes: -1

Related Questions