How to send notification from handheld to wear to

2019-01-30 04:52发布

问题:

I'm wondering if it is possible to send notification from handheld (android phone) to wear device to open Activity on wear device? What I want to do is as following.

So far, I checked the following documents, but it's different from what I want to do.

  • Adding Wearable Features to Notifications
    • What is described here is sending notification from phone to wear, then open activity on the phone (I want to open activity on the wear)
  • Create Custom Notifications
    • What is described here is sending notification from wear to wear, then open activity on the wear (I want to send notification from phone to wear)

Any ideas?

回答1:

The pattern to use for this is:

  1. Create a DataItem on the mobile. It will be synced to the connected wearable automatically.
  2. On the wearable, implement a WearableListenerService and listen for onDataChanged events.
  3. When you receive a DataItem, create a notification (with the data sent in the DataItem) and send it locally (i.e. on the wearable). Use setContentIntent() on the notification to specify a pending intent that will launch your wearable activity.
  4. Don't forget to also provide an intent that is fired when the user dismisses the notification on the wearable, so that the DataItem can be removed. Otherwise, you will not receive any update events.

I've created a sample project that shows all of this in action.

Check out this question if the onDataChanged method is not getting called.



回答2:

I think in most cases it would be better to include your app activity inside the notification. For example, instead of the "Open" button in your notification, you could use setDisplayIntent(notificationPendingIntent) to display an activity as part of the notification as described here: http://developer.android.com/training/wearables/apps/layouts.html

This gives you a best of both worlds situation between having an app and a notification.