Android Nougat Reply Notification Demo

Starting in Android 7.0 (API level 24) You have a lot of control, and you’re getting even more. Google has also done some cleanup to make all of those alerts look more refined and add in some color in the right places to make it easier to quickly see the most important information.

From the lock screen, you’ll notice the subtle aesthetic differences, but just as before you can swipe the away or tug down slightly to launch the Quick Reply. To type, you’ll need to unlock the device with the fingerprint reader, PIN, passcode, or pattern.

device-2016-09-06-200742
Notification on lock screen

With the new notification type introduce in Android 7.0 (Nougat) users can respond directly to text messages or update task lists from within the notification dialog. On a handheld, the inline reply action appears as an additional button displayed in the notification. When a user replies via keyboard, the system attaches the text response to the intent you had specified for the notification action and sends the intent to your handheld app.

  • Create an instance of RemoteInput.Builder that you can add to your notification action.

RemoteInput remoteInput = new RemoteInput.Builder(Constants.REPLY_KEY)
 .setLabel(replyLabel)
 .build();

  •  Attach the RemoteInput
Notification.Action action = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
    action = new Notification.Action.Builder(Icon.createWithResource(this, R.drawable.ic_reply),
                    getString(R.string.reply), contentIntent)
                    .addRemoteInput(remoteInput)
                    .build();
}else{
    action = new Notification.Action.Builder(R.drawable.ic_reply,
            getString(R.string.reply), contentIntent)
            .addRemoteInput(remoteInput)
            .build();
}
  • Apply the action to a notification
Notification newMessageNotification =
        new Notification.Builder(mContext)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentTitle(getString(R.string.username))
                .setContentText(getString(R.string.content))
                .addAction(action).build();
NotificationManager notificationManager =(NotificationManager) mContext.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Constants.NOTIFICATION_ID, newMessageNotification);

Above codes need while sending notification. While you need to read reply as well.

Read text written on REPLY.

  • Query the bundle using the result key
private CharSequence getReplyMessage(Intent intent) {
    Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
    if (remoteInput != null) {
        return remoteInput.getCharSequence(Constants.REPLY_KEY);
    }
    return null;
}
  • Give another notification, using the same notification ID that you provided for the previous notification. The progress indicator disappears from the notification interface to inform users of a successful reply. When working with this new notification.
Notification repliedNotification =
        new Notification.Builder(context)
                .setSmallIcon(R.mipmap.ic_launcher)
                .setContentText("Message sent")
                .build();
notificationManager.notify(Constants.NOTIFICATION_ID, repliedNotification);

Get full example source code on Github

Thank you for reading.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s