简体   繁体   中英

FCM : I want to group all my notifications into one. How can I achieve this?

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM