651.288.7000 info@intertech.com

Android Nougat Development Tutorial (Part 1): Notification Inline Replies

by | Mar 7, 2017

Introduction

Android 7, also known as Nougat, introduces several new features. This part of our Android Nougat Development Tutorial demonstrates one enhancement made to Notifications: Inline Reply Actions.

Inline reply actions allow the user to enter a response to a notification without having to open the application; the response is entered directly in the notification drawer. This is a nice improvement to the user’s experience; they don’t need to switch applications in order give a response to a notification.

The code used in this tutorial is available in the demo app.

 

Notification Actions

To start, let’s review how actions work in Android prior to Nougat. In those versions, you can (and should) provide a default action that occurs when your notification is clicked. This is done by creating a PendingIntent and setting it on the notification builder by calling setContentIntent. This does not affect the appearance of the notification. Typically, this default action starts an Activity in your application.

Additional actions are supported starting in Android 4.1. These are added to a notification using addAction. Additional actions appear as buttons at the bottom of the notification. Here is what that looks like:

Basic Actions

Basic Actions

Beginning with Android 7.0 Nougat development, these actions can be enhanced to capture input without starting an Activity. This example shows actions in Nougat:

Actions in Nougat

Actions in Nougat

For actions that allow inline replies, clicking the action will open a text input. The “Respond” action above allows inline replies. Here is what the notification looks like after selecting the Respond action:

Inline Action Reply

Inline Action Reply

The response is available to the pending intent you specified for the action. Furthermore, when issuing the notification, you can include a history of previous replies. Here is an example of what that looks like:

Reply History

Reply History

 

Implementing Inline Reply Actions

Now that we’ve seen what they look like, let’s see how we can add inline replies to an application’s notifications.

Actions with inline replies require a RemoteInput. Building a remote input simply requires a label and a key. The key is used to access the reply from the pending intent. Here is how we can build the remote input:

Next, create an action that uses the remote input and add the action to the builder:

Now, when the notification is created using NotificationManagerCompat (or NotificationManager if you are not using the support libraries), it will have an inline reply action.

Next, we need to retrieve the input. In the component started by the pending intent (e.g. an Activity or Service), the remote input is available on the Intent. The user’s input is accessed using the same key that was used when building the remote input:

After the reply has been entered, a progress spinner is displayed to indicate that something is happening. It is up to you to stop the spinner, once the reply has been processed. There are at least a couple of ways to handle this:

  • Cancel the notification by calling cancel
  • Re-issue the notification, and include an updated message (e.g. “Successful reply”)

In the demo application, I use the second option. Additionally, I am also using setRemoteInputHistory to display previous responses. Here is what the notification looks like with the spinner, and after it has been re-issued (after the reply has been processed):

Inline Reply Spinner

Inline Reply Spinner

nougat_inline_action_reply_complete

Reply Complete, with History

 

Backwards Compatibility

So far we have a notification with an inline action that works on Android 7. However, we need to consider how our app will work when running on previous versions of Android. This is true anytime we add new System features to our applications. We want to take advantage of those new features as soon as possible, to provide a good experience to users who upgrade early. But, we also want to keep our app working well on previous versions of Android.

Therefore, we need to think about how to handle the inline reply from within the pending intent, and how to handle the scenario where the remote input is not present. Until now, we haven’t given much thought on how to handle the reply, other than to say it is handled by an intent.

For handing inline replies, I am going to use a Service. That way, our app can process the reply without any further user interaction (there is no need to start an Activity). After all, that is the key benefit of inline reply actions: our app gets user input with minimal disruption.

For Android versions prior to Nougat, the action in the notification is simply a button with no option for a reply. When the button is clicked and the Service is invoked, the remote input associated with the intent is null. How do we handle that scenario?

We still want to allow the user to enter a reply, so we should start an Activity that contains that functionality. One tempting way to handle this might be to start the Activity from the Service, once it is determined the remote input is null.

However, you don’t want to do this. Android handles notification actions differently for different intent types. If the Action invokes an Activity, the notification drawer is closed and the Activity receives focus. On the other hand, if the Action invokes a Service, the notification drawer stays open. Thus, if our pending intent is for a Service, and we start an Activity from that service, the result is the Activity is started behind the notification drawer. This is not a good user experience.

A better approach is to set the action’s pending intent based on the Android API level. If the version is Nougat or greater, use a Service. Otherwise, use an Activity. Android Nougat development provides a simply way to check the API level:

Instead of starting an Activity on prior versions of Android, it might make sense to not create the Action at all. The correct way to handle this scenario is determined by the specific needs of the application.

Conclusion

Notifications with an Inline Action Reply is a nice enhancement to the Android notification system. This blog post shows how to add inline replies to notifications in your application.

To learn about another new feature introduced in Android 7.0 Nougat, stay tuned for Part 2  in this Android Nougat Development Tutorial on Bundled Notifications.

Both of these new features are demonstrated in the demo application.

Working with Android 7 Nougat? We can help you implement these new features and even help create the Android app that you are building. Take a look at what we can do on our Android Consulting page.

Intertech also offers Android Training! See if you can find a course that best fits you and your team.

Follow Us

Categories

Android Development Consulting
Tips for a Virtual Development Environment
Android Course Finder
Free eBook: iOS and Android Push Notifications Tutorial

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This