Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to group android notifications like whatsapp?

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

like image 544
Raúl Pérez López Avatar asked Oct 09 '15 14:10

Raúl Pérez López


People also ask

Can I group notifications on Android?

Starting in Android 7.0 (API level 24), you can choose to display related notifications in a group (previously called "bundled" notifications). For example, if your app shows notifications for received emails, you should put all notifications into the same group so they can be collapsed together.

How do I stack notifications on Android?

To create a stack, call setGroup() for each notification you want in the stack and specify a group key. Then call notify() to send it to the wearable. final static String GROUP_KEY_EMAILS = "group_key_emails"; // Build the notification, setting the group appropriately Notification notif = new NotificationCompat.

How do I get group notifications on my Samsung?

Android Notification Grouping Simply enter any value in the “Group Key” field under the Android Options when Sending Push Messages. This is the android_group parameter on the REST API. Notifications with the same group key will automatically group on the device.


1 Answers

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..

like image 154
Sackurise Avatar answered Sep 28 '22 04:09

Sackurise