Enhanced and Wearable-Ready Notifications on Android

Introduction

Notifications are a very useful way to interact with your application's users and, with Android Wear, we now also have wearable devices running Android. It's therefore a good idea to learn how to take advantage of these new features by adding appropriate actions to notifications or by creating actions that are only visible on wearable devices.

In this tutorial, I'm going to show you the modern implementation of notifications, as shown during this year's Google I/O. We'll use the new support package and extend its capabilities by adding actions that are only visible on smartwatches, the only wearable devices available with Android Wear at the time of writing.

1. Prerequisites

For this project, you can use either Android Studio or the Android Developer Tools. If you're using Android Studio, then make sure to add the following line to your build.gradle file.

2. Setting Up the Project

Launch your IDE and create a new Android project, or open a project you've created previously. For this tutorial, I'm going to create a new project and name it ImprovedNotifications. Don't forget to use a unique package name.

While setting up the project, make sure that you select the Empty Activity option in the Create Activity step.

Once the project is created, create a new Activity, ActivatedActivity. This Activity is going to be called from a notification on your mobile or wearable device.

Before we move on, we need to update the strings.xml file by adding the strings that we're going to be using a bit later in this tutorial.

3. Creating the Layout

The next step is to create a layout for the MainActivity and the ActivatedActivity classes. The layout for the MainActivity class is shown below.

And this is the layout for the ActivatedActivity class.

4. Creating a Notification

We create a notification in the MainActivity class. In the code snippet below, you can see what steps are involved in creating a notification. I've commented the code block to help you understand the various steps, but let's walk through the snippet step by step.

Step 1

We first instantiate the button that we'll use to launch the notification. You could also create the notification directly in the onCreate method, but by using a button you have more control over the exact timing of the notification.

Step 2

In the second step, we instantiate an Intent object with the task to perform when the notification is tapped. We pass the object to a PendingIntent instance to handle it later when it's called.

Step 3

Using the Android Support Library, we create the notification using the Builder class of the NotificationCompat object and set its attributes.

Step 4

In this step, we instantiate a NotificationManagerCompat instance to start and/or stop the notification anytime we want. This will make testing much easier.

Step 5

When the button is tapped, the notification is fired using the notify method.

Don't forget to use the classes from the Android Support Library. This way you can be sure your notification is going to look fine on older versions of Android.

You can now run the app, tap the button, and see the notification appear at the top of the screen. If you tap the notification, it should take you to the ActivatedActivity activity. With the notification set up and working, it's time to start adding actions to it.

5. Adding Actions to the Notification

You can add extra actions to the notification by invoking the addAction method on the notification_builder object. For this to work, you need to pass a PendingIntent instance with the task you like to perform.

In the following code snippet, I show you the steps you have to implement to create an action with a custom task. In this example, I'm going to take you to my Twitter profile in the Twitter app. This means I need a URI instance pointing to my Twitter profile, add this to the Intent, and let the PendingIntent handle it when the action is tapped. Insert this code block before the instantiation of the notification_builder object.

To add the action, invoke the addAction method on the notification_builder object and pass in the open_twitter_profile object we just created.

Run the application, tap the button to trigger the notification, and you should see the notification appear along with the action we just created.

While you can add more actions to a notification using the addAction method, make sure the user isn't overwhelmed by the number of actions they can choose from.

6. Supporting Android Wear

So far, we have used the classes from the Android Support Library to make sure the notifications are also shown on smartwatches running Android Wear. You can run the application on a physical smartwatch or you can try it on the emulator from the Android Virtual Device Manager. Either way, you need to sync your device with the smartwatch.

Before syncing your device with the smartwatch emulator, you need to install the Android Wear app, which is available on Google Play. After you've unplugged every other Android device connected to your computer, execute the following command from the command line.

adb devices

This command lists the devices connected to your development machine. You should see two of them, the smartwatch emulator and your device. Then run the following command from the command line to enable port forwarding.

adb -d forward tcp:5601 tcp:5601

You can now connect your device with the emulator and turn on the notifications using the Android Wear app. Run the app again and trigger the notification. The notification should look similar to the one shown below.

7. Adding Wearable-Only Actions

It is possible to add actions that are only visible on wearables. This is accomplished by invoking the addAction method of the WearableExtender class. The result is that any actions added through the NotificationCompat.Builder class are ignored.

As we did before, to trigger the action, we make use of an Intent and a PendingIntent instance, but we'll create the action displayed on the wearable device using the Builder class of a special Action class, which is part of the NotificationCompat class as shown below.

We then add this action to the notification_builder object using the extend method as shown below.

Run the app and tap the button to display the notification on your device. It should be different than the notification that pops up on the wearable emulator.

Conclusion

Smartwatches are here to stay, for a while at least, and it's therefore important to take advantage of this new way of communicating with your application's users. I hope you've found the tutorial helpful and don't forget to share it if you liked it.


Tags:

Comments

Related Articles