In this codelab, you'll learn how to extend a basic messaging app to work with Android Auto and Wear.

What you'll learn

What you'll need

How would you rate your experience with building Android apps?

Novice Intermediate Proficient

If you have Git installed, you can simply run the command below. (You can check if Git is installed by typing git --version in the terminal / command line and verify it executes correctly.)

$ git clone https://github.com/googlecodelabs/device-messaging.git

If you do not have Git, you can download the project as a zip file:

Download Zip

First, run the basic messaging app, which simply creates a notification.

  1. Open Android Studo
  2. Select the device-messaging directory from the code folder (File > New > Import Project... > device-messaging).
  3. Enable USB debugging and plug in your Android device.
  4. Select the "base" module.
  5. Click the Android Studio Run button.
  6. After a few seconds, the app will appear with a screen like the one below:

Click on the Start Conversation button and a notification from Alicia appears after 10 seconds. Ignore the message about plugging your phone to Android Auto.

If you run the Desktop Headunit, you won't see the notification, because it needs to be extended in order to show up on the car's head unit. The next step shows how to do this.

Troubleshooting

As you've seen in the previous step, your notification didn't appear the Android Auto head unit. To be seen on Android Auto, a notification needs to be associated with a messaging app and be extended with a CarExtender from support library v4.

Declare the capabilities

In order for Android Auto to recognize your messaging app, you need to declare the capabilities it requires from the car:

  1. Right click in the "res" folder and select File -> New -> Android Resource File

  1. Fill the dialog with the following info:

File name: automotive_app_desc
Resource type: XML
Root element: automotiveApp

  1. Replace the content of the file you just created with the following content. This declares that the application is using the Android Auto Messaging API.

automotive_app_desc.xml

<?xml version="1.0" encoding="utf-8"?>
<automotiveApp>
    <uses name="notification"/>
</automotiveApp>
  1. Edit AndroidManifest.xml
  2. Search for "TODO: Add Auto meta-data tag." and add the below meta-data declaration to reference the file that you just created.

AndroidManifest.xml

<meta-data
   android:name="com.google.android.gms.car.application"
   android:resource="@xml/automotive_app_desc" />

Extend the code

Extend the app to take advantage of these capabilities:

  1. Open MessagingService.java and add the necessary code to create an UnreadConversation object. Paste the following code into the method sendNotificationForConversation(..), within the delimiting comments.

Before:

/// TODO: Add the code to create the UnreadConversation.
/// End create UnreadConversation.

After:

/// TODO: Add the code to create the UnreadConversation.
// Build a RemoteInput for receiving voice input from devices.
RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY).build();

// Create the UnreadConversation and populate it with the participant name,
// read and reply intents.
NotificationCompat.CarExtender.UnreadConversation.Builder unreadConversationBuilder =
       new NotificationCompat.CarExtender.UnreadConversation.Builder(sender)
               .setLatestTimestamp(timestamp)
               .setReadPendingIntent(readPendingIntent)
               .setReplyAction(replyIntent, remoteInput);

// Note: Add messages from oldest to newest to the UnreadConversation.Builder.
// Since we are sending a single message here we simply add the message.
// In a real world application there could be multiple unread messages which
// should be ordered and added from oldest to newest.
unreadConversationBuilder.addMessage(message);
/// End create UnreadConversation
  1. Search for "TODO: Extend the notification with CarExtender." and add the following code to extend the notification with a CarExtender:

Before:

.setContentIntent(readPendingIntent)
/// TODO: Extend the notification with CarExtender.
/// End

After:

/// TODO: Extend the notification with CarExtender.
.extend(new CarExtender()
      .setUnreadConversation(unreadConversationBuilder.build()))
/// End

Now that the app has been updated, click theRun button.

The app looks the same, but now the notification appears on Android Auto. In the next step you will learn how to test it.

What we've covered

Start the Desktop Head Unit, run your app again and click the Start Conversation button.

After 15 seconds the the notification appears on the head unit.

When you click on the notification, Google reads the text out loud and asks if you'd like to reply.

When the app receives your reply, it sends another message, so you can keep having this very productive conversation for as long as you'd like.

Normally it takes two steps to reply to a wear notification: click on the notification, select the action. Wear 2.0 allows you to streamline the user experience by using an extender to add a reply button directly to the notification, as shown below:

  1. Open MessagingService.java and add the necessary code to create an Action.WearableExtender object. Paste the following code in the method sendNotificationForConversation(..), within the delimiting comments.

Before:

/// TODO: Add the code to allow inline reply on Wear 2.0.
/// End in-line action for Wear 2.0

After:

/// TODO: Add the code to allow inline reply on Wear 2.0.
// Wear 2.0 allows for inline actions, which will be used for "reply"
NotificationCompat.Action.WearableExtender inlineActionForWear2 =
       new NotificationCompat.Action.WearableExtender()
               .setHintDisplayActionInline(true)
               .setHintLaunchesActivity(false);
/// End inline action for Wear 2.0.
  1. Search for "TODO: Add better wear support." and add the following code to add the remote input and an extender:

Before:

/// TODO: Add better wear support.

After:

/// TODO: Add better wear support.
.addRemoteInput(remoteInput)
.extend(inlineActionForWear2)

Ensure your phone is connected to a Wear device and then run your application again. Click the Start Conversation button, and after 15 seconds the notification appears.

Wear, in contrast to Auto, allows the use of standard actions for apps, utilizing the same in-line reply action as on the mobile device itself. In addition, by adding the remote input that was created for Auto, voice replies are also supported on Wear.

As before, each reply sent will generate a new notification.

Takeaways

The messaging API is intentionally simple and builds on top of the existing Android Notification API, requiring minimal effort from developers.

We hope you had fun conversations with Alicia and your other new friends!

What we've covered

Next Steps

If you are interested in learning more about Android Devices, take a look at the codelab "Playing music on cars and wearables", which will teach you how to adapt a music player for Android Auto and Android Wear with the same codebase.

Also, take a look at our messaging sample. It is a more comprehensive version of this codelab.

Learn More