34

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.

enter image description here

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?

Poly
  • 1,053
  • 1
  • 11
  • 16

2 Answers2

52

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.

corazza
  • 31,222
  • 37
  • 115
  • 186
Peter Friese
  • 6,709
  • 31
  • 43
  • 1
    It works, great! Also, I checked google camera app which also open a remote shutter activity from notification, it seems like they use the same way.Even if it works great, I want to keep this question open for another week or so, because I am wondering if there is more straightforward way. Thanks anyway. – Poly Jul 12 '14 at 07:04
  • Hey Peter - i was not able to get your Github sample project to work - not on an LG G Watch. I forked it and added some logging and a Toast - the Toast shows up, but neither the Activity nor the Notification show up. – RoundSparrow hilltx Jul 13 '14 at 17:28
  • 1
    @RoundSparrowhilltx sorry to hear this. Which firmware does your G watch run? Unfortunately, I'm travelling next week, so I'm nowhere near my G watch, but I'll check later. – Peter Friese Jul 13 '14 at 17:31
  • @PeterFriese - never mind! I just rebooted the watch and it's working fine. My watch is running stock firmware (purchased retail). Not sure why it wasn't working - but it is now. Thanks for the code. You are welcome to take the changes I made - which make for a slightly better code sample with extra logging. Traveling without your watch ;) Thanks again. – RoundSparrow hilltx Jul 13 '14 at 17:37
  • @RoundSparrowhilltx I'm travelling with my Samsung :-) Feel free to send a pull request! – Peter Friese Jul 13 '14 at 19:15
  • Hey guys, what phone are you using to deploy to your watch? I'm getting errors and wondering if its because my phone is an older version. – Erik B Jul 14 '14 at 22:06
  • Do you know if it is possible to do this from a Push Notification? The sample project has a button triggering the request. I'm trying to do this from a Push Notification Intent but am having trouble. More info here: http://stackoverflow.com/questions/24877141/using-googleapiclient-from-push-notification-intentservice-to-send-data-to-andro – TWilly Jul 25 '14 at 18:01
  • @PeterFriese, I'm on a Gear Live, your sample project didn't work for me. I've also tried rebooting, disconnecting, reconnecting. Still no luck. The onDataChanged method is never called. Any idea? Tks – EyeQ Tech Aug 05 '14 at 14:00
  • @TungMaiLe it seems to depend on the order in which you install / uninstall / reinstall the wearable / mobile apps. I'm digging deeper into this and will let you know when I've got a solution / better answer. – Peter Friese Aug 07 '14 at 21:43
  • How do you actually *run* the service? I can run MyActivity directly from Android Studio, but the services seems not to start with it, and the changes don't get registered on the wear device. – corazza Aug 08 '14 at 14:39
  • @TungMaiLe - I had another look at the code and the root of the issue was that the data items sent from the phone were not unique. This resulted in them not being updated on the watch (because the system will only synchronise changes). I [updated the code](https://github.com/peterfriese/AndroidWear/commit/55a6f0fc105f1eea721c7059ddf3170bf3b74379#diff-d41d8cd98f00b204e9800998ecf8427e) - it should work fine now. – Peter Friese Aug 08 '14 at 22:43
  • @jco - see my comment to TungMaiLe - there was a flaw in my code. Check out the [latest revision of the code](https://github.com/peterfriese/AndroidWear/commit/55a6f0fc105f1eea721c7059ddf3170bf3b74379#diff-d41d8cd98f00b204e9800998ecf8427e), it should work fine now. The service will be started as soon as the system synchronises any data item. – Peter Friese Aug 08 '14 at 22:46
  • @PeterFriese OK, thank you, I will get back at you on Monday. Just one more question - how am I supposed to actually get the code to the Wear device? When I have an activity - I just "Run" it, and it gets ran on the Wear device - but no such option is available for a Service. – corazza Aug 09 '14 at 10:54
  • It works flawlessly now! Thank you. I'm still unsure of the correct way of running it, but at the moment I just run the activity which is supposed to be started by the notification, and it seems to upload the service along with itself, as well. – corazza Aug 11 '14 at 08:05
  • @PeterFriese Actually, the `dismissNotification` method is never getting called, even when I swipe the notification away. I logged a string in it, and I never see it in the Logcat console - but I see other logs. – corazza Aug 11 '14 at 10:19
  • @jco - regarding how to run services on your watch: when in development mode, you need to deploy your wearable app manually to the watch (using Android Studio or the command line). For production, the build script will embed the wearable app inside your mobile app. When a user installs your app, it will be automatically installed on the watch. See [Packaging Wearable Apps](https://developer.android.com/training/wearables/apps/packaging.html) for more info. Otherwise, feel free to post a new question :-) – Peter Friese Aug 11 '14 at 11:12
  • @jco - regarding the `dismissNotification` call - have you tried setting a breakpoint? For me, this works. Maybe your log call is filtered... – Peter Friese Aug 11 '14 at 11:14
  • @PeterFriese For example, I also don't see anything logged from `onStartCommand` (just a single "started"). I haven't tried with breakpoints, I will. – corazza Aug 11 '14 at 11:16
  • Additionally, what is `notificationId` for? I log it in `sendNotification` - it usually increments, but resets itself after a pause of about half a minute. Does that mean Android restarted the service? – corazza Aug 11 '14 at 11:17
  • Does anyone know of an alternative to creating the notification on the wearable? I'd prefer not to have to deal with the post syncing of dismiss status back to the phone. The only way I can think of it would be to send a messages to the phone and then send a message back to wearable via a service to then have the wearble start the activity, which seems convoluted. My question here.. http://stackoverflow.com/questions/25167859/android-wear-start-wear-activity-from-handheld-action – TWilly Aug 11 '14 at 15:51
  • I was wondering why you use a DataItem and not the MessageAPI? The MessageAPI is described in the documentation as "... is good for .. starting an intent on the wearable from the handheld" and therefore seems more appropriate or straightforward rather than using a DataItem? – Gruntcakes Jan 19 '15 at 17:50
  • I follow your code but I wasn't able to make it work... I don't see any "startService" calls, so.. how come this is working for you? – frankelot Sep 22 '15 at 20:58
  • Hi, It didn't work for me. Do I need to generate signed apk everytime? – Krupal Shah Sep 23 '15 at 10:48
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.

Graydyn Young
  • 5,041
  • 1
  • 17
  • 19
  • But what are the limitations of this? Surely you can't have your activity running on the phone and updating on the Wear device? – corazza Aug 08 '14 at 15:07
  • This way doesn't put your app into the notification stream which is a key feature of using notifications. – TWilly Aug 11 '14 at 15:35
  • The activity will run on the wearable device, inside of a notification. You can have the activity running inside of a notification, as described in the link above. And yes, it does put the app in the notification stream. – Graydyn Young Aug 17 '14 at 02:46
  • I'm playing with that currently you can do in your activty what you want, except swiping right or left in a recyclerview. – rekire Nov 14 '14 at 08:49