Overview
Hello Android Nerds,
In this blog we will learn more about smart replies notification in Android.
Android smart replies have been quite beneficial when it comes to quickly viewing a particular content and responding to it. They can save time, be efficient and elevate the User experience. Therefore here we have thoughtfully provided a brief overview of Android Smart Replies.
Setup & Instructions
First of all we need to have a project setup in firebase console, you can follow official documentation for that
After you complete setup, move to this messaging tab & click on “New notification”. You will be able to create & test your notification
You’ll also need the FCM token of your device to get your token in order to test the notifications on your device.
override fun onNewToken(token: String) { super.onNewToken(token) Log.e("TAG", "token: $token") }
That’s it for the setup part let’s move ahead with the smart notifications.
Implementing reply notifications
For implementing reply notifications you need a remote input object which will contain a key with its help you can get your reply message.
val remoteInput = RemoteInput.Builder(KEY_TEXT_REPLY) .build() val replyAction = NotificationCompat.Action.Builder( android.R.drawable.ic_input_add, "reply", resultPendingIntent ) .addRemoteInput(remoteInput) .build() val resultIntent = Intent(this, NotificationReceiver::class.java).putExtra( "timeStamp", System.currentTimeMillis() ).putExtra("name", remoteMessage.notification?.title) .putExtra("message", remoteMessage.notification?.body) val resultPendingIntent = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { PendingIntent.getBroadcast( this, 0, resultIntent, PendingIntent.FLAG_CANCEL_CURRENT or PendingIntent.FLAG_MUTABLE ) } else { PendingIntent.getBroadcast( this, 0, resultIntent, PendingIntent.FLAG_UPDATE_CURRENT ) }
Then move on to creating the replyAction which will be passed into your notification after that we will create our PendingIntent, here what flag you pass is very important based on your SDK level so here I had to add a condition for users who are using android 12 or later versions & users below that.
Now to receive the reply message you need BroadCastReceiver which can receive the broadcast when the user clicks on the reply action & sends a reply message then you can make your API call to pass on the reply message, also we need to pass details like name of sender, message, timestamp of message we will use this later in our BroadcastReceiver
class NotificationReceiver : BroadcastReceiver() { private val CHANNEL_ID = "01" private val NOTIFICATION_ID = 101 private val KEY_TEXT_REPLY = "key_text_reply" override fun onReceive(context: Context, intent: Intent) { val remoteInput = android.app.RemoteInput.getResultsFromIntent(intent) val message: String = remoteInput.getString(KEY_TEXT_REPLY).toString() if (remoteInput != null) { /* ViewModel code for calling your API vm.sendMessage(message) fun onSuccess(){ showNotification(context,message,remote) } */ val time = intent.getLongExtra("timeStamp", 0L) val name = intent.getStringExtra("name") val msg = intent.getStringExtra("message") Log.e("TAG", "onReceive: $name") val remote = NotificationCompat.MessagingStyle.Message( msg, time, Person.Builder().apply { setName(name) }.build() ) showNotification(context, message, remote) } }
MessagingStyle Notification
// Register the channel with the system val notificationManager: NotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val channel = NotificationChannel( CHANNEL_ID, CHANNEL_NAME, NotificationManager.IMPORTANCE_DEFAULT ).apply { description = "descriptionText" } notificationManager.createNotificationChannel(channel) } val builder = NotificationCompat.Builder(this, CHANNEL_ID) .setStyle( NotificationCompat.MessagingStyle(Person.Builder().apply { setName(remoteMessage.notification?.title) }.build()) .addMessage( remoteMessage.notification?.body.toString(), System.currentTimeMillis(), Person.Builder().apply { setName(remoteMessage.notification?.title) }.build() ) ) .setSmallIcon(R.drawable.ic_launcher_foreground) .setPriority(NotificationCompat.PRIORITY_DEFAULT) .addAction(replyAction) with(NotificationManagerCompat.from(this)) { notify(NOTIFICATION_ID, builder.build()) }
Here the style NotificationCompat.MessagingStyle is what we need to learn about, it is a recommended style for chat related notifications & it uses a Person object to show name message & other details in notification.
To create an object of Person class it provides us a builder & with that we can set all the values into the object & pass it in the Message object.
Output
your BusinessGet Expert Assistance
Conclusion
That’s it for the notification reply, thanks for reading here is the link to source code