Direct Reply Notification in Android like WhatsApp Tutorial

Hey guys, here is another useful tutorial. Today we will learn about Direct Reply Notification in Android. You might have seen this feature already in the very popular messaging app WhatsApp. Now we can directly reply to WhatsApp messages without even opening WhatsApp.

So if you are searching how you can implement this feature in your Android Project then, this is what we will be learning in this tutorial. So let’s begin.

Creating a new Android Studio Project

As always let’s do this thing in a new Android Studio Project. So for my Direct Reply Notifications I’ve already created a project named DirectReplyNotification.

Creating Interface

Here we have nothing much to do. We will create only a simple button and pressing that button will create a notification. As this post is only for an example so we are not going to cover about push notifications here. We will only see how Direct Reply Notification works.  If you want to learn about push notification you can check Firebase Cloud Messaging Tutorial here.

  • So come inside activity_main.xml and create a button here.

  • The above xml will generate the following design.
Direct Reply Notification Example
Direct Reply Notification Example

Defining Constants

  • For this project we need some constants values, like CHANNEL_ID, CHANNEL_NAME etc. So, first define all the Constant values inside

Creating Notification Channel

  • So, from Android Nougat, creating a notification channel is compulsory for displaying notifications. Inside onCreate(), we will check if the device version is Android N or greater we will create a notification channel.

Adding Click Listener to Button

  • Now, we will attach an OnClickListener to the Button. So inside onCreate() write the following lines of code.

  • Inside onClick() we are firing a method named displayNotification(). So we need to define this method inside MainActivity.

  • Inside this method, we will build the notification.

Building Direct Reply Notification

  • Now lets define the displayNotification() method to display our Direct Reply Notification.

  • Right now you will see error on NotificationReceiver.class, as we haven’t created it yet.

Creating a Notification Action Handler

Now, finally we need to handle the input, and other buttons in the notification. For this we will create a Broadcast Receiver.

  • Create a class named NotificationReceiver and write the following code.

  • We also need to define this receiver inside our AndroidManifest.xml file. So write the following xml code inside the <application> tag.

Displaying Direct Reply Notification

  • Now, we have everything, and to display the notification, we just need to play the application and then hit on the Create Notification Button.
Android Direct Reply Notification
Android Direct Reply Notification
  • Bingo! it is working absolutely fine.

Download Source Code

Now, if you still having some issues, you can check my GitHub Repository for this project from the link given below.

So, that’s all for this Direct Reply Notification Example friends. If you are having any confusions regarding this tutorial, feel free to leave your comments. And if you think the post is helpful please SHARE it with your friends. Thank You 🙂

Leave a Comment