Hide the open on phone action on wearable

安稳与你 提交于 2019-12-04 10:59:00
Maciej Ciemięga

According to the documentation the "Open on phone" action is added automatically when you have any PendingIntent set in setContentIntent(PendingIntent intent) method.

When this notification appears on a handheld device, the user can invoke the PendingIntent specified by the setContentIntent() method by touching the notification. When this notification appears on an Android wearable, the user can swipe the notification to the left to reveal the Open action, which invokes the intent on the handheld device.


I don't think you can "disable" this action (apart from just not specifying the contentIntent, but I guess that this is not a solution for you).

I'm not familiar with your exact situation, but mostly people set contentIntent on a notification to launch some Activity that shows details or to allow user to do some more things (like input, configuration etc). In that case I don't see a need to try to disable this extra action even if you are serving some kind of "lightweight" solution right on your Android Wear device.

But if you really want to get rid of this "Open on phone" action (while still having the contentIntent set on phone) you will need to have separate notification published from the Android Wear device.

You will need to use DataApi to sync your notification state. See more details about DataApi in the documentation:
https://developer.android.com/training/wearables/data-layer/index.html https://developer.android.com/training/wearables/data-layer/data-items.html

Also you can check an example of usage of DataApi in one of my answers.

I ended in creating multiple notifications. Here is an example:

// for the wearable
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setContentTitle("Title")
    .setContentText("Message")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(openIntent)
    .setGroup("MyGroup")
    .setDeleteIntent(magic());
    // since I don't set setGroupSummary(true) this
    // notification will not been display on a mobile
NotificationCompat.WearableExtender extender =
                   new NotificationCompat.WearableExtender();
extender.addAction(new NotificationCompat.Action.Builder(icon, "do something",
                   openIntent).build());
builder.extend(extender);
// fire it

// for the mobile
NotificationCompat.Builder builder = new NotificationCompat.Builder(context)
    .setContentTitle("Title")
    .setContentText("Message")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(openIntent)
    .setLocalOnly(true) // the magic to hide it on the wearable
    .setDeleteIntent(magic());
// fire it

With magic() I create a PendingIntent which invokes a BroadcastReciever which hides the other notifications to keep them in sync.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!