0

I have used FCM to receive notifications. When I receive multiple notifications it fills up the notification status bar. How do I group them into one?

My code:

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("ABC")
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setGroup(GROUP_KEY_NOTIFICATIONS)
                .setContentIntent(pendingIntent);

Using setGroup() it is not working.

Edit:

.setGroupSummary(true)
is working for OS lollipop but not in marshmallow. Can you please help

Aparajita Sinha
  • 514
  • 1
  • 10
  • 21

1 Answers1

-1

To group notifications, you have to insert notifications in database and using NotificationCompat.InboxStyle you can group notifications.

PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
                PendingIntent.FLAG_UPDATE_CURRENT);

        Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
                .setContentTitle("ABC")
                .setPriority(Notification.PRIORITY_HIGH)
                .setContentText(messageBody)
                .setAutoCancel(true)
                .setSound(defaultSoundUri)
                .setGroup(GROUP_KEY_NOTIFICATIONS)
                .setContentIntent(pendingIntent);

     NotificationCompat.InboxStyle inboxStyle = new NotificationCompat.InboxStyle();

                    for (int i = 0; i < data.size(); i++)
                    {

                        inboxStyle.addLine(data.get(i));
                    }

                    inboxStyle.addLine("");

                notificationBuilder.setStyle(inboxStyle);

Where data is list fetched from database containing all notifications.

Ragini
  • 332
  • 1
  • 4
  • 16