How can I specify that an Notification action is not shown on Android Wear?

android, wear-os

Solution

Whenever you use `addAction()` in `NotificationCompat.WearableExtender`, you are not actually extending the actions (despite the name), but rather separating them into two lists, one for the phone, and one for the wearable.

- The actions added on the original `NotificationCompat.Builder` are shown on the handheld.

- The actions added on the WearableExtender are shown on the Android Wear device.

See Specifying Wearable-only Actions:

If you want the actions available on the wearable to be different from those on the handheld, then use `WearableExtender.addAction()`. Once you add an action with this method, the wearable does not display any other actions added with `NotificationCompat.Builder.addAction()`. That is, only the actions added with `WearableExtender.addAction()` appear on the wearable and they do not appear on the handheld.

Therefore, to have handheld-only actions, add them before creating the extender. And To have wearable-only actions, add them on the extender. If you use an extender and you want to have repeated actions in both devices, you must add them in both (though maybe there is an option to copy them?).

For example:

NotificationCompat.Builder notificationBuilder =
        new NotificationCompat.Builder(this)
                .setSmallIcon(R.drawable.ic_launcher)
                .setContentTitle("The title")
                .setContentText("This is the text")
                .setContentIntent(pendingIntent);

// Handheld-only actions.
notificationBuilder.addAction(drawable1, "In Both", pendingIntent);
notificationBuilder.addAction(drawable2, "Only in phone", pendingIntent);

// Wearable-only actions.
NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender();
wearableExtender.addAction(new NotificationCompat.Action.Builder(drawable2, "In Both", pendingIntent).build());
wearableExtender.addAction(new NotificationCompat.Action.Builder(drawable3, "Only in wearable", pendingIntent).build());
notificationBuilder.extend(wearableExtender);

// Build and show notification.
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(notificationId, notificationBuilder.build());

Also

- If you create a `WearableExtender` but don't add any actions into it, then the actions from the original notification are used.

- The "content intent" from the handheld seems to always be present on the watch, with the "Open on Phone" text. I haven't found a way to disable this for the watch only.

Problem

If I create an Notification I can add three Actions. Each action can also be invoked on a watch. Is it possible to make some of this actions not available on an Android Wear Watch?

Original source