1

How to update a Firebase push notification containing data payload when the app is in background? Is there a way to specify the notification id in the notification to the Firebase API?

My request json to the firebase api.

{
"registration_ids": ["device id"], 
"collapse_key": "Updates Available"
"notification": {
                    "title": "title", 
                    "desc": "description",
                    "body": "Message received", 
                    "sound": "TYPE_NOTIFICATION",
                    "click_action": "sometargetAction"
                },
"data":         {
                    "user": 
                    {
                        "id": 2
                        "name":"leapingwolf", 
                        "occupation": "passionate coder"
                    }
                }
}

I use the id of the "user" to append to a delivered push notification when the app is in foreground in the onMessageReceived function like so

        User user = remoteMessage.getData().get("user");
        Gson gson = new GsonBuilder().create();
        User userModel =  gson.fromJson(user, User.class);
        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("FCM Message With Payload")
                .setContentText(messageBody)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setContentIntent(pendingIntent);

        NotificationManager notificationManager =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

        notificationManager.notify(userModel.getId(), notificationBuilder.build());

The full project is in github https://github.com/akshatashan/FirebaseCloudMessagingDemo

Akshata
  • 1,005
  • 2
  • 12
  • 22

2 Answers2

3

I figured out the solution based on the answers posted in the link How to handle notification when app in background in Firebase

To summarise, if the request json does not have the notification tag, then onMessageReceived is called irrespective of whether or not the app is in background. Send all relevant field in the data tag and parse it in the onMessageReceived.

Community
  • 1
  • 1
Akshata
  • 1,005
  • 2
  • 12
  • 22
1

I think what you are looking for is Collapsible Messages:

A collapsible message is a message that may be replaced by a new message containing the same collapse key if it has yet to be delivered to the device.

For both message types (notification and data), it seems they can be both set as collapsible, in your case, you were asking for the data payload:

Data message

  • Client app is responsible for processing data messages. Data messages have only custom key-value pairs.
  • Use your app server and FCM server API: Set the data key only. Can be either collapsible or non-collapsible.

So to put it simply, you just have to make use of the collapse_key accordingly:

This parameter identifies a group of messages (e.g., with collapse_key: "Updates Available") that can be collapsed, so that only the last message gets sent when delivery can be resumed. This is intended to avoid sending too many of the same messages when the device comes back online or becomes active.

Note that there is no guarantee of the order in which messages get sent.

Note: A maximum of 4 different collapse keys is allowed at any given time. This means a FCM connection server can simultaneously store 4 different send-to-sync messages per client app. If you exceed this number, there is no guarantee which 4 collapse keys the FCM connection server will keep.

Community
  • 1
  • 1
AL.
  • 36,815
  • 10
  • 142
  • 281
  • 1
    Collapse key is useful when the message is not yet delivered to the device. ex. if several messages are sent when the device is offline, only one will be delivered when the device is online and a collapse_key had been specified. My requirement is to update or replace an already delivered message when app is in background. When app is in foreground, I achieve it by specifying the same notifyId to the notificationManager in the onMessageReceived function. – Akshata Dec 13 '16 at 18:58
  • @Akshata Looks like you get it pretty well. With your use-case, I was also thinking of suggesting to use `delay_while_idle`, but that ones been deprecated. Anyways, when your app is in background, do you somehow *store* the temporarily until such time that the app goes to foreground? Can you show the code and expound more on your process flow? – AL. Dec 13 '16 at 23:12
  • 1
    I added more details to the question with some code snippets. is there some key that i can send with the request to specify updating a delivered message instead of creating a new one when app is in bg ? – Akshata Dec 14 '16 at 09:40