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.setPriority(Notification.PRIORITY_HIGH);
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("Hi")
                .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).
                addMessage(message1).
                addMessage(message2).
                addMessage(message3).
                setGroupConversation(true).
                setConversationTitle("Friends");

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 !!

1 thought on “Notification In Android- Part 1”

Comments are closed.