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

- 14,436
- 26
- 44
- 46

- 538
- 1
- 5
- 14
-
1the documentation says **Note: If your app sends four or more notifications and does not specify a group, the system automatically groups them together on Android 7.0 and higher.** https://developer.android.com/training/notify-user/group – Wini Nov 26 '20 at 13:23
4 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..

- 15,198
- 7
- 41
- 70

- 2,804
- 1
- 18
- 18
-
2I 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. – Nazim ch Apr 18 '17 at 06:51
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.

- 1
- 1

- 575
- 2
- 7
- 20
You need to create the notification so that it can be updated with a notification ID by calling NotificationManager.notify(ID, notification)
.
The following steps need to be created to update the notification:
- Update or create a
NotificationCompat.Builder
object - Build a Notification object from it
- Issue the Notification with the same ID you used previously
An example taken from the android developer docs:
mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Sets an ID for the notification, so it can be updated
int notifyID = 1;
mNotifyBuilder = new NotificationCompat.Builder(this)
.setContentTitle("New Message")
.setContentText("You've received new messages.")
.setSmallIcon(R.drawable.ic_notify_status)
numMessages = 0;
// Start of a loop that processes data and then notifies the user
...
mNotifyBuilder.setContentText(currentText).setNumber(++numMessages);
// Because the ID remains unchanged, the existing notification is updated.
mNotificationManager.notify(notifyID, mNotifyBuilder.build());
...
Also see the Android docs on Stacking Notifications https://developer.android.com/training/wearables/notifications/stacks.html

- 2,475
- 10
- 28
- 35
-
8Right. 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. – John May 04 '16 at 16:22
You can stack all your notifications into a single group using the setGroup
method and passing your groupId string as parameter.
builer.setGroup("GROUP ID STRING" ) ;
NotificationManager nManager = (NotificationManager)
getSystemService(NOTIFICATION_SERVICE);
Notification.Builder builder = new Notification.Builder(this);
builder.setContentTitle("Lanes");
builder.setGroup("GROUP_ID_STRING");
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());

- 12,274
- 10
- 84
- 125