Inbox Style Notification Like Whatsapp
In this post we will discuss about Inbox style notification like Whatsapp, Gmail and other chat applications are having.
Android notification will be displayed in Notification area and to see the details regarding the notification, the user can expand it in by open 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 section, you can use NotificationCompat.InboxStyle.
Syntax:
1 2 3 4 5 |
Notification notification = new Notification.InboxStyle(builder) .addLine() .setBigContentTitle() .setSummaryText() .build(); |
Now we will see step by step example of Inbox style notification. First create object of your intent, it will be targeted activity where user will be redirected when click on notification.
1 2 3 |
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.
1 2 3 4 5 |
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 Inbox style notification object.
1 2 3 4 5 6 7 8 |
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 summary of your notification.
Now final step is to show notification in notification area.
1 2 |
NotificationManager notificationManager = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE); notificationManager.notify(121,notification); |
Here what it looks like when you execute this code :
Ravi Rupareliya
Latest posts by Ravi Rupareliya (see all)
- Dialogflow Entities With Actions on Google - May 7, 2020
- Actions on Google Using Cloud Functions - February 3, 2020
- Create WhatsApp Stickers Android Application - April 19, 2019