android auto-Providing Messaging for Auto(UnreadConversation)

desaco發表於2016-01-23

 The Android framework enables messaging apps to extend their services into car dashboards using a standard user interface that lets drivers keep their eyes on the road.Once connected, your app can provide text information to users and allow them to respond. The Auto dashboard system handles displaying the notification and the interface for replies.

Messaging apps do not run directly on the Android dashboard hardware. They are installed on a separate Android mobile device. When the mobile device is plugged into a dashboard, the installed messaging apps can offer services for viewing and responding to messages through the Auto user interface.

To enable your app to provide messaging services for Auto devices:

  • Configure your app manifest to indicate that your app provides messaging services which are compatible with Android Auto dashboard devices.
  • Build and send a specific type of notification for display on Auto devices.
  • Configure your app to receive Intent objects that indicate a user has read or replied to a message.

The v4 support library defines an UnreadConversation object. This object holds all messages in a conversation which have not yet been heard by the user. To give those messages to the user, you attach thatUnreadConversation to a notification. However, you do not attach messages to the UnreadConversationdirectly. Instead, you must first set up an UnreadConversation.Builder object for the conversation. The messages are added to the builder, then when you are ready to send the messages, you use the builder to create the actual UnreadConversation and attach the UnreadConversation to the notification.

Note: When Auto presents messages to the user, it uses the notification tag and ID to determine which conversation the messages belong to. It is important to use the same tag and ID for all messages in a conversation, and to not use that tag for other conversations.

This section describes how the mobile device interacts with Auto to present messages to the user.

  1. The app receives a message that it wants to pass on to the user. The app attaches the message to anUnreadConversation.Builder object, then uses the UnreadConversation.Builder to generate anUnreadConversation. The app attaches that UnreadConversation to a notification. That notification is associated with a CarExtender object, which indicates that the notification can be handled by Android Auto.
  2. The app posts the notification. The Android notification framework passes the message to Auto. Auto uses the notification tag and ID to determine which conversation the message belongs to, and presents the message to the user in an appropriate way.
  3. When the user listens to the message, Auto triggers the app's message heard pending intent. The app should discard the UnreadConversation object and its builder at this time, since the messages contained in those objects have been heard by the user.
  4. If the user sends a reply, Auto triggers the app's "message reply" intent and attaches a transcript of the user's response. The app can take appropriate action, based on the app's logic. For example, a chat app might interpret the reply as a message to go to the other conversation participants.

 This manifest entry refers to a secondary xml file, where you declare what Auto capabilities your app supports. For an app that supports messaging for Auto devices, add an xml file to the res/xml/ your app's development project directory as automotive_app_desc.xml, with the following content:

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

 > You define the read action and reply action intents types for your app and the BroadcastReceiver classes that handle them in the manifest. The following code example demonstrates how to declare these intents and their associated receivers.

<application>
    ...
    <receiver android:name=".MyMessageHeardReceiver">
        <intent-filter>
          <action android:name="com.myapp.messagingservice.MY_ACTION_MESSAGE_HEARD"/>
        </intent-filter>
    </receiver>

    <receiver android:name=".MyMessageReplyReceiver">
        <intent-filter>
          <action android:name="com.myapp.messagingservice.MY_ACTION_MESSAGE_REPLY"/>
        </intent-filter>
    </receiver>
    ...
</application>
> A messaging app provides messages to a connected Auto dashboard using the notifications framework. When your messaging app has a message for a user, you build a specially configured notification that is received by the dashboard system and presented to the user. The Auto device manages the presentation on the dashboard screen and may play the message via text-to-speech. The dashboard system also handles voice interaction if the user replies to a message using verbal input.

 The messaging user interface for Auto presents users with two levels of information about messages. The first level of notification tells users what conversations are available, and who they are with, but not the content of the messages. Typically, a conversation is one or more messages from another user to the Auto user.

 The second level of the notification is the actual content of messages in the conversation. If a user indicates they want to hear the messages in a conversation, the Auto user interface plays the messages using text-to-speech.

 how to define a PendingIntent to let your app know if a conversation was read to the Auto user:

Intent msgHeardIntent = new Intent()
    .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
    .setAction("com.myapp.messagingservice.MY_ACTION_MESSAGE_HEARD")
    .putExtra("conversation_id", thisConversationId);

PendingIntent msgHeardPendingIntent =
    PendingIntent.getBroadcast(getApplicationContext(),
        thisConversationId,
        msgHeardIntent,
        PendingIntent.FLAG_UPDATE_CURRENT);

 Messaging notifications for Auto organize messages into conversations using theNotificationCompat.CarExtender.UnreadConversation class, that represents an unread or new portion of a conversation from a particular sender. It contains a list of messages from the sender.

 You generally do not configure the UnreadConversation directly. Instead, you configure anUnreadConversation.Builder with the information about the conversation, as shown in the following example code.

// Build a RemoteInput for receiving voice input in a Car Notification
RemoteInput remoteInput = new RemoteInput.Builder(MY_VOICE_REPLY_KEY)
        .setLabel(getApplicationContext().getString(R.string.notification_reply))
        .build();

// Create an unread conversation object to organize a group of messages
// from a particular sender.
UnreadConversation.Builder unreadConvBuilder =
    new UnreadConversation.Builder(conversationName)
        .setReadPendingIntent(msgHeardPendingIntent)
        .setReplyAction(msgReplyPendingIntent, remoteInput);

Note: You won't actually create the UnreadConversation until you are almost ready to send the message.

When a message arrives for a conversation, you take the following steps to dispatch it as a notification to Auto.

First, add the message to the UnreadConversation.Builder for this conversation, and update its timestamp:

unreadConvBuilder.addMessage(messageString)
    .setLatestTimestamp(currentTimestamp);

Note: If you are sending several messages at once, add them to the UnreadConversation.Builder in order, from oldest to newest.

Then create the NotificationCompat.Builder object that builds the actual notification. You need to use the pending intents you created in the previous step.

NotificationCompat.Builder notificationBuilder =
    new NotificationCompat.Builder(getApplicationContext())
        .setSmallIcon(smallIconResourceID)
        .setLargeIcon(largeIconBitmap);

You'll also need to extend the NotificationCompat.Builder with the CarExtender. This is where you actually create the UnreadConversation object using the builder you just created, and attach it to the CarExtender:

notificationBuilder.extend(new CarExtender()
    .setUnreadConversation(unreadConvBuilder.build());

Note: If you wish, you can set an override icon or color for the CarExtender by calling setLargeIcon() orsetColor(). The override icon or color is used when the notification is handled by a car, and has no effect if the notification is handled on the Android device. This is useful if the notification's default icon or color are not suitable for the car's display.

Once you've done all this, you use your app's NotificationManagerCompat to send the notification:

NotificationManagerCompat msgNotificationManager =
    NotificationManagerCompat.from(context);
msgNotificationManager.notify(notificationTag,
    notificationId, notificationBuilder.build());
public class MyMessageHeardReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {

        // If you set up the intent as described in
        // "Create conversation read and reply intents",
        // you can get the conversation ID by calling:
        int thisConversationId = intent.getIntExtra("conversation_id", -1);

        // Remove the notification to indicate it has been read
        // and update the list of unread conversations in your app.
    }
}

Once a notification is read, your app can remove it by calling NotificationManagerCompat.cancel() with the notification ID. Within your app, you should mark the messages provided in the notification as read.

Note: An alternative to this implementation is to use a service in a PendingIntent.

how to define a BroadcastReceiver class to handle a received message reply intent:

  public class MyMessageReplyReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        // If you set up the intent as described in
        // "Create conversation read and reply intents",
        // you can get the conversation ID by calling:
        int thisConversationId = intent.getIntExtra("conversation_id", -1).

    }

    /**
     * Get the message text from the intent.
     * Note that you should call
     * RemoteInput.getResultsFromIntent() to process
     * the RemoteInput.
     */
    private CharSequence getMessageText(Intent intent) {
        Bundle remoteInput =
            RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            return remoteInput.getCharSequence(MY_VOICE_REPLY_KEY);
        }
        return null;
    }

}

相關文章