Direct Reply Notification for Android in 4 Steps

Yifeng Hou
Software Engineer’s Toolbox
4 min readAug 25, 2020

What is a Direct Reply Notification?

Direct Reply is a notification action. It enables users to directly enter text into the notification. Meanwhile, the app stays in the background. Usually, you would encounter Direct Reply when using messaging apps like Whatsapp.

Let’s build it together!

Today, we will build a Direct Reply Notification together in 4 steps.

  1. Create a Notification Channel
  2. Show Direct Reply Notification
  3. Handle User Input
  4. Update Notification

If you want to check out the source code, you can find it at this GitHub repo. The commits correspond to the four steps in this blog. You can check out each commit along with the steps.

You can also create the initial project via Android Studio or Flutter.

More Details

In this example, I will use an Android project generated via Flutter. But the steps should be the same for a pure Android project. So let’s dive in.

1.Create a Notification Channel

The reason we need to create a channel is that a channel is required for all notification after Android 8.0 (Oreo, API level 26). For more details, click here.

Create a NotificationHelper.kt next to MainActivity.kt with the following content. We will use NotificationHelper.kt to create a channel and show notifications later. I declared NotificationHelper.kt as an object class because we only need one instance of NotificationHelper. If you want to learn more about Kotlin object class. Click here.

Call createChannel from MainActivity.kt

Create a Channel onCreate

2. Show Direct Reply Notification

In order to show a Direct Reply Notification, we will need the following elements.

  1. A RemoteInput to capture user input,
  2. An Intent, and PendingIntent to handle user input
  3. A Notification.Action for users to reveal the RemoteInput.

Then we can use the elements above to build our Direct Reply Notification and launch it via NotificationManager.

To achieve it in code, update NotificationHelper.ktand MainActivity.kt to show Notification onCreate. Also, create an empty NotificationReceiver.kt class next to NotificationHelper.kt for now. We will implement it in the next step.

Show Direct Reply Notification
Show Notification onCreate

Now, when you launch the app, you will see the Direct Reply Notification. Yeah!

Notification shows up. Yeah!

3. Handle User Input

However, if you try to add input in the Direct Reply Notification, you will notice that it hangs with a rotating circle.

This is because the notification needs to be updated after users input value. Let’s handle the user input first before we update the notification. To handle the user input, we need to register the NotificationReceiver class we created from the last step. Update AndroidManifest.xml with the following line outside <activity> but inside <application>.

Register NotificationReceiver

Update NotificationReceiver.kt with the following content.

When we launch the app now, you will see that the user input is captured in the NotificationReceiver.

User Input in logged in the console

However, we still see the spinning circle after adding input. We will fix it with the last step.

4. Update Notification

Lastly, we need to update the notification by calling showNotification from NotificationHelper.kt again from our NotificationReceiver.kt. Or you can also choose to cancel the notification depending on your needs. In this example, we will just update the notification.

Now, if we launch the app again. It should capture user input immediately and show the same notification to capture new input. You can notice when the app is in the background, entering input into the notification does not bring the app to the foreground.

Congratulations! You made it through the 4 steps and unlock the Direct Reply Notification. Yeah! Happy Coding :)

References:

Notifications Tutorial Part 5 — MESSAGING STYLE + DIRECT REPLY — Android Studio Tutorial

--

--

Yifeng Hou
Software Engineer’s Toolbox

AI Solutions Engineer, Technology enthusiast, Business Sustainability Advocate