How to Direct reply on Notification in Android Studio using Kotlin.
Last updated Dec 20, 2021
In this article, we will see how to direct reply from notification like Whatsapp in Android Studio by using Kotlin Language.
Notification: A notification is a message displayed outside your app's UI by Android to offer the user with reminders, communication from other people,
or other timely information from your app. Users can access your app or perform an action immediately from the notice by tapping it.
You might have seen this feature already in the very popular messaging app WhatsApp.
Implementation:
Step 1. Create a new Project in android studio.
Go to File > New > New Project > Empty Activity > Next > Enter Name > Select Language Kotlin > Finish.
Step 2. Go to activity_main.xml file and add the following code
Step 3. Open MainActivity.kt file and add the following code below setContentView(R.layout.activity_main).
val btn = findViewById(R.id.create)
val channelId = "My_Channel_ID"
val notificationId = 1
createNotificationChannel(channelId)
btn.setOnClickListener {
if (Build.VERSION.SDK_INT >= 24) {
// Create an instance of remote input builder
val remoteInput: RemoteInput = RemoteInput.Builder("KEY_TEXT_REPLY")
.run {
setLabel("Write your message here")
build()
}
// Create an intent
val intent = Intent(this, NotificationReceiver::class.java)
intent.action = "REPLY_ACTION"
intent.putExtra("KEY_NOTIFICATION_ID", notificationId)
intent.putExtra("KEY_CHANNEL_ID", channelId)
intent.putExtra("KEY_MESSAGE_ID", 1)
// Create a pending intent for the reply button
val replyPendingIntent: PendingIntent = PendingIntent.getBroadcast(
this,
101,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
// Create reply action and add the remote input
var action: NotificationCompat.Action = NotificationCompat.Action.Builder(
R.drawable.ic_baseline_settings_24,
"Reply",
replyPendingIntent
).addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build()
// Build a notification and add the action
val builder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.home)
.setContentTitle("Developers")
.setContentText("Hi! How are you?")
.addAction(action)
private fun createNotificationChannel(channelId:String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val name = "My Channel"
val channelDescription = "Channel Description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId,name,importance)
channel.apply {
description = channelDescription
}
// Finally register the channel with system
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
In above code we defined some constant values like CHANNEL_ID, CHANNEL_NAME. Created Notification Channel, inside onCreate().
Added Click Listener event to create Button which is available in activity_main.xml file and inside onClick method we defined the code for displaying the notification.
if (Build.VERSION.SDK_INT >= 24) {
// Create an instance of remote input builder
val remoteInput: RemoteInput = RemoteInput.Builder("KEY_TEXT_REPLY")
.run {
setLabel("Write your message here")
build()
}
// Create an intent
val intent = Intent(this, NotificationReceiver::class.java)
intent.action = "REPLY_ACTION"
intent.putExtra("KEY_NOTIFICATION_ID", notificationId)
intent.putExtra("KEY_CHANNEL_ID", channelId)
intent.putExtra("KEY_MESSAGE_ID", 1)
// Create a pending intent for the reply button
val replyPendingIntent: PendingIntent = PendingIntent.getBroadcast(
this,
101,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
// Create reply action and add the remote input
var action: NotificationCompat.Action = NotificationCompat.Action.Builder(
R.drawable.ic_baseline_settings_24,
"Reply",
replyPendingIntent
).addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build()
// Build a notification and add the action
val builder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.home)
.setContentTitle("Developers")
.setContentText("Hi! How are you?")
.addAction(action)
Right now you will see error on NotificationReceiver.class, as we haven’t created it yet.
Step 5. Create a new kotlin class and enter name (NotificationReceiver.kt) and click ENTER . Now, finally we need to handle the input, and other buttons in the notification. For this we will create a Broadcast Receiver.
.
Creating a Notification Action Handler
Add the following code to NotificationReceiver.kt file:
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.apply {
if ("REPLY_ACTION".equals(action)){
val message = replyMessage(this)
val messageId = getIntExtra("KEY_MESSAGE_ID",0)
Toast.makeText(context,"$messageId : $message",Toast.LENGTH_LONG).show()
}
context?.apply {
val notificationId = getIntExtra("KEY_NOTIFICATION_ID",0)
val channelId = getStringExtra("KEY_CHANNEL_ID")
// Build a notification and add the action
val builder = NotificationCompat.Builder(this, channelId.toString())
.setSmallIcon(R.drawable.ic_secure)
.setContentTitle("Successful")
.setContentText("Message sent!")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btn = findViewById(R.id.create)
val channelId = "My_Channel_ID"
val notificationId = 1
createNotificationChannel(channelId)
btn.setOnClickListener {
if (Build.VERSION.SDK_INT >= 24) {
// Create an instance of remote input builder
val remoteInput: RemoteInput = RemoteInput.Builder("KEY_TEXT_REPLY")
.run {
setLabel("Write your message here")
build()
}
// Create an intent
val intent = Intent(this, NotificationReceiver::class.java)
intent.action = "REPLY_ACTION"
intent.putExtra("KEY_NOTIFICATION_ID", notificationId)
intent.putExtra("KEY_CHANNEL_ID", channelId)
intent.putExtra("KEY_MESSAGE_ID", 1)
// Create a pending intent for the reply button
val replyPendingIntent: PendingIntent = PendingIntent.getBroadcast(
this,
101,
intent,
PendingIntent.FLAG_UPDATE_CURRENT
)
// Create reply action and add the remote input
var action: NotificationCompat.Action = NotificationCompat.Action.Builder(
R.drawable.ic_baseline_settings_24,
"Reply",
replyPendingIntent
).addRemoteInput(remoteInput)
.setAllowGeneratedReplies(true)
.build()
// Build a notification and add the action
val builder = NotificationCompat.Builder(this, channelId)
.setSmallIcon(R.drawable.home)
.setContentTitle("Developers")
.setContentText("Hi! How are you?")
.addAction(action)
private fun createNotificationChannel(channelId:String) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O){
val name = "My Channel"
val channelDescription = "Channel Description"
val importance = NotificationManager.IMPORTANCE_DEFAULT
val channel = NotificationChannel(channelId,name,importance)
channel.apply {
description = channelDescription
}
// Finally register the channel with system
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
}
class NotificationReceiver : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.apply {
if ("REPLY_ACTION".equals(action)){
val message = replyMessage(this)
val messageId = getIntExtra("KEY_MESSAGE_ID",0)
Toast.makeText(context,"$messageId : $message",Toast.LENGTH_LONG).show()
}
context?.apply {
val notificationId = getIntExtra("KEY_NOTIFICATION_ID",0)
val channelId = getStringExtra("KEY_CHANNEL_ID")
// Build a notification and add the action
val builder = NotificationCompat.Builder(this, channelId.toString())
.setSmallIcon(R.drawable.ic_secure)
.setContentTitle("Successful")
.setContentText("Message sent!")