Usman Riaz
Usman Riaz

Reputation: 3020

Handling multiple notification in android

I have an application which generates notification when a particular event that user entered on app occurs.Now for example user entered 7 events and he is not clicking on notifications then notification bar will became full. I don't want that. I want to show just one icon of notification but all 7 notifications. Just like whatsapp where only 1 notification icon is shown.

Upvotes: 1

Views: 1139

Answers (1)

AADProgramming
AADProgramming

Reputation: 6335

I guess what you are looking at is "Stacking" of Notifications.

There are couple of significant APIs here.
1. setGroup() : This sets the notification to be part of a group of notifications sharing the same key.
2.setGroupSummary(): Set this notification to be the group summary for a group of notifications.

Also, we Need to have same notification builder id. Below are my declarayions in class:

final static String GROUP_KEY_EMAILS = "group_key_emails";
int UNIQUE_NOTIFICATION_ID=422;

And Sample code to post notification:

Notification summaryNotification = new NotificationCompat.Builder(context)        
        .setContentTitle("2 new messages")        
        .setSmallIcon(R.drawable.ic_launcher)                   
        .setStyle(new NotificationCompat.InboxStyle()                
        .addLine("Notification 1   First line of info")                
        .addLine("otification 2   Second line of info")  
        .addLine("otification 3   Second line of info")
        .setBigContentTitle("3 new messages")                
        .setSummaryText("[email protected]"))        
        .setGroup(GROUP_KEY_EMAILS)        
        .setGroupSummary(true)        
        .build();


        NotificationManagerCompat notificationManager =  NotificationManagerCompat.from(this);
        notificationManager.notify(UNIQUE_NOTIFICATION_ID, summaryNotification);

This will show the UI As below:

enter image description here

Upvotes: 5

Related Questions