Android notifications will be displayed in the notification area and to see the details regarding the notification, the user can expand it by opening the notification drawer.
Note - Inbox style notification is not available on platforms prior to Android 4.1.
When you want to display multiline text detail sections, you can use NotificationCompat.InboxStyle.
- Notification notification = new Notification.InboxStyle(builder)
- .addLine()
- .setBigContentTitle()
- .setSummaryText()
- .build();
Now we will see a step by step example of inbox style notification. First create the object of your intent, it will be targeted activity where the user will be redirected when clicking on the notification.
- Intent resultIntent = new Intent(context, ResuleActivity.class);
- resultIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
- PendingIntent piResult = PendingIntent.getActivity(this, 0, resultIntent, 0);
Create an object of Notification.Builder.
- Notification.Builder builder=new Notification.Builder(context)
- .setSmallIcon(R.mipmap.ic_launcher)
- .setContentTitle("contentTitle")
- .setContentText("contentText")
- .setContentIntent(piResult);
contentTitle and contentText will be shown when your notification is not expanded. Now create an inbox style notification object.
- Notification notification = new Notification.InboxStyle(builder)
- .addLine("First Message")
- .addLine("Second Message")
- .addLine("Third Message")
- .addLine("Fourth Message")
- .setBigContentTitle("Here Your Messages")
- .setSummaryText("+3 more")
- .build();
Here you can give your messages in addLine(), while setSummaryText() will be used to show a summary of your notifications.
Now the final step is to showthe notification in the notification area.
- NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
- notificationManager.notify(121,notification);
Here is what it looks like when you execute this code,