Updated 11 October 2021
The Direct reply is the feature where a user sends a message from the notification itself without using the app’s UI. Users can tap the notification to open your app or take an action directly from the notification. The direct reply feature was introduced in Android 7.
A notification is a message that Android displays outside your application to provide the user with reminders, communication from other people, or other timely information from your app. Users can tap the notification to open your app or take an action directly from the notification.
1 2 3 |
dependencies { implementation "com.android.support:support-compat:28.0.0" } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
class MainActivity : AppCompatActivity() { lateinit var mContentViewBinding: ActivityMainBinding override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mContentViewBinding=DataBindingUtil.setContentView(this, R.layout.activity_main) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { val mNotificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager val importance = NotificationManager.IMPORTANCE_HIGH val mChannel = NotificationChannel( CHANNEL_ID, CHANNEL_NAME, importance) mChannel.description = CHANNEL_DESC mNotificationManager.createNotificationChannel(mChannel) } /*Click listener added.*/ mContentViewBinding.createNotificationBt.setOnClickListener { createNotification() } } private fun createNotification() { //Pending intent notification Action button val helpPendingIntent = PendingIntent.getBroadcast( this@MainActivity, REQUEST_CODE_ACTION, Intent(this@MainActivity, NotificationReceiver::class.java) .putExtra( KEY_ACTION, REQUEST_CODE_ACTION ), PendingIntent.FLAG_UPDATE_CURRENT ) //We need this object for getting direct input from notification val remoteInput = RemoteInput.Builder(NOTIFICATION_REPLY) .setLabel(getString(R.string.reply_here)) .build() //For the remote input we need this action object val action = NotificationCompat.Action.Builder( android.R.drawable.ic_dialog_email, getString(R.string.reply), helpPendingIntent ) .addRemoteInput(remoteInput) .build() //Creating the notification builder object val mBuilder = NotificationCompat.Builder(this, CHANNEL_ID) .setSmallIcon(android.R.drawable.ic_dialog_info) .setContentTitle(getString(R.string.app_name)) .setContentText(getString(R.string.remote_message)) .setAutoCancel(true) .setContentIntent(helpPendingIntent) .addAction(action) .addAction(android.R.drawable.ic_menu_directions, getString(R.string.action), helpPendingIntent) //initializing the notification manager and showing the notification. val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(NOTIFICATION_ID, mBuilder.build()) } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/create_notification_bt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/create_notification" /> </RelativeLayout> </layout> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
class DirectReplyNotificationReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { //getting the bundle from intent val bundle = RemoteInput.getResultsFromIntent(intent) if (bundle != null) { val reply = bundle.getCharSequence(NOTIFICATION_REPLY) //updated value get from bundle val mBuilder = NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(android.R.drawable.ic_menu_info_details) .setContentTitle(String.format(context.getString(R.string.your_reply), reply)) val notificationManager = context.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager notificationManager.notify(NOTIFICATION_ID, mBuilder.build()) } /*--if help or send button is clicked--*/ if (intent.getIntExtra(KEY_ACTION, 0) == REQUEST_CODE_ACTION) { Toast.makeText(context, context.getString(R.string.apply_action_here), Toast.LENGTH_LONG).show() } } } |
1 2 3 4 5 6 7 8 9 10 11 |
object Constants { const val NOTIFICATION_REPLY = "NotificationReply" const val CHANNEL_ID = "test_channel" const val CHANNEL_NAME = "Test Direct Reply" const val CHANNEL_DESC = "Test Til" const val KEY_ACTION = "keyAction" const val NOTIFICATION_ID = 101 const val REQUEST_CODE_ACTION = 102 } |
1 2 3 4 5 6 7 8 9 10 11 12 |
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" ……> ---- <receiver android:name=".DirectReplyNotificationReceiver" android:enabled="true" android:exported="false"/> </application> |
1 2 3 4 5 6 7 8 9 10 |
<resources> <string name="app_name">Direct Reply</string> <string name="apply_action_here">Apply your Action Here</string> <string name="your_reply">Your Reply: %s</string> <string name="reply_here">Reply here</string> <string name="reply">Reply</string> <string name="remote_message">Remote Message can be here</string> <string name="action">Action</string> <string name="create_notification">Create Notification</string> </resources> |
As all the code is completed, now let’s run the app:
. . . . . . . . .
That’s it from my side for today, thanks for reading it until now. If you want to read more about the notification check the official document: Click here
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.