Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve notification values from a notification in system tray android firebase FCM

I am using firebase to send notifications to my app. I want to save the notifications received on each device in a local database. The problem is, when the app is in the background and a notification is received, the method onMessageReceived() won't be called, but instead the notification would be added to the system tray as per documentation. The documentation also mentions the following for receiving a notification when the app is in background:

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.

So, when I'm trying to access the extras in the intent, the keys of the notification are not found. My code in MainActivity.java:

if (getIntent().getExtras() != null) {
        for (String key : getIntent().getExtras().keySet()) {
            Object value = getIntent().getExtras().get(key);
            Toast.makeText(this, "Key: " + key + " Value: " + value, Toast.LENGTH_LONG).show();
        }
    }

The notification we are sending from the server:

{
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
"notification" : {
  "body" : "great match!",
  "title" : "Portugal vs. Denmark",
  "icon" : "myicon"
  }
}

Anyone has an idea of how I can retrieve the values inside the notification?

like image 810
riadrifai Avatar asked Apr 03 '17 20:04

riadrifai


People also ask

How do I get data from Firebase notification?

A user tap on a notification opens the app launcher by default. Messages with both notification and data payload, when received in the background. In this case, 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.

How do I test FCM push notifications in Firebase?

Send a test notification messageOpen the Notifications composer and select New notification. Enter the message text. Select Send test message. In the field labeled Add an FCM registration token, enter the registration token you obtained in a previous section of this guide.

What displays notification messages to the user from within the Android enabled device?

Status bar and notification drawer When you issue a notification, it first appears as an icon in the status bar. Users can swipe down on the status bar to open the notification drawer, where they can view more details and take actions with the notification.


1 Answers

This is the intended behavior (as you've seen in the documentation). If you're using a notification-only message payload (which you are), onMessageReceived() will only be called if your app is in foreground, if not, the System Tray will be receiving it.

The data being referred to in (emphasis mine):

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.

is the data message payload (See Message Types).

You'll have to send a payload with both notification and data. Like so:

{
  "to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1...",
  "notification" : {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
    },
  "data": {
    "body" : "great match!",
    "title" : "Portugal vs. Denmark",
    "icon" : "myicon"
  }
}

Also see my answer here.

like image 130
AL. Avatar answered Oct 01 '22 20:10

AL.