安卓汽车通知未显示

13

我试图通过Android Auto显示通知。这个通知在我的手机上确实显示了。然而,在Android Auto模拟器中没有显示出来。这是一个媒体应用程序。

automotvie_app_desc.xml:

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

这段代码在我的MediaBrowserService类中:


Translated:

这段代码在我的MediaBrowserService类中:

private Notification postNotification(AutoNotificationHelper.Type type) {
    Log.d(TAG, "Post Notification");
    Notification notification = AutoNotificationHelper.createMenuErrorNotification(
            getApplicationContext(), type, mSession);

    if (notification != null) {
        mNotificationManager.notify(TAG, NOTIFICATION_ID, notification);
    }
    return notification;
}

这里是通知创建的位置:

static Notification createMenuErrorNotification(Context context, Type type,
                                                MediaSessionCompat mediaSession) {

    MediaControllerCompat controller = mediaSession.getController();
    MediaMetadataCompat mMetadata = controller.getMetadata();
    PlaybackStateCompat mPlaybackState = controller.getPlaybackState();

    if (mMetadata == null) {
        Log.e(TAG, "MetaData is null");
    }

    if (mPlaybackState == null) {
        Log.e(TAG, "Playback state is null");
    }

    if (type.equals(Type.MENU_ERROR)) {
        Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.error);

        NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(context.getApplicationContext());
        notificationBuilder.extend(new android.support.v4.app.NotificationCompat.CarExtender())
                .setStyle(new NotificationCompat.MediaStyle()
                .setMediaSession(mediaSession.getSessionToken()))
                .setSmallIcon(R.drawable.error)
                .setShowWhen(false)
                .setContentTitle(context.getString(R.string.title))
                .setContentText(context.getString(R.string.message))
                .setLargeIcon(icon)
                .setVisibility(NotificationCompat.VISIBILITY_PUBLIC);

        return notificationBuilder.build();
    }

    return null;
}

我需要做哪些更改才能让它显示在自动显示屏上而不是手机上?

3个回答

2

NotificationCompat.CarExtender 似乎只适用于声明为“通知”(例如,消息应用的已读和响应功能)的应用程序。

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

在实际的API版本中,似乎不允许在“自动”上下文中使用“媒体”汽车应用程序显示主屏幕通知。
对于与媒体播放应用程序相关联的错误消息(就像在您的情况下一样),您可以使用错误状态,该状态将由Auto系统直接解释和显示。
private void showErrorMessage(final int errorCode, final String errorMessage) {
    final PlaybackStateCompat.Builder playbackStateBuilder = new PlaybackStateCompat.Builder();
    playbackStateBuilder.setState(PlaybackStateCompat.STATE_ERROR, -1L, 1.0F);
    playbackStateBuilder.setErrorMessage(errorCode, errorMessage);
    mSession.setPlaybackState(playbackStateBuilder.build());
}

enter image description here


0
尝试使用以下代码显示通知:
 private void showPushNotification(String title, String message, Intent tapIntent, int notificationID) {
        android.support.v7.app.NotificationCompat.Builder builder = new android.support.v7.app.NotificationCompat.Builder(this);
        builder.setSmallIcon(R.drawable.swiftee_white_logo_notification);
        //Intent tapIntent = new Intent(this, HomeScreenActivity.class);
        //tapIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
        //tapIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        //tapIntent.putExtra(AppConstants.PUSH_MESSAGE, true);
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, tapIntent, PendingIntent.FLAG_ONE_SHOT);
        builder.setContentIntent(pendingIntent);
        builder.setAutoCancel(true);
        builder.setContentTitle(title);
        builder.setContentText(message);
        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(notificationID, builder.build());
    }

这仅在手机上显示通知,而不在汽车音响系统上显示。 - mattfred

0
请按照这里的步骤逐步操作。
此示例演示完整演示。 编辑 媒体通知可以使用这个。这里逐步解释了自动媒体应用程序和通知。

演示应用是一个通知应用程序。一个应用程序可以同时是通知和媒体吗? - mattfred
此外,这个链接可能会有所帮助 https://developer.android.com/training/auto/audio/index.html - Rajesh N

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接