Varun Garg Varun Garg -4 years ago 103
Android Question

Firebase Get Notification data on tap

I was firebase console to send notifications. It sends notifications to system tray directly. I want to get payload from them when user taps on them.

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). In these
cases, the notification is delivered to the device's system tray, and
the data payload is delivered in the extras of the intent of your
launcher Activity.

I am able to get notifications that I sent from console
How do I get notification payload from extra of my intent?

Answer Source

I dumped extras bundle and found the answer.

These are they keys that I was looking for:

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

Apart from these you can directly use the keys that you used for custom data (while sending message).

when we fire messages from firebase console, I think only custom data that we sent along can be received from extra's intent. Title,message etc I cannot retrieved from here


As also observed by others, I don't think it is possible to extract title and message from extras intent. I found a workarond by passing them again with a separate key. Some code how to extract data from extras intent is

Bundle Extras = getIntent().getExtras(); 
public static String Content_KEY = "NotificationContent"; //alterative to message
String Content = Extras.getString(Content_KEY);

I don't remember exact names, but there are 2 modes to send a message.

  1. First mode is what firebase console uses. It sends messages directly

    1. To system tray if in background (as a notification)
    2. To onMessageReceived if in in foreground.

You will have to handle both, I ended up by sending notification from my app itself to system tray if I got it in onMessageReceived so that I could manage them from same code that handles extras intent in main activity.

You will also loose data if notification is lost.

  1. Send message directly from a server or app to firebase server. I think message will always be sent to onMessageReceived, saving all the pain of sending message again as separate key and problem of loosing data if notification is removed. But here you will have to setup your own notification app / server
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download