Notification.Action with multiple RemoteInputs - why and how?

1.2k views Asked by At

TL;DR version: I've noticed that an action-able notification in Android can be assigned with multiple instances of RemoteInput - but couldn't find a scenario in which this is useful, and not even sure how to properly use this potential feature.

Using a NotificationListenerService (like in this example app) I've noticed that when launching a notification's action/intent I can theoretically choose which remote input to populate. So I wondered how the Android UI's default behavior would be like.

I've used this demo app with a mix from another thread, in order to generate a notification whose action is assigned with two different instances of RemoteInput:

public void showRemoteInputNotification(Context context) {

    //Yes intent
    Intent yesReceive = new Intent();
    yesReceive.setAction(KEY_TEXT_REPLY);
    Bundle yesBundle = new Bundle();
    yesBundle.putInt("userAnswer", 1);
    yesReceive.putExtras(yesBundle);
    PendingIntent pendingIntentYes = PendingIntent.getBroadcast(context, 12345, yesReceive, PendingIntent.FLAG_UPDATE_CURRENT);
    //mBuilder.addAction(R.drawable.calendar_v, "Yes", pendingIntentYes);

    RemoteInput remoteInput = new RemoteInput.Builder(KEY_TEXT_REPLY)
            .setLabel("TEST")
            .build();

    RemoteInput remoteInput2 = new RemoteInput.Builder(KEY_TEXT_REPLY2)
            .setLabel("TEST2")
            .build();

    PendingIntent replyIntent = PendingIntent.getActivity(context,
            REPLY_INTENT_ID,
            getMessageReplyIntent(LABEL_REPLY),
            PendingIntent.FLAG_UPDATE_CURRENT);

    PendingIntent archiveIntent = PendingIntent.getActivity(context,
            ARCHIVE_INTENT_ID,
            getMessageReplyIntent(LABEL_ARCHIVE),
            PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Action replyAction =
            new NotificationCompat.Action.Builder(android.R.drawable.sym_def_app_icon,
                    LABEL_REPLY, pendingIntentYes)//replyIntent)
                    .addRemoteInput(remoteInput)
                    .addRemoteInput(remoteInput2)
                    .build();

    NotificationCompat.Action archiveAction =
            new NotificationCompat.Action.Builder(android.R.drawable.sym_def_app_icon,
                    LABEL_ARCHIVE, archiveIntent)
                    .build();

    NotificationCompat.Builder builder =
            createNotificationBuider(context, "Remote input", "Try typing some text!");
    builder.addAction(replyAction);
    builder.addAction(archiveAction);
    //builder.addAction(android.R.drawable.ic_menu_send, "Yes", pendingIntentYes);

    showNotification(context, builder.build(), REMOTE_INPUT_ID);
}

Then, the UI (notifications top-bar scroll-down) would still look the same (same amount of action buttons). So I thought that perhaps when the user executes the notification's action via the system notifications UI, both remote inputs will be used? So I checked that in the BroadcastReceiver class I've added, to the same app which generates that notification in the first place:

public class NotificationReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Set<String> keys1 = intent.getExtras().keySet();
        Set<String> keys2 = RemoteInput.getResultsFromIntent(intent).keySet();
    }
}

and it seems that only the last RemoteInput to be added to the Action is being used.

So basically I was wondering if there's a better way to use or demonstrate this kind of scenario - or perhaps it's a bad practice to begin with? Did I mix between different API parts that shouldn't be used in this way?

EDIT: Also tried with this example by Google, only difference (as far as I understand) is that this one is using Wearable-type Action/RemoteInput. Inside its generateBigPictureStyleNotification function, I've added/changed the following lines:

RemoteInput remoteInput2 =
                new RemoteInput.Builder(BigPictureSocialIntentService.EXTRA_COMMENT2)
                        .setLabel("TEST")
                        // List of quick response choices for any wearables paired with the phone
                        .setChoices(bigPictureStyleSocialAppData.getPossiblePostResponses())
                        .build();
[...]
NotificationCompat.Action replyAction =
            new NotificationCompat.Action.Builder(
                    R.drawable.ic_reply_white_18dp,
                    replyLabel,
                    replyActionPendingIntent)
                    .addRemoteInput(remoteInput)
                    .addRemoteInput(remoteInput2)
                    .build();

But then, just like before, when I tried to reply from Android's notifications UI, it seems to have populated only the last RemoteInput key (checked in BigPictureSocialIntentService's getMessage()).

0

There are 0 answers