4

I've developed and Android app that receives Push Notifications using Firebase. My code is based on Firebase/Google official docs (https://firebase.google.com/docs/cloud-messaging/android/client and https://firebase.google.com/docs/cloud-messaging/android/receive) and has nothing special, it has the service that extends FirebaseMessagingService and the service that extends FirebaseInstanceIdService. Everything is working ok.

But this app is receiving more notifications than expected and my client wants the app to stack the notifications it receives. I've seen tutorials with the solution working over the old GCM mechanism but nothing with FCM. So my doubts are: is possible to stack received push notifications with FCM? or has this to be somehow coded in the backend?

Wonton
  • 1,033
  • 16
  • 33
  • 1
    This is my issue too. Really looking forward for someone from Firebase team to clarify the topic – Defuera Dec 01 '16 at 20:28

3 Answers3

0

Stacking notifications can be done with FCM, it requires a bit of changes at server side also. All this is explained over here in detail:-

https://stackoverflow.com/a/43913156/6157185

Community
  • 1
  • 1
Rishabh
  • 1,827
  • 1
  • 12
  • 9
-1
 NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), NOTIFICATION_CHANNEL_ID);
    Notification mNotification = builder
            .setLargeIcon(image)/*Notification icon image*/
            .setContentText(messageBody)
            .setSmallIcon(R.drawable.dhlone)
            .setContentTitle("DHL")
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(image).bigLargeIcon(image))/*Notification with Image*/
            .setAutoCancel(true)
            .setSound(defaultSoundUri)
            .setContentIntent(pendingIntent)
           .setBadgeIconType(R.drawable.dhlone)
            .setAutoCancel(true)

//use set setAutoCancel(true). it will work

Syed Danish Haider
  • 1,334
  • 11
  • 15
-4

You just need to add .setGroup(GROUP_KEY_XPTO) to your notification builder. All the notifications with the same group id will be stacked.

final static String GROUP_KEY_EMAILS = "group_key_emails";

// Build the notification, setting the group appropriately
Notification notif = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender1)
         .setContentText(subject1)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();

// Issue the notification
NotificationManagerCompat notificationManager =
        NotificationManagerCompat.from(this);
notificationManager.notify(notificationId1, notif);

On the next notification:

Notification notif2 = new NotificationCompat.Builder(mContext)
         .setContentTitle("New mail from " + sender2)
         .setContentText(subject2)
         .setSmallIcon(R.drawable.new_mail)
         .setGroup(GROUP_KEY_EMAILS)
         .build();

notificationManager.notify(notificationId2, notif2);

See more about it here: https://developer.android.com/training/wearables/notifications/stacks.html

Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
  • That is the solution I've found so far but I'm not sure if it's valid for Firebase. In my code I don't have any call to NotificationCompat.Builder. Following the Firebase tutorial you don't need to have it. – Wonton Oct 20 '16 at 17:00
  • Override `onMessageReceived`. You need to follow this: https://firebase.google.com/docs/cloud-messaging/android/receive – Sandro Machado Oct 20 '16 at 17:04
  • 1
    But this method is only called if the app is in the foreground (and in that case I don't need to stack the notifications). With Firebase, if the app is closed or in the background the notification is delivered to the device’s system tray and is there where the notifications are shown one by one, how could I stack them there if my app is not running? – Wonton Oct 20 '16 at 17:10
  • For that I think you need to use data messages: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages – Sandro Machado Oct 20 '16 at 17:34
  • But then, this implies that some changes have to be made in the backend code, no? – Wonton Oct 20 '16 at 17:40
  • Yes, the body/payload is different. Instead of `notification` you need to replace by `data`. You can check the differences here: https://firebase.google.com/docs/cloud-messaging/concept-options#notifications_and_data_messages – Sandro Machado Oct 20 '16 at 17:46
  • OnMessageRecieved is never called, when app is in background. – ROHIT PARMAR Jan 03 '17 at 07:19