{
"$type": "site.standard.document",
"canonicalUrl": "https://segunfamisa.com/posts/notifications-direct-reply-android-nougat",
"coverImage": {
"$type": "blob",
"ref": {
"$link": "bafkreiahneujdsbk65muayasppw4jpbummwxm2sb6duf3mb4dgiug6p2mi"
},
"mimeType": "image/png",
"size": 179285
},
"description": "Tutorial on how to implement notifications with direct reply in Android N",
"path": "/posts/notifications-direct-reply-android-nougat",
"publishedAt": "2016-09-25T06:42:45.000Z",
"site": "at://did:plc:a5mekodp4afxadlpr4hp2wci/site.standard.publication/3mm2oa7vz5327",
"tags": [
"android",
"android studio",
"tutorial",
"nougat",
"notifications"
],
"textContent": "Introduction\n\nNotifications is one feature that improves at almost every Android update.\nFrom single tap notifications, to actionable notifications, Android N also introduces some new features to Notifications in Android.\nAndroid N introduces direct reply, notification bundling and other cool features.\n\nIf you have Android N installed on your device, you may have noticed the new way the notifications\nappear. You may also have noticed that you can reply messages in-line right from your notification.\nI noticed this behaviour has already been implemented on quite a number of apps including Twitter, Slack, WhatsApp.\n\nThis is made possible by Direct reply. Direct reply aims to reduce the number of steps between receiving a notification and acting on it.\n\nThe image below is direct reply in action on Slack for Android app.\n\n<p align=\"center\">\n <img src=\"http://i.imgur.com/R8BUkFt.jpg\">\n</p>\n\nIn this post, we will walk through the steps in implementing direct reply in your apps.\nI will cover the new notification styles and bundling notifications in another post.\n\nHow it works?\nDirect reply makes use of a combination of notification, action and Remote Input. Once a Remote input is added to a notification, Android knows that it should request for input from the user.\nThis happens the same way to request for voice input on Android wear 2.0.\n\nHow to implement?\n\nImplementation happens in 3 major steps:\n\n1. Add direct reply action\nFirst thing to do, is to add a direct reply action to your notification. To do this, you need to first:\n\na. Build your label using a remote input.\n\nThe remote input holds information such as the label to show for the action, and the key\nthat will be used to retrieve the user input later on.\n\nb. Build your notification action\nNext step is to build the notification action. This uses the NotificationCompat.Action for\nbackwards compatibility. You will then connect the action with the remote input.\n\nThis requires:\n\n An icon,\n A label (which we built in the previous step) and,\n A PendingIntent.\n\nThe pending intent is what describes the action that will be taken when the notification action\nis clicked. More notes on selecting the pending intent in the next section.\n\nc. Select the appropriate PendingIntent\nThe usual next step is to select the appropriate PendingIntent. Now, this is important,\nbecause, the best practices regarding the PendingIntent depends on the OS version of the user.\n\nDirect reply is new in Android N, and Android already handles the UI, so the right\npending intent here will ideally be a Service/IntentService (for a long running background task) or\na BroadcastReceiver, which runs on the UI thread. It also works without unlocking, making the process really fluid for the user.\n\nHowever, for Android devices running Marshmallow and below (API level 23 and below), it will be more appropriate to use an activity. Since you have to provide your own UI.\n\nCreating a PendingIntent will look like:\n\nd. Build your notification\nNext is to build notification and create it. Doing this makes use of the reply action we built in step b above.\nSample code to implement this is:\n\n2. Read input from inline message\n\nNow that we've shown the notification, we now want to read what the user has typed in the\ndirect reply remote input. Remember, we had to set a key when we created the remote input in step a above, now is the time to use it. It's really easy to do that.\n\nIn our broadcast receiver (or Service, if you chose that) we simply add something like:\n\nYou can then use that method in the onReceive method of your broadcast receiver.\n\nUpdate\n\nNB: If you happen to be using a broadcast receiver, ensure that your BroadcastReceiver isn't exported.\nThe reason for this is, if your component (say BroadcastReceiver) is accessible from a 3rd party app,\nthe app could create fake RemoteInput results and create PendingIntents without any user interaction. This could lead to major security issues.\n\nIf you're using a BroadcastReceiver, one way to make sure it's only accessible within your app, is to set\nexportable to false\n\nYour BroadcastReceiver will look like this in your AndoridManifest.xml file:\n\nThanks Cketti for suggesting the update\n\n3. Update your notification.\nYou don't want your remote input to keep spinning forever, so you should update the notification.\nIt's good practice, to update the notification, instead of outright dismissal. Dismissal leaves the user\nwondering whether or not the action was successful or not.\n\nTo do that, you need the notification id to be the same as the one currently showing.\n\nSample code:\n\nResults?\n\nThe result of this is something like what we have below.\n\n<p align=\"center\">\n <img src=\"http://i.imgur.com/epBNyB1.png\">\n</p>\n\nMore code?\n\nIf you want to see more code, I've created a demo project on Github. Check it out here:\nhttps://github.com/segunfamisa/android-nougat-notification. Please remember to star the repo :D. Thanks.\n\nReferences and further reading\n https://developer.android.com/guide/topics/ui/notifiers/notifications.html#direct\n http://android-developers.blogspot.com.ng/2016/06/notifications-in-android-n.html#directreply\n https://www.youtube.com/watch?v=Zg4v9G-lku8&feature=youtu.be\n\nHey, thank you for reading!\nIf you have any comments/suggestions or corrections, feel free to drop a comment below or tweet at me.\n\nPlease share if you found this useful.\n\nThanks.\n\nCheers,\n\nSegun.",
"title": "Improved notifications with Direct reply in Android N"
}