How to group android notifications like whatsapp?

46,596

Solution 1

Steps to be taken care from the below code.

NotificationCompat.Builder:contains the UI specification and action information
NotificationCompat.Builder.build() :used to create notification (Which returns Notification object)
Notification.InboxStyle: used to group the notifications belongs to same ID
NotificationManager.notify():to issue the notification.

Use the below code to create notification and group it. Include the function in a button click.

private final int NOTIFICATION_ID = 237;
private static int value = 0;
Notification.InboxStyle inboxStyle = new Notification.InboxStyle();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.push_notify_icon);
public void buttonClicked(View v)
{
        value ++;
        if(v.getId() == R.id.btnCreateNotify){
            NotificationManager nManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
            Notification.Builder builder = new Notification.Builder(this);            
            builder.setContentTitle("Lanes");
            builder.setContentText("Notification from Lanes"+value);
            builder.setSmallIcon(R.drawable.ic_launcher);
            builder.setLargeIcon(bitmap);
            builder.setAutoCancel(true);
            inboxStyle.setBigContentTitle("Enter Content Text");
            inboxStyle.addLine("hi events "+value);
            builder.setStyle(inboxStyle);
            nManager.notify("App Name",NOTIFICATION_ID,builder.build());
        }
}

For separate notifications assign different NOTIFICATION_IDs..

Solution 2

For full logic please consider checking my answer.I used the logic with shared preferences and broadcast receiver as i needed to group each user message into single one and be in sight of active notifications.As only by targeting the api level 23 you can get active notifications,it did not help me at all.So i decided to write some slight logic.Check it here if you feel like to.

https://stackoverflow.com/a/38079241/6466619

Share:
46,596
Raúl Pérez López
Author by

Raúl Pérez López

Updated on July 09, 2022

Comments

  • Raúl Pérez López
    Raúl Pérez López almost 2 years

    I don´t know how to group two or more notifications into only one and show a message like "You have two new messages".

  • John
    John almost 8 years
    Right. I find the API confusing: NotificationCompatBuilder.setGroup() method comes from the Wearable API. Its purpose is to manage notification groups on the wearable - though it also affects the phone. But most people are looking for the Gmail-Whastapp-like groups on the phone. Answer is : you have to do it yourself using notificationIDs to update your notifications. And you have to save your data between each notifications, to be able to compute the "summary" notification - which is, again, up to you to build and to send with the same ID to replace the previous "non-summary" notification.
  • Nazim ch
    Nazim ch about 7 years
    I have written code for builder in FcmMessagingService class. inside an inner class onMessageRecieved.I am able to recieve all notifications in detailed but it looks like notification recived from different applications.