Extract notification values ​​from notifications in the android firebase FCM system tray

I use firebase to send notifications to my application. I want to save notifications received on each device in a local database. The problem is that when the application is in the background and a notification is received, the onMessageReceived() method will not be called, but instead the notification will be added to the system tray according to the documentation. The documentation also mentions the following to be notified when the application is in the background:

In these cases, a notification is delivered to the device’s system tray, and a data payload is delivered in addition to assigning your launch activity.

So, when I try to access additional features in intent, notification keys were 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 send from the server:

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

Does anyone have an idea on how I can get the values ​​inside the notification?

+5
source share
4 answers

This is the intended behavior (as you saw in the documentation ). If you are using the notification -only message payload that you are, onMessageReceived() is only called if your application is in the foreground, if not, the system tray will receive it.

referenced data (underscore):

In these cases, a notification is delivered to the device’s system tray, and a data payload is supplied for the additional purposes of your launch activity.

- payload of the data message (see Message Types ).

You need to send the payload using notification and data . For instance:

 { "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 .

0
source

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

From:

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

For

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

Explanation:

There is a firebase service launched by your system that works for the entire application and processes all push notifications if it contains a "notification" tag. but if it contains the "data" tag, it will simply pass the task to the service created by the application, where you can do something. In your case, show the user notification and save it on a local db. Read more here Documentation: process messages .

+1
source

This is not a simple or clean solution, but you can use the NotificationListenerService to get a callback for each notification. Another limitation is that it only supports API 18 and above.

0
source

I had the same problem when the backend team introduced a notification tag in the fcm payload.

I found out that the handleIntent FirebaseMessagingService method is called every time a push is sent, but in the background . And its Intent parameter had data in it, from where the programmer can perform the intended action.

Hope this helps.

0
source

Source: https://habr.com/ru/post/1266275/


All Articles