Add Wear Notifications to Android App
1 min read

Add Wear Notifications to Android App

Add Wear Notifications to Android App

TL;DR: Use android.support.v4.app notification mechanism. Steps are:

  • Create a WearableExtender:

    NotificationCompat.WearableExtender wearableExtender =
    new NotificationCompat.WearableExtender()
    .setHintShowBackgroundOnly(true)
    .setHintHideIcon(true)
    .setContentIcon(R.mipmap.ic_launcher);
    
  • Extend the notification mechanism:

    NotificationCompat.Builder mBuilder =
    new NotificationCompat.Builder(this)
    .setContentTitle(title)
    .setStyle(new NotificationCompat.BigTextStyle().bigText(msg))
    .setContentText(msg)
    .setSmallIcon(R.drawable.ic_stat_e)
    .setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
    .extend(wearableExtender);
    
  • Trigger the notification:

    NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, notification);
    

After reading various sources, I got my application to send notification to my watch using the code above. At the beginning, I thought the notifications weren't sent. I thought I needed to enable wear in the manifest (which I did), but... notifications were sent, just the watch was on charging dock and was't doing anything :(.

To make sure things were OK, I've enabled vibration:

Notification notification = mBuilder.build();
notification.defaults |= Notification.DEFAULT_VIBRATE;

NotificationManagerCompat.from(this).notify(NOTIFICATION_ID, notification);

Note: You need <uses-permission android:name="android.permission.VIBRATE"/> in your Manifest.xml file. References are here, here and, if you dig enough, on GitHub.

Now the watch (and the phone) vibrate when the notification is triggered! Cool!

screen - wearable

PS: Never mind the amazon underground swatch :)

HTH,