Android N Inline Reply Push Notifications

In Android Nougat provides Direct reply options in the notification bar, this makes user to respond instantly.

 

Add Direct Reply From Notification bar

  • Pending intent: Provides the permission to another app to perform operation like its done by your application.
  • Remote input: This is required to show the text box in notification bar & pass the input data to receiver remotely.
  • Action Buttons: Show the buttons in Notification bar & specify action performed on tapping the buttons.
  • Build a pending intent with broadcast receiver, handle response separately using remote input data.
  • setup action item with Remote input object.
  • Pass the actionitem to Notification object & show the notification.
public static final String NOTIFICATION_REPLY = "notification_reply";

public void InLineReplyExample()
{
    //Provide receiver class to handle the response
    PendingIntent detailsPendingIntent = PendingIntent.getBroadcast(
            MainActivity.this,
            0,
            new Intent(MainActivity.this, MyReceiver.class),
            PendingIntent.FLAG_UPDATE_CURRENT
    );

    RemoteInput remoteInput = new RemoteInput.Builder(NOTIFICATION_REPLY)
            .setLabel("Respond to Message")
            .build();
 
    //Setup action item
    NotificationCompat.Action action =
            new NotificationCompat.Action.Builder(android.R.drawable.ic_delete,
                    "Reply Now...", detailsPendingIntent)
                    .addRemoteInput(remoteInput)
                    .build();

    NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(MainActivity.this)
            .setSmallIcon(android.R.drawable.ic_dialog_email)
            .setContentTitle("Customer identification system")
            .setContentText("Please share your Name")
            .setAutoCancel(true)
            .setContentIntent(detailsPendingIntent)
            .addAction(action)
            .addAction(android.R.drawable.ic_menu_compass, "More", detailsPendingIntent)
            .addAction(android.R.drawable.ic_menu_directions, "Help", detailsPendingIntent);

    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    notificationManager.notify(112, mBuilder.build());

}

Reading the Remote input Text and update Notification item

Control comes to Pending intent on hitting send button, it could be activity or broadcast receiver. Read the text from Remote input object and update notification. Specify the Receiver in Manifest file

<receiver
    android:name=".NotificationReceiver"
    android:enabled="true"
    android:exported="false"></receiver>
public class NotificationReceiver extends BroadcastReceiver {
    public NotificationReceiver() {
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {

            CharSequence id = remoteInput.getCharSequence(MainActivity.NOTIFICATION_REPLY);

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(context)
                    .setSmallIcon(android.R.drawable.ic_menu_info_details)
                    .setContentTitle("Thank you --- " + id);

            NotificationManager notificationManager = (NotificationManager) context.
            getSystemService(Context.NOTIFICATION_SERVICE);
            notificationManager.notify(112, mBuilder.build());
        }
    }
}