Android multiple line notification like Gmail app
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.
NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.notification_icon)
.setContentTitle("Event tracker")
.setContentText("Events received")
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
String[] events = {"line 1","line 2","line 3","line 4","line 5","line 6"};
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");
...
// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
inboxStyle.addLine(events[i]);
}
// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);
...
// Issue the notification here.
You are looking for "Big View Style", like this:
Related documentation:
- Notifications
- Using Big View Styles
Here i got the solution :Make sure to create BrodCast Reciever to clear array stack when notification dismiss
static ArrayList<String> notifications = new ArrayList<>();
private static void sendNotification(String messageBody,Context cxt) {
//onDismiss Intent
Intent intent = new Intent(cxt, MyBroadcastReceiver.class);
PendingIntent broadcastIntent = PendingIntent.getBroadcast(cxt.getApplicationContext(), 0, intent, 0);
//OnClick Listener
startWFApplication().addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(cxt, 0, startWFApplication(),
PendingIntent.FLAG_ONE_SHOT);
Uri defaultSoundUri= RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(cxt)
.setSmallIcon(R.drawable.fevicon)
.setContentTitle("Title")
.setContentText(messageBody)
.setAutoCancel(true)
.setSound(defaultSoundUri)
.setContentIntent(pendingIntent);
NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();
// Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Title - Notification");
inboxStyle.setSummaryText("You have "+notifications.size()+" Notifications.");
// Moves events into the expanded layout
notifications.add(messageBody);
for (int i=0; i < notifications.size(); i++) {
inboxStyle.addLine(notifications.get(i));
}
// Moves the expanded layout object into the notification object.
notificationBuilder.setStyle(inboxStyle);
NotificationManager notificationManager =
(NotificationManager) cxt.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(0, notificationBuilder.build());
notificationBuilder.setDeleteIntent(broadcastIntent);
}
public static Intent startWFApplication(){
Intent launchIntent = new Intent();
launchIntent.setComponent(new ComponentName("your.package", "Yyour.package.servicename"));
return launchIntent;
}
BroadCastReciever would look like this:
public class MyBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Notification.notifications.clear();
}
}
In the manifest Put this:
<receiver
android:name="your.package.MyBroadcastReceiver"
android:exported="false" >
</receiver>