One line to get WearableExtender, how cool would that be?
WearableExtender wearableExtender = new WearableExtender(statusBarNotification.getNotification());
Ok, so now lets just extract Actions and from them our beloved RemoteInput.
List<Action> actions = wearableExtender.getActions();
for(Action act : actions) {
if(act != null && act.getRemoteInputs() != null) {
RemoteInput[] remoteInputs = act.getRemoteInputs();
}
}
Ok, but what else do we need? We definitely need PendingIntent to send our results back to the app that triggered the notification and it would also be good to keep the Bundle as it might contain some extra values such as
conversationId. Let’s do it!
notificationWear.pendingIntent = statusBarNotification.getNotification().contentIntent;
notificationWear.bundle = statusBarNotification.getNotification().extras;
Now we are ready to send it back! The approach in our sample app, to speed up the development process of the POC, was to pass our data via EventBus to Activity where it would be saved on Stack, and when the user clicks “Respond to last” to just pop the last item and put fake response text into it.
Filling the RemoteInput
Probably the most interesting part is how we can fill an object that normally takes voice input with our fake text. It is not that hard.
RemoteInput[] remoteInputs = new RemoteInput[notificationWear.remoteInputs.size()];
Intent localIntent = new Intent();
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Bundle localBundle = notificationWear.bundle;
int i = 0;
for(RemoteInput remoteIn : notificationWear.remoteInputs){
getDetailsOfNotification(remoteIn);
remoteInputs[i] = remoteIn;
localBundle.putCharSequence(remoteInputs[i].getResultKey(), "Our answer");//This work, apart from Hangouts as probably they need additional parameter (notification_tag?)
i++;
}
RemoteInput.addResultsToIntent(remoteInputs, localIntent, localBundle);
try {
notificationWear.pendingIntent.send(MainActivity.this, 0, localIntent);
} catch (PendingIntent.CanceledException e) {
Log.e(TAG, "replyToLastNotification error: " + e.getLocalizedMessage());
}
Our notificationWear object is a temporary container for all needed data to respond to a particular notification. Then we take each RemoteInput from the saved notification and fill it with our desired text by using the putCharSequence() method on a Bundle that we will pass back. The key here is to use getResultKey() on each RemoteInput as this is where the called app will look for the reply text in the returned Bundle. As a final step we need to populate Intent with RemoteInputs that we just filled with fake reply, to do so let’s use addResultsToIntent(inputs, intent, bundle). Now we are ready to fire PendingIntent with all that data, to trigger it let’s call pendingIntent.send(context, code,intent). This is all, we just responded to a conversation in Messenger, Telegraph, Line or any other app that uses WearableExtender with RemoteInput! This method still lacks support for Hangouts as probably they require to pass some additional parameter — our best guess is Tag of StatusBarNotification as in case of other (working) apps it is null.