使用 Android 自动未显示通知

Notification not showing up using Android Auto

我遵循了 Udacity 上的教程和 Android auto 的开发者网站。我正在使用 DVU 进行测试。通知不会出现在 DHU 上,但会出现在 phone 上,这是我的代码:

使用 GcmListenerService:

 final PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);

final PendingIntent contentIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT
                | PendingIntent.FLAG_ONE_SHOT);
        Intent msgHeardIntent = new Intent()
                .addFlags(Intent.FLAG_INCLUDE_STOPPED_PACKAGES)
                .setAction("com.xyz.abc.MY_ACTION_MESSAGE_HEARD")
                .putExtra("conversation_id", 9999);
        PendingIntent msgHeardPendingIntent =
                PendingIntent.getBroadcast(getApplicationContext(),
                        9999,
                        msgHeardIntent,
                        PendingIntent.FLAG_UPDATE_CURRENT);

        NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder =
                new  NotificationCompat.CarExtender.UnreadConversation.Builder("TestAndroidAuto")
                        .setReadPendingIntent(msgHeardPendingIntent);

        unreadConvBuilder.addMessage(description);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(this)
                        .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
                        .setSmallIcon(R.drawable.ic_stat_notification_icon)
                        .setContentTitle(title)
                        .setTicker(ticker)
                        .setStyle(new NotificationCompat.BigTextStyle()
                                .bigText(description))
                        .setContentText(description)
                        .setDefaults(Notification.DEFAULT_ALL);
        mBuilder.extend(new NotificationCompat.CarExtender().setUnreadConversation(unreadConvBuilder.build()));
        mBuilder.setContentIntent(contentIntent);
        mBuilder.setAutoCancel(true);
        mNotificationManager = NotificationManagerCompat.from(getApplicationContext());
        mNotificationManager.notify(REMOTE_NOTIFICATION_ID, mBuilder.build());

我不明白我哪里出错了,因为这是相当新的。

您没有设置回复操作,因此 android auto 不会显示通知。首先在最开始创建一个这样的远程输入:

// Build a RemoteInput for receiving voice input in a Car Notification
        RemoteInput remoteInput = new RemoteInput.Builder(9999)
                .setLabel(msg)
                .build();

然后在 unreadConversation 构建器中设置 replyAction。所以现在你的看起来像这样:

NotificationCompat.CarExtender.UnreadConversation.Builder unreadConvBuilder =
            new  NotificationCompat.CarExtender.UnreadConversation.Builder("TestAndroidAuto")
                    .setReadPendingIntent(msgHeardPendingIntent)
            .setReplyAction(msgHeardPendingIntent, remoteInput);;