I am trying to create a multiple line notification like the Gmail application does as shown in the image below (the 5 notifications grouped under one notification)
I have tried various examples but can only seem to create single notifications like
public void createSingleNotification(String title, String messageText, String tickerttext) {
int icon = R.drawable.notification_icon; // icon from resources
CharSequence tickerText = tickerttext; // ticker-text
long when = System.currentTimeMillis(); // notification time
Context context = getApplicationContext(); // application Context
CharSequence contentTitle = title; // expanded message title
CharSequence contentText = messageText; // expanded message text
Intent notificationIntent = new Intent(this, MainActivity.class);
Bundle xtra = new Bundle();
xtra.putString("title", title);
xtra.putString("message", messageText);
notificationIntent.putExtras(xtra);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, PendingIntent.FLAG_ONE_SHOT
+ PendingIntent.FLAG_UPDATE_CURRENT);
String ns = Context.NOTIFICATION_SERVICE;
NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns);
Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
notification.defaults |= Notification.DEFAULT_LIGHTS;
notification.defaults |= Notification.DEFAULT_SOUND;
notification.defaults |= Notification.FLAG_AUTO_CANCEL;
notification.flags = Notification.DEFAULT_LIGHTS
| Notification.FLAG_AUTO_CANCEL;
final int HELLO_ID = 0;
mNotificationManager.notify(HELLO_ID, notification);
}
I am not sure how to create a notification group that I can add lines to.
You are looking for "Big View Style", like this:
Related documentation:
There's currently no such thing as a "notification group", but rather individual notifications with multiple lines of text (as has already been pointed out this is done with the
bigContentView
, which you want to use theBuilder
to create). To update a notification to add more data, just post it again (with the same ID and tag) with a bigger string in BigTextStyle, or more lines in InboxStyle.Here i got the solution :Make sure to create BrodCast Reciever to clear array stack when notification dismiss
BroadCastReciever would look like this:
In the manifest Put this:
I know this has been asked a long time ago and probably lots of users like me were searching here as well but I will give the full code of notification in this manner. Imagine a scenario where you have a chat application and where you want to get notifications grouped BY EACH USER!notice by each user. So the best way to do it to notify by unique user id, thus you have to write a single code to notify as you can`t guess how many notifications are present at that moment if you are targeting below API level 23 which is getting almost lots of users out of sight. So what shall you do when you want to group each user received notification and keep logic? The best way I did that was using shared preferences with some slight logic there let me post the code with comments.
(By this you targeting to lower APIs so the KitKat will handle this logic too)
The class which imitates the notification as a test.
The broadcast receiver for delete intent. Do not forget to add a receiver to your manifest!