Customizing wearable notifications

Notifications displayed on the wearable offer a hands-free experience for using apps; however, it is often desirable to provide the user with a wearable-specific experience.

How to do it...

Notifications from a handheld device can be easily displayed on wearables, but we can also provide special features for notifications that will only appear on a wearable:

  1. We can customize the appearance of notifications using extenders, such as NotificationCompat.WearableExtender for wearable devices:
    var background = BitmapFactory.DecodeResource(
      Resources, Resource.Drawable.notificationBackground);
    var extender = new NotificationCompat.WearableExtender()
      .SetHintHideIcon(true)
      .SetBackground(background);
  2. We can also use extenders to provide specific action buttons for the wearable, instead of just using the actions from a handheld:
    var icon = Android.Resource.Drawable.IcDialogDialer;
    var action = new NotificationCompat.Action(
      icon, "Make Call", pendingIntent);
    extender.AddAction(action);
  3. To apply the customizations of the extenders, we will attach the extender using the Extend() method:
    var notification = new NotificationCompat.Builder(this)
      ...
      .Extend(extender)
      .Build();
  4. Then, we can show the notification normally, and the extender will render the additional content on the wearable:
    var manager = NotificationManagerCompat.From(this);
    manager.Notify(NotificationId, notification);
  5. Once we have created the notification, we can read the extended values through the same extender type:
    var extender = 
      new NotificationCompat.WearableExtender(notification);
    var isIconHidden = extender.HintHideIcon;

How it works...

Android automatically synchronizes notifications to the wearable, which is often enough. However, we can provide an improved experience for the wearable using the NotificationCompat.WearableExtender type.

Tip

The WearableExtender type can be used to provide wearable-specific features and functionality to synchronized notifications.

This type provides the means to override and customize the default notification functionality and appearance on the wearable device. Usually, the notification has an app icon in the corner of the notification, but we may wish to use a different look for our app.

If we create an instance of the WearableExtender type, we can use it to modify the notification. We will use the SetHintHideIcon() method to let the wearable know not to render the icon. Then, we set a background behind the content card using the SetBackground() method. In addition to removing the icon, we can also change its position or use an icon that is different from the app icon.

The bitmap that we use with the SetBackground() method should have a resolution of 400 × 400 for non-scrolling backgrounds and 640 × 400 for scrolling backgrounds. These images should be placed in the drawable-nodpi resource folder. Other image resources for wearable notifications, such as icons, should be placed in the drawable-hdpi resource folder.

Tip

When using the WearableExtender type, a custom set of actions can be provided for the wearable.

Another method to override functionality is to provide a different set of actions for the wearable. When not providing actions via the extender, all the actions on the notification are available on the wearable. We may want to provide a more useful set of actions designed for a hands-free device. One such improvement would be to provide a reply by typing a response on the handheld; but on the wearable, we would provide a reply by a voice input.

To provide custom actions on a wearable, we can create the action using the NotificationCompat.Action type. Then, instead of adding the action to the notification, we add the action to the wearable extender using the AddAction() method.

Note

If the actions are added to the WearableExtender type, they replace all actions for the wearable, resulting in a different set of actions for each device.

There's more...

Notifications using RemoteViews are stripped of custom layouts, and the wearable will only display the text and icons. However, we can create the appearance custom notifications by creating an app that runs on the wearable device.

If the standard notification styles don't provide the required features or appearance, we can display a custom activity with a custom layout. Then, we can request for the wearable to issue a notification and set the activity as the display intent.

Note

Notifications displayed from the wearable are not synchronized to the handheld, but notifications on the handheld are synchronized to the wearable.

We can use normal mechanisms to display a notification from a wearable because the wearable is actually just a smaller Android device. Displaying a notification from the wearable is the same as displaying a notification on the handheld.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.216.174