Notification in Android – Part 2

In our last blog, we covered basics of Android Notification along with their different styles. If you have not read it yet, I will recommend you to read it first before deep diving into other notification styles.

In this blog post, we will be covering following Notification:

  • Reply Notification
  • Media Notification

Reply Notification Aka Direct Reply Notification

With the introduction of Android N, Direct Reply Notification was rolled out to Android Users in order to reduce the numbers of steps required to act on the notification once received. Next question which pops in my mind is how Reply Notification differ from Normal Notification? Let’s Look into it:

Reply Notification is created using two set of features:

  • Action
  • Remote Input

Action as we know signify a user action on the notification and Remote Input once added, make the Android OS understand that an input is requested from the user.

Let’s have a look at how we can create a Remote Input in our notification:

If you look carefully in the above code, there are two things that might need your attention, first REPLY, and second replyLabel. KEY_REPLY will be used to fetch the user input later on and replyLabel is the action which will be shown on the notification.

Now, focus our attention towards creating Notification Action:

If you look at the above code, we are mapping the action with RemoteInput alongside, we also need an icon and a replyLabel (which we create in the previous step)

Now comes the most important part of building this notification, deciding the Pending Intent. Generally, some of the developers forget to remember that Pending Intent must depend on the OS version of the user.

As we know now, Reply Notification was introduced in Android N, hence deciding the Pending Intent for Android N or above can either be a BroadcastReceiver or a Long running Service. What about Android Versions prior Android N ?

So, for all those versions we can use our Android OS backbone an Activity and give our own User Interface!! Let’s have a look, how we will create it:

Looking at above code, you might be thinking what is this TaskStackBuilder? Let’s deep dive a little into this as well:

To understand the concept well, let’s assume we have two Activities, Activity A and Activity B. Activity A consist of List of emails and Activity B displays any Email in Detail. Now when the application is not alive and you get a notification for an Email. As soon as user taps on this notification, Email description i.e Activity B is displayed to the user. Now when the user back press from Activity B, Activity A i.e Email ListActivity should be displayed to the user. In such special Scenerio’s we make use of Task Stack Builder.

Now comes the question, how we will be reading the reply which the user sent from the notification. Let’s have a look at that piece of code:

So in our broadcast receiver we have checked for the action whether it correspond to the action send by our Pending Intent?

In the above method, we fetch the Data from bundle using the same KEY which we used while creating our RemoteInput object.

Media Notification

Media Notification was introduced with Android Lollipop. In case you are developing a Music App and the target Android version is Lollipop and above, you have just struck gold:). Android has given the developers a Life by bringing Media Notification through which you can control Media Sessions.

In Order to make use of Media Session inside Media Notification you need to get the permission from the User:

So to demonstrate the Idea, we have created a background Service, which when started will trigger a notification and notify the user that a song is playing. In this blog, we will focus on the media Notification rather than building the complete Media Application. Although, for basic understanding let’s go through some of the functions:

As soon as we start the service, we will get the Intent object in this method where we are initialising the MediaSession and then Handling the Intent Object.

In the above method, we are retrieving the values passed from the Activity and triggering the corresponding Intent Action.

In the above method, we extract the action and call the corresponding transport control method.

Now Let’s look into how we create the Media Notification:

If you look carefully at the above code, Firstly, we have used NotificationCompat.MediaStyle() to display media notification. Secondly, we have added Actions to the Notification, one can add up to 5 Actions when notification is in expanded state. Lastly, setShowActionsInCompactView(1, 2, 3) method tells the OS that when the notification is in collapse mode, add these indices to the view so that only these actions are visible to the user.

You can find complete working project demonstration the Reply and Media Notification from here.

Feel free to reach out to me for any queries or comments. Cheers!

Leave a Reply

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