12

我正在尝试通过 Android Auto 显示通知。通知确实显示在我的手机上。但是,它没有显示在 Android Auto 模拟器上。这是一个媒体应用程序。

automotvie_app_desc.xml:

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

这段代码在我的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;
}

为了让它显示在自动显示屏上而不是手机上,我错过了什么?

4

3 回答 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());
}

在此处输入图像描述

于 2017-10-08T16:46:30.827 回答
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());
    }
于 2017-10-04T07:14:50.390 回答
0

请从这里一步一步地进行

此示例演示了完整的演示

编辑

对于媒体通知,您可以使用。这里逐步解释了媒体应用程序和自动通知

于 2017-10-10T04:19:01.143 回答