Retrieve notification values from a notification i

2019-02-17 03:59发布

问题:

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?

回答1:

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.



回答2:

All you have to do is update your notification push format.

From:

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

To

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

Explanation:

There is a firebase service running by your system which works for all app and handle all push notification if it contains the "notification" tag. but if contain the "data" tag only it just handover the task to the service created by the app where you can do anything.In your case show user notification and save it to the local db. For more you read here Documentation : Handle Messages.



回答3:

It's not a simple or clean solution, but you could use a NotificationListenerService to receive a callback for each notification. A further limitation is that it supports only API 18 and above.



回答4:

Add this to manifest file

        <intent-filter>
            <action android:name=".MainActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>