Introducing wearable notifications

Wearable devices are very useful for apps as they allow users to read notifications without having to reach for a handheld.

Getting ready

To make use of notifications on a wearable, we need to have the Android Support Library v4 Component or the Xamarin.Android.Support.v4 NuGet installed on our handheld project.

How to do it...

We can make notifications automatically appear on a wearable if we manage our app's notifications using the NotificationCompat type:

  1. To display typical notifications on a wearable, we will build and display them using the NotificationCompat and NotificationManagerCompat types:
    var notification = new NotificationCompat.Builder(this)
      .SetSmallIcon(Android.Resource.Drawable.StatSysDownload)
      .SetContentTitle("Wearable Notifications")
      .SetContentText("This is a notification from a device.")
      .SetContentIntent(pendingIntent)
      .Build();
    var manager = NotificationManagerCompat.From(this);
    manager.Notify(NotificationId, notification);
  2. If we have specified a style for the notification, the wearable notification will also adhere to this style:
    var expandedStyle = new NotificationCompat.BigTextStyle();
    expandedStyle.BigText("This is the extended text...");
    var notification = new NotificationCompat.Builder(this)
      ...
      .SetStyle(expandedStyle)
      .Build();
  3. If we want to display buttons on the notification on both handheld and wearable, then we will use the SetAction() method of the notification builder:
    var action = new NotificationCompat.Action.Builder(
        iconResource, "Open Maps", pendingIntent)
      .Build();
    var notification = new NotificationCompat.Builder(this)
      ...
      .AddAction(action)
      .Build();

How it works...

When an Android wearable device is connected to a handheld device, the handheld device automatically synchronizes notifications to this wearable. This allows users to read notifications without having to reach for the handheld. This hands-free ability is what makes this wearable device such a great convenience. Although not as powerful as a handheld, the wearable can often provide an overall improved user experience.

To provide notifications on the wearable, no extra code or configuration is required. All that needs to be done is to show the notification using the NotificationCompat type. This type() functions exactly similar to the Notification type and supports all the Android versions from Android 2.0.

To show a notification, we will create it using the NotificationCompat.Builder type. Once we have set all the properties, we will build it using the Build() method. Then, to show the built notification, we will pass it to the Notify()method on the NotificationManagerCompat type.

In addition to displaying notifications, we can customize their appearance using various styles and actions. Each component of the notification is rendered differently and more appropriately on each type of device. Whether it is the handheld or wearable, the notification will be displayed in an easy-to-consume manner.

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

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