公式サイトではAndroid Autoに対応したサンプルアプリが公開されていますが、若干凝った作りの為、分かりづらい部分があります。そこでサンプルアプリを元に、簡単なメッセージアプリを作ってみました。
※公式サイトのサンプルアプリは、以下のURLからダウンロード可能です。
http://github.com/googlesamples/android-MessagingService
Android Auto用メッセージアプリでは、以下のことができます。
それでは実際のアプリの作り方を見ていきましょう。
(プロジェクトフォルダ)\res\xml\automotive_app_desc.xml
メッセージ部分をタップすると、
「New message from BRIL TARO,Hello Android Auto」
と音声で読み上げてくれます。
読み終わった後に既読通知のIntentがBroadcast送信され、以下の画面に遷移します。
ここで返信ボタンを押すと、返信通知用のIntentがBroadcast送信されます。
現在のシミュレーションアプリ上では返信内容を作成できず、アプリ側には"This is a reply"の固定文言のみが返ってきますが、将来的には音声入力などにも対応すると思われます。
実際に作ってみると、意外と簡単にNotificationを表示することができました。
現状ではまだ未完成の部分もありますが、これからどうUpdateされていくのか楽しみですね。
http://github.com/googlesamples/android-MessagingService
Android Auto用メッセージアプリでは、以下のことができます。
- 
車載機器へのNotification送信
 Android Auto用のAPIを使用して、Notificationを車載機器側(現在はシミュレーションアプリ上)に通知できます。車載機器側でNotificationを選択すると、内容を音声で読み上げてくれます。
- 
車載機器からの既読通知を受信
 車載機器側でNotificationが選択される(=音声読み上げ)と、既読になったことをIntent通知で受け取ることができます。
- 
車載機器からの返信を受信
 車載機器側でNotificationに対する返信操作が行われると、その内容をIntent通知で受け取ることができます。
それでは実際のアプリの作り方を見ていきましょう。
■AndroidManifestファイルの記載
・BroadcastReceiverの登録
既読通知と返信通知のIntent通知を受けるために、BroadcastReceiverを登録します。    <receiver android:name=".MyMessageReadReceiver" >
        <intent-filter>
            <action android:name="com.example.androidautonotificationtest.ACTION_MESSAGE_READ" />
        </intent-filter>
    </receiver>
    <receiver android:name=".MyMessageReplyReceiver" >
        <intent-filter>
            <action android:name="com.example.androidautonotificationtest.ACTION_MESSAGE_REPLY" />
        </intent-filter>
    </receiver>
・専用リソースファイルのメタデータ定義
AndoridManifestファイルには以下のように定義し、    <meta-data
        android:name="com.google.android.gms.car.application"
        android:resource="@xml/automotive_app_desc" />
別途、専用リソースファイルを用意します。(プロジェクトフォルダ)\res\xml\automotive_app_desc.xml
・専用リソースファイルの定義
AndroidManifestファイルで定義した専用リソースファイルには、以下のように記載します。    <automotiveapp>
        <uses name="notification"/>
    </automotiveapp>
■既読通知用のIntent設定
車載機器からNotificationの既読通知を受けるために、PendingIntentを生成します。- AndroidManifestファイルでBroadcastReceiverのIntent-filterに登録したアクションを、Intent#setAction()で設定する。
- Notification管理用の固有IDを、IntentのExtraに設定する。
 ※今回は固定値TEST_IDを使用しています。
    int TEST_ID = 1;
    String REPLY_ID = "conversation_id";
    String READ_ACTION =
            "com.example.androidautonotificationtest.ACTION_MESSAGE_READ";
    Intent readIntent = new Intent()
                        .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
                        .setAction(READ_ACTION)
                        .putExtra(REPLY_ID, TEST_ID);
    PendingIntent readPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            TEST_ID,
            readIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
■返信通知用のIntent設定
車載機器からNotificationの返信通知を受けるために、PendingIntentを生成します。- AndroidManifestファイルでBroadcastReceiverのIntent-filterに登録したアクションを、Intent#setAction()で設定する。
- Notification管理用の固有IDを、IntentのExtraに設定する。
- 返信内容のStringを受け取るためのキー(EXTRA_VOICE_REPLY)を登録する。
    String REPLY_ACTION =
            "com.example.androidautonotificationtest.ACTION_MESSAGE_REPLY";
    String EXTRA_VOICE_REPLY = "extra_voice_reply";
    Intent replyIntent = new Intent()
                    .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
                    .setAction(REPLY_ACTION)
                    .putExtra(REPLY_ID, TEST_ID);
    RemoteInput remoteInput = new RemoteInput.Builder(EXTRA_VOICE_REPLY)
                                    .setLabel(getApplicationContext().getString(R.string.app_name))
                                    .build();
    PendingIntent replyPendingIntent = PendingIntent.getBroadcast(getApplicationContext(),
            TEST_ID,
            replyIntent,
            PendingIntent.FLAG_UPDATE_CURRENT);
■メッセージオブジェクトの生成
UnreadConversation.Builderオブジェクトを生成します。- Notification送信者の設定(TEST_NAME)
- 既読通知用/返信通知用Intentの登録
- Notification本文の登録(TEST_MESSAGE)
    String TEST_NAME = "BRIL TARO";
    String TEST_MESSAGE = "Hello Android Auto!";
    UnreadConversation.Builder unreadConvBuilder =
            new UnreadConversation.Builder(TEST_NAME)
                .setReadPendingIntent(readPendingIntent)
                .setReplyAction(replyPendingIntent, remoteInput);
    unreadConvBuilder.addMessage(TEST_MESSAGE);
■Notificationオブジェクトの生成と送信
NotificationCompat.Builderクラスを使用して、Notificationオブジェクトを生成、送信します。    NotificationCompat.Builder notificationBuilder =
            new NotificationCompat.Builder(getApplicationContext())
                .setSmallIcon(R.drawable.ic_launcher)
                .extend(new CarExtender()
                            .setUnreadConversation(unreadConvBuilder.build()));
    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
    mNotificationManager.notify(TEST_ID, notificationBuilder.build());
■既読通知用のBroadcastReceiver
既読通知用のBroadcastReceiverでは、Notification送信時に設定した管理用の固有IDを取得できます。public class MyMessageReadReceiver  extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        int conversationId = intent.getIntExtra(MainActivity.REPLY_ID, -1);
    }
}
■返信通知用のBroadcastReceiver
返信通知用のBroadcastReceiverでは、Notification送信時に設定した管理用の固有IDと、車載機器側が設定した返信内容を取得できます。public class MyMessageReplyReceiver extends BroadcastReceiver{
    @Override
    public void onReceive(Context context, Intent intent) {
        int conversationId = intent.getIntExtra(MainActivity.REPLY_ID, -1);
        CharSequence reply;
        Bundle remoteInput = RemoteInput.getResultsFromIntent(intent);
        if (remoteInput != null) {
            reply = remoteInput.getCharSequence(MainActivity.EXTRA_VOICE_REPLY);
        }
    }
}
■Notificationの確認方法
上記処理を全て実装したアプリを作成して、以下の手順を行ってください。- メッセージアプリ用のシミュレーションアプリ(messaging-simulator.apk )をインストール。
 ※詳細は前回記事「Android AutoのAPIが公開されました」をご参照ください。
- テストアプリを起動し、Notificationを送信
 ※その後、ステータスバーに表示されるNotificationは開かないでください。
- シミュレーションアプリを起動
メッセージ部分をタップすると、
「New message from BRIL TARO,Hello Android Auto」
と音声で読み上げてくれます。
読み終わった後に既読通知のIntentがBroadcast送信され、以下の画面に遷移します。
ここで返信ボタンを押すと、返信通知用のIntentがBroadcast送信されます。
現在のシミュレーションアプリ上では返信内容を作成できず、アプリ側には"This is a reply"の固定文言のみが返ってきますが、将来的には音声入力などにも対応すると思われます。
実際に作ってみると、意外と簡単にNotificationを表示することができました。
現状ではまだ未完成の部分もありますが、これからどうUpdateされていくのか楽しみですね。


 







 
 
 
 
 
 
 
 
 
