Android auto - 推送 unreadConversation 的代码不起作用

Android auto - Code to push unreadConversation not working

我正在使用桌面主机,我有以下代码将对话推送到该设备,请记住,我在未决意图上设置了 null,因为我不需要任何 hooks/callbacks。我只想在 android auto 处于活动状态时发送通知。这是无效的代码:

private void pushAndroidAutoUnreadConversation(String msg) {
// Build a RemoteInput for receiving voice input in a Car Notification
RemoteInput remoteInput = new RemoteInput.Builder(MY_VOICE_REPLY_KEY)
.setLabel(msg)
.build();

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

unreadConvBuilder.addMessage(msg)
.setLatestTimestamp(System.currentTimeMillis());


Bitmap largeIcon = BitmapFactory.decodeResource(getResources(),
R.drawable.icon);


NotificationCompat.Builder notificationBuilder =
new NotificationCompat.Builder(getApplicationContext())
  .setSmallIcon(R.drawable.icon)
  .setLargeIcon(largeIcon).setContentTitle(msg);

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

NotificationManagerCompat msgNotificationManager =
NotificationManagerCompat.from(this);
msgNotificationManager.notify("my_apps_tag",
Integer.parseInt(MY_VOICE_REPLY_KEY), notificationBuilder.build());

}

MY_VOICE_REPLY_KEY 只是一个数字,它的 9675

这是我的 build.gradle 文件:

应用插件:'com.android.application'

android {
    compileSdkVersion 21
    buildToolsVersion "23.0.1"

    defaultConfig {
        applicationId "org.myapp.easy"
        minSdkVersion 9
        targetSdkVersion 21
    }

    buildTypes {
        release {
            minifyEnabled true
            proguardFiles getDefaultProguardFile('old_proguard-android.txt')
        }
    }
}

dependencies {
    compile files('libs/GoogleAdMobAdsSdk-4.1.1.jar')
    compile 'com.android.support:support-v4:21.0.2'
}

这是清单的一部分:

   <application
        android:allowBackup="false"
        android:icon="@drawable/icon"
        android:label="@string/app_name" >

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

这是我放在 xml 文件夹中的 automotive_app_desc.xml:

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

我在看官方教程here

我假设当我创建通知时,物理通知应该出现在 android 自动仪表板上,但我什么也没看到。但是在移动设备上我看到了我的通知。让我解释一下发生了什么。在插入汽车之前,我正在创建一条消息方式。因此,当用户与汽车断开连接时,此方法会得到 运行。当用户连接到 android auto 时,我希望这个通知应该显示在仪表板上,但它没有,但我在移动设备通知托盘上看到了它。

终于找到问题了。我正在为未决意图传递 null。它不想那样。我必须为听到的消息和回复的消息制定未决意图,并实际将它们设置在 UnreadConversation.Builder 中,如下所示:

UnreadConversation.Builder unreadConvBuilder =
            new UnreadConversation.Builder("reminder note")
                    .setReadPendingIntent(msgHeardPendingIntent)
                    .setReplyAction(msgHeardPendingIntent, remoteInput);

所以它似乎不喜欢 setReadPendingIntent 和 setReplyAction 为 null。