651.288.7000 info@intertech.com

So far in this Android Wear developer tutorial series, we have covered using bridged notifications and creating a standalone app that runs on Android Wear.  In some situations, however, we may want a notification that is shown only on the wearable device.  This may be the case when the notification is based on information that is known only by the wearable (for example, when monitoring the user’s physical activity).  Or, maybe we want to create a notification that uses a fully customized layout.  This is not possible with bridged notifications, but it can be done if the notification originates from the wearable device.

In this Android Wear developer tutorial, I am going to update our Time Entry Android app to create a wearable notification that uses a fully customized layout.  Here’s how it will work: I am going to add a configurable setting to enable a reminder at the end of each day, to remind the user to enter time.  This notification will only be for wearable devices.  There will also be a second page that displays the time entered so far, which will use a customized layout.

Because this notification will be created on the wearable device, you must have a standalone app that runs on Android Wear.

Triggering the Notification

To enable the daily notification, I am going add a user configurable preference to the handheld app.  I am doing it this way for a couple of reasons.  First, I already have a fragment that manages user preferences, so I can just add another setting there.  Second, and more generally, configuring preferences on a handheld device is usually a better user experience than on a wearable device.  In addition to simply allowing the user to select yes or no to receive the daily reminder, I may also want to allow them to select the time of day, and whether or not to include weekends.  Looking at it from a user’s perspective, I think that would be easier to do on a larger screen.

For now, when the preference is enabled, I am going to schedule an alarm to trigger every day at 4:30 pm.  This will start an IntentService that will send a message to the wearable device, instructing it to create the notification.  Since this notification is only for wearables, I don’t want to use a bridged notification.  That’s why I will use the wearable data layer to send a message.  When the message is received, the wearable app will create the notification.

Here is the code to schedule the alarm:

We create a pending intent for DailyAlarmIntentService, which is a class we will write that sends a message to the wearable to trigger the notification.

An important thing to note about AlarmManager is that the alarms will not persist when the device is rebooted.  To get around this, we need to create a receiver that will be notified when the device boots, and then reschedule the alarm if the user preference is enabled.  Here is the code for that receiver:

In order for the receiver to receive the broadcast, it needs to be declared in the manifest:

We also need to declare that we use the permission for boot completed:

Next we need to implement DailyAlarmIntentService, which is an IntentService that is invoked by our alarm.  All this needs to do is send a message to the wearable, notifying it to create the daily reminder notification.  Sending and receiving messages has been covered in previous posts, so I won’t repeat that here.

Creating the Wearable Only Notification

When the message is received by the wearable, it can go ahead and create the notification.  Wearable notifications are created in the same way that they are created on handheld devices.  However, since they are created on a wearable device, they will not be bridged to other devices.

Also, we can now take advantage of wearable features that are not available using bridged notifications.  This includes using a fully customized layout.  In addition to the main notification, I am going add a second page with a custom view:

First Page

First Page

Second Page

Second Page

You may notice that the second page looks a lot like the daily summary that I used in the standalone app.  In fact, I am using the exact same Fragment.  After all, one of the advantages of Fragments is that you have a reusable portion of the UI.

In order to use a custom layout in a notification, you need to provide an Activity, which is wrapped in a PendingIntent and provided to the WearableExtender.  Android Wear will then use that Activity to display the notification.  In this case, the Activity simply hosts the daily summary fragment that I am already using the standalone app.  Here is the code to do this:

In order for the Activity to display full screen, you need to use WearableExtender.SIZE_FULL_SCREEN.  Without this, Android will try to fit your Activity inside of a default card.

The final piece is to create DailySummaryActivity.  As already mentioned, this Activity simply hosts an existing Fragment:

A few things are worth noting:

  • This Activity extends DataLayerAwareActivity, which is described in part 3 of this tutorial.
  • When using an Activity to display a custom layout in a notification, you have the full capability of the Activity.  In this case that includes binding to a service and communicating with the data layer, which are done in the DataLayerAwareActivity.

Wrap Up

In addition to bridged notifications (which are created by the handheld app), you can also create notifications from the wearable app.  This enables you to take full advantage of Android Wear features.  Creating notifications on a wearable device is not much different than on a handheld device.

If you’re just finding this Android Wear Developer tutorial now, here are a couple links to the rest of the posts in the series:

Android Wear Developer Tutorial (Part 1) – Introduction and Overview

Android Wear Developer Tutorial (Part 2) – Bridged Notifications

Android Wear Developer Tutorial (Part 3) – Creating a Standalone App

Android Wear Developer Tutorial (Part 5) – Tips and Tricks

 

Like What You've Read?

Subscribe to the Blog.

Every Friday we send that week's content from our Developers via email. Try it out!

Some ad blockers can block the form below.

You have Successfully Subscribed!

Pin It on Pinterest

Share This