Android Notification Direct Reply With Examples

Android Notification Direct Reply action lets us reply to the notification message, it’s very popular with chat notifications such as Whatsapp and Facebook messenger notification messages.

Android Nougat has introduced several new features. It offers some awesome features such as Inline Reply Actions and Bundled Notifications. In this tutorial, we’ll be implementing Inline Replies in our application

Android Notification Direct Reply

Inline Reply Actions (also known as Direct Replies) allows us to reply to messages from the notifications itself. It makes life easier by removing the need to open applications for providing input. Such features are commonly seen in messaging applications. Direct Replies uses a combination of Notification Actions and Remote Input. Remote Input API provides a mechanism to access the entered text from the notification in our application.

RemoteInput requires the following Strings as input.

  • Unique Key: This is used to correctly identify the text entered from the inline notification later on.
  • Label: This is displayed to the user as a hint text.

Let’s implement a basic application that triggers a Notification with Inline Reply set as the Action.

Android Notification Direct Reply Project Structure

android-inline-replies-project-structure-223x450

Android Notification Direct Reply Code

The code for the activity_main.xml layout is given below.

Note: We’ll deal with the second Button later in the tutorial.

The code for the MainActivity.java class is given below.

Two Notification Actions namely “REPLY” and “DISMISS” are set on the notification. Clicking REPLY would trigger the Direct Reply feature. Running the above code on the emulator would give the following output.

Android Notification Direct Reply

In the above gif, clicking the send icon from the notification shows an indicator. This implies that the notification is waiting for an acknowledgment from the activity. We haven’t processed the replied text in our activity yet. The inline replied text is retrieved using the key set in the RemoteInput instance as shown in the updated code for MainActivity.java below.

In the above code, onNewIntent is invoked when the reply key is pressed(Thanks to the Intent flags SINGLE_TOP and CLEAR_TOP).

The method processInlineReply() is where we fetch the text entered in the notification and set it in the TextView. The notification is then updated (with the same NOTIFICATION_ID as when created) to get rid of the progress indicator and display that the reply was received.

In another world, instead of updating the notification we could have cancelled the notification by using the below snippet.

The updated code above would give the following output in the emulator.
android notification inline reply example

Note: Multiple Inline Reply Actions can be added in a notification by adding multiple addAction(remoteInput) instances on the builder instance. Though it’s recommended to have just one inline reply action button per notification.

Notifications With Reply History

We can display previous inline responses in the Notification too with the help of the method setRemoteInputHistory.

In this application we’ll be storing the Charsequences returned in inline replies in the form of a LinkedList as shown below.

We’ve added a new Button to handle Inline Replies With History.
In the above code, responseHistory holds the history of replies.
The following snippet is used to set the previous inline replies in the notification.

The else part of method processInlineReply() updates the current notification with the new history of inline replies as shown below:

responseHistory.add(0, reply); adds the latest inline reply in the history of replies.

The output that the above code would give is:
android notification direct reply history

Note: The above concept is for demonstration purposes only. You can tweak it according to your requirements.

Inline Replies do not work on versions prior to Android N. So to make the application functional on them, you can add a Basic EditText in the activity.

This brings an end to this tutorial. You can download the final Android DirectReplyNotifications Project from the link below.

By admin

Leave a Reply

%d bloggers like this: