Build messaging apps for Android Auto

Staying connected through messages is important to many drivers. Chat apps can let users know if a child needs to be picked up, or if a dinner location has been changed. The Android framework enables messaging apps to extend their services into the driving experience using a standard user interface that lets drivers keep their eyes on the road.

Apps that support messaging can extend their messaging notifications to allow Android Auto to consume them when Auto is running. These notifications are displayed in Auto and allow users to read and respond to messages in a consistent, low distraction, interface. Additionally, when using the MessagingStyle API, you get the benefit of optimized message notifications for all Android devices, including Android Auto. Such optimizations include UI that's specialized for message notifications, improved animations, and support for inline images.

This lesson assumes that you have built an app that displays messages to the user and receives the user's replies, such as a chat app. The lesson shows you how to extend your app to hand those messages off to an Auto device for display and replies.

Get started

To enable your app to provide messaging service for Auto devices, your app must be able to do the following:

  1. Build and send NotificationCompat.MessagingStyle objects that contain reply and mark-as-read Action objects.
  2. Handle replying and marking a conversation as read with a Service.
  3. Configure your manifest to indicate the app supports Android Auto.

Concepts and objects

Before you start designing your app, it's helpful to understand how Android Auto handles messaging.

An individual chunk of communication is called a message and is represented by the class MessagingStyle.Message. A message contains a sender, the message content, and the time the message was sent.

Communication between users is called a conversation and is represented by a MessagingStyle object. A conversation (or MessagingStyle) contains a title, the messages, and whether the conversation is amongst a group (that is, the conversation has more than one other recipient).

To notify users of updates to a conversation (such as a new message), apps post a Notification to the Android system. This Notification uses the MessagingStyle object to display messaging-specific UI in the notification shade. The Android platform also passes this Notification to Android Auto, and the MessagingStyle is extracted and used to post a notification through the car's display.

Apps can also add Action objects to a Notification to allow user to quickly reply to a message or mark-as-read directly from the notification shade. Android Auto requires the mark-as-read and reply Action objects, in order to manage a conversation.

In summary, a single conversation is represented by a single Notification object that is styled with a single MessagingStyle object. MessagingStyle contains all the messages within that conversation with one or more MessagingStyle.Message objects. Finally, to be fully Android Auto compliant, you must attach a reply and mark-as-read Action to the Notification .

Messaging flow

This section describes a typical messaging flow between your app and Android Auto.

  1. Your app receives a message.
  2. Your app then generates a MessagingStyle notification with a reply and mark-as-read Action.
  3. Android Auto receives “new notification” event from Android system and finds MessagingStyle, reply Action, and mark-as-read Action.
  4. Android Auto generates and displays a notification in the car.
  5. If a user taps on the notification via the car's display, Android Auto triggers the mark-as-read Action.
    • In the background, your app must handle this mark-as-read event.
  6. If the user responds to the notification via voice, Android Auto includes a transcription of the user's response into the reply Action and then triggers it.
    • In the background, your app must handle this reply event.

Preliminary assumptions

This page does not guide you on creating an entire messaging app. However, the code sample below includes some of the things your app should already have before you start to support messaging with Android Auto.

data class YourAppConversation(
        val id: Int,
        val title: String,
        val recipients: MutableList<YourAppUser>,
        val icon: Bitmap) {
    companion object {
        /** Fetches [YourAppConversation] by its [id]. */
        fun getById(id: Int): YourAppConversation = // ...
    }

    /** Replies to this conversation with the given [message]. */
    fun reply(message: String) {}

    /** Marks this conversation as read. */
    fun markAsRead() {}

    /** Retrieves all unread messages from this conversation. */
    fun getUnreadMessages(): List<YourAppMessage> { return /* ... */ }
}
data class YourAppUser(val id: Int, val name: String, val icon: Uri)
data class YourAppMessage(
    val id: Int,
    val sender: YourAppUser,
    val body: String,
    val timeReceived: Long)

Declare Android Auto support

When Android Auto receives a notification from a messaging app, it checks that the app has declared support for Android Auto. To enable this support, include the following entry in your app's manifest:

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

This manifest entry refers to another XML file that you should create with the following path: YourAppProject/app/src/main/res/xml/automotive_app_desc.xml, where you declare what Android Auto capabilities your app supports. For example, to include support for notifications, include the following in your automotive_app_desc.xml:

<automotiveApp>
    <uses name="notification" />
</automotiveApp>

If your app requires support for handling SMS, MMS, and RCS, you must also include the following:

<automotiveApp>
    ...
    <uses name="sms" />
</automotiveApp>

Import the AndroidX core library

Building notifications for use with Auto devices requires the AndroidX core library, which you can import into your project as follows:

  1. In the top-level build.gradle file, make sure you include Google's Maven repository, as shown below.

Groovy

allprojects {
    repositories {
        google()
    }
}

Kotlin

allprojects {
    repositories {
        google()
    }
}
  1. In your app module's build.gradle file, include the AndroidX core library dependency, as shown below: