Notification In Android- Part 1

Android notification is one of an integral part of the Android System. It has become one of the prominent tools for the Applications to reach out to its intended audience to lure them towards exciting offers and many more interesting facts.

Displaying notification on Android is fairly easy, although, with Android Oreo OS, you need to take care of a couple of extra things. In this blog post, we would have a look at different Styles of Notification but first, let’s classify them into two types:

  • Action Notification
  • Over-the-Top Notification Aka Heads Up Notification

Notification can further be classified into the following categories based on the notification Style:

  • Large Text Notification
  • Inbox Notification
  • Message Notification
  • Big Image Notification

Let’s walk though about each notification one by one:

Action Notification

As the name suggests notification which performs some actions are known to be Action Notification. An action notification has an icon, label and Pending Intent which is fired when any Action is performed. With the introduction of Android N, the icons were removed from the notification and user is displayed notification with Labels.

Over the Top Notification Aka Heads Up Notification

Such kind of notification is common in Android Lollipop and above OS. You might have noticed, when a device is active i.e it is unlocked and you receive a call, a floating window is displayed from the top of the screen. This floating window is what we call Heads Up notification or Over the top notification, Similar to Action notification, action can be performed on these as well like Accept/Decline for a call. For notification to be displayed like a Heads-Up notification you need to have the following conditions satisfied in your App:

  • The user must be interacting with the application in Full-Screen Mode.
  • The notification must have High Priority.
  • Ringtone or vibration must be set for devices running on Android 7.1 or lower
  • The Notification Channel has high importance on devices running Android 8.0 and higher
//for Devices running Android 7.1 or lower
notificationBuilder.setVibrate(new long[0]);

//for Devices running Android 8.0 and above
notificationChannel = new NotificationChannel(notificationChannelID,
                    CHANNEL_NAME, NotificationManager.IMPORTANCE_HIGH);

Large Text Notification

These type of notification are generally used when you want to display large text to the user. We can do that by setting the text inside the method bigText()

notificationBuilder.setStyle(new NotificationCompat.BigTextStyle().bigText(BaseApplication.getInstance().getResources().getString(R.string.large_notification_text)));

Big Picture Notification

Such type of notification is used, when you want to display the notification that contains an image. You can achieve the desired functionality by implementing the following line of code:

Bitmap pic = BitmapFactory.decodeResource(BaseApplication.getInstance().getResources(), R.drawable.background_image);
 notificationBuilder.setStyle(new NotificationCompat.BigPictureStyle().bigPicture(pic).setSummaryText(BaseApplication.getInstance().getString(R.string.image_preview_text)).setBigContentTitle(BaseApplication.getInstance().getString(R.string.image_content_title)));

Method setSummaryText(String) is used to display the subtext for the image and method setBigContentTitle() is used to set the title for the image.

Inbox Style Notification

We generally see such type of notification coming from Gmail/Yahoo, informing the user about the new messages received in their inbox. With InboxStyle a single notification can contain multiple lines i.e it is a consolidated view, hence you do not need to bombard the user with many notifications. We leverage the following method to achieve functionality.

notificationBuilder.setStyle(new NotificationCompat.InboxStyle()
                .addLine("How are you?")
                .setBigContentTitle("New Messages from " +sender)
                .setSummaryText("+2 more"));

Let’s look into each method one by one:

  • addLine(), the method will add a line to the content of the notification.
  • setBigContentTitle(), method will override the text set by ContentTitle() method of NotificationBuilder.
  • setSummaryText(), the method will set the text as the summary and display order is dependent on the OS.

Message Style Notification

This style is used to display the notification for content between two or more people. This style can be created using the following line of code:

        Person user = new Person.Builder().setName("Lisha").build();
        Person currentUser = new Person.Builder().setName("Sarabjit").build();
        NotificationCompat.MessagingStyle.Message message1 = new NotificationCompat.MessagingStyle.
                Message("Hi", 0, user);
        NotificationCompat.MessagingStyle.Message message3 = new NotificationCompat.MessagingStyle.
                Message("Where are you ?", 2, user);
        NotificationCompat.MessagingStyle.Message message2 = new NotificationCompat.MessagingStyle.
                Message("Hello !", 1, currentUser);

        return new NotificationCompat.MessagingStyle(user).

In the second phase of Android Notification- Part 2, we will cover Reply Notification and Media Notification. The project demonstrating the above notifications is available here

Till then Stay Tuned and Happy Coding and Happy New year 2019 !!

44 thoughts on “Notification In Android- Part 1”

  1. Its like you read my mind! You appear to know so much about this, like
    you wrote the book in it or something. I think that you could do with a few pics to drive the message
    home a little bit, but instead of that, this is great blog.
    A fantastic read. I’ll definitely be back.

  2. Howdy! I realize this is somewhat off-topic but
    I needed to ask. Does building a well-established blog such as yours take a large amount of work?
    I am completely new to blogging however I do write in my
    diary everyday. I’d like to start a blog so I can share my experience and
    thoughts online. Please let me know if you have any kind
    of ideas or tips for brand new aspiring blog owners.
    Appreciate it!

  3. I’m truly enjoying the design and layout of your blog. It’s a very easy on the eyes which
    makes it much more pleasant for me to come here and visit more often.
    Did you hire out a developer to create your theme? Exceptional

  4. Если Вы занимаетесь продвижением сайтов (блогов)
    или просто устали платить «SEO специалистам»,то Вам это будет интересно.
    В любом случае Вы извлечёте из данной информации-нужную Вам.

  5. Have you ever thought about including a little bit more than just your articles?
    I mean, what you say is fundamental and everything.
    Nevertheless imagine if you added some great pictures or videos to give your posts more, “pop”!
    Your content is excellent but with images and videos,
    this site could definitely be one of the very best in its niche.
    Terrific blog!

Leave a Reply

Your email address will not be published. Required fields are marked *