5

I am not sure whether this is entirely possible but I'll state my requirement. If it is possible then kindly help me figure out how to do it.

Let's say I have a gallery kind of an android app. When the user likes or comments on a photo in the gallery, We'd trigger an fcm notification using the code given below

    value++;
    NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
            .setSmallIcon(R.drawable.notification)
            .setLargeIcon(rawBitmap)
            .setContentTitle("MyApp")
            .setContentText(ContentText)
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setDefaults(Notification.DEFAULT_SOUND)
            .setStyle(new NotificationCompat.BigTextStyle().bigText(ContentText))
            .setContentIntent(pendingIntent);

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

    notificationManager.notify(value, notificationBuilder.build());

By adding InboxStyle we can group notifcations into a single one and just increase the count.(For e.g. You have 5 notifications)

      NotificationCompat.InboxStyle inboxStyle =
                new NotificationCompat.InboxStyle();
            // Sets a title for the Inbox in expanded layout
            inboxStyle.setBigContentTitle("Title - Notification");
            inboxStyle.setSummaryText("You have "+value+" Notifications.");
            notificationBuilder.setStyle(inboxStyle);

But my requirement is like separate grouping for separate photos. If the user leaves 2 comments each for 3 photos. I need three groups of notifications to be listed. More like you have 2 comments on this photo,2 on this and so on.

I'll be receiving unique ids for the photos,if that helps.

How long will the id be retained?

Let's assume the user drops 2 comments on photo with id 001 and the partner receives the notification as a group .

What happens when the user drops another 2 comments on photo with id 002?
Will there be 2 groups?

Because a group of notification with id 001 remains untouched.

  • You could use the [`tag`](https://firebase.google.com/docs/cloud-messaging/http-server-ref#notification-payload-support) parameter and pass the unique id of the photo. – AL. Apr 26 '17 at 10:48
  • use the `photo unique id` as the notification id, and it should be fine. they'll be grouped by notification IDs. – Shark Apr 26 '17 at 10:48
  • How long would the id be retained? Let's assume the user drops 2 comments on photo with id 001 and the partner receives the notification as a group . What happens when the user drops another 2 comments on photo with id 002? Will there be 2 groups? Because a group of notification with id 001 remains untouched. – Shyamnath Mallinathan Apr 26 '17 at 10:52
  • 2
    I just checked,I'm not getting separate groups. The same group tends to get overwritten. – Shyamnath Mallinathan Apr 26 '17 at 11:29

1 Answers1

2

I would use the TAG parameter. For each group of messages you should use a different TAG.

For example:

Message 1)

{
    "notification": {
        "title": "PhotoApp: photo 123",
        "body": "You have 1 notification",
        "click_action" : "OPEN_MAINACTIVITY",
        "icon": "ic_launcher",
        "color": "#ffffff"
        "tag": "photo123"
    },
    "registration_ids":[
        "--your_id--"
    ]
}

Message 2)

{
    "notification": {
        "title": "PhotoApp: photo ABC",
        "body": "You have 1 notification",
        "click_action" : "OPEN_MAINACTIVITY",
        "icon": "ic_launcher",
        "color": "#ffffff"
        "tag": "photoABC"
    },
    "registration_ids":[
        "--your_id--"
    ]
}

Message 3)

{
    "notification": {
        "title": "PhotoApp: photo 123",
        "body": "You have 2 notifications",
        "click_action" : "OPEN_MAINACTIVITY",
        "icon": "ic_launcher",
        "color": "#ffffff"
        "tag": "photo123"
    },
    "registration_ids":[
        "--your_id--"
    ]
}

This will show only 2 notification alerts. One for Photo123, showing there are 2 notifications (last message), and the other for PhotoABC, showing there is just 1 notification.

The most important thing here is the TAG parameter. It will group notifications as you need,

Hope I made myself clear at it helps you out.

Some helpful links:

FCM Documentation

Similar SO question

Community
  • 1
  • 1
Federico Alvarez
  • 1,459
  • 1
  • 19
  • 30
  • 1
    Yes, We can use tag or use the unique id as parameter for- `notificationManager.notify(notifyid, notificationBuilder.build());` – Shyamnath Mallinathan Apr 27 '17 at 06:15
  • 1
    The problem is that **Message1-** _will have a body stating- User liked photo123._ **Message3**- _will have a body -User commented on this photo123_ **The resulting group should have the content you have "2" notifications on photo123** whereas the **Message2-** _will remain as notification with body -User liked photoABC_ – Shyamnath Mallinathan Apr 27 '17 at 06:27
  • I see. That logic belongs to your application. You have to be able to count 'likes', if the numbers of likes is grater than 1, then send a message "You have # notifications on photo123". If the number of likes is 1, just send "User commented on photo123". Just send the correct message, that's it. – Federico Alvarez Apr 27 '17 at 12:53
  • In FCM docs I can't find any documentation about "tag" field. Any suggestions ? I need to implement message grouping in the app. – NKM Jun 15 '20 at 06:57
  • NKM, did you follow the "FMC Documentation" link provided in the answer? It is described at the, more or less, middle of the page. Or just find the word TAG in that page. – Federico Alvarez Jun 15 '20 at 12:24