Android自动通知未显示

时间:2017-09-29 21:45:38

标签: android android-notifications android-auto

我正在尝试通过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;
}

我想要在自动显示屏上显示而不是在手机上显示什么?

3 个答案:

答案 0 :(得分:2)

NotificationCompat.CarExtender 似乎仅适用于应用声明为&#34;通知&#34; (例如,消息传递应用程序的消息读取和响应功能)。

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

在家中显示通知&#34;自动&#34;与&#34;媒体&#34;在实际的api版本中似乎不允许使用automotiveApp。

对于与媒体播放应用相关联的错误消息(就像您的情况一样),您可以使用错误状态,该状态将由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

答案 1 :(得分:0)

尝试使用此代码显示通知

    public string DownloadSite(string RefinedLink)
    {
        try
        {
            Uri address = new Uri(RefinedLink);

            ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

            System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls11 | SecurityProtocolType.Tls12;

            using (WebClient webClient = new WebClient())
            {
                var stream = webClient.OpenRead(address);
                using (StreamReader sr = new StreamReader(stream))
                {
                    var page = sr.ReadToEnd();

                    return page;
                }
            }

        }
        catch (Exception e)
        {
            log.Error("DownloadSite - error Lin = " + RefinedLink, e);
            return null;
        }
    }

答案 2 :(得分:0)

请按照here

的步骤进行操作

此示例演示完整演示

编辑

对于媒体通知,您可以使用this。这里一步一步解释媒体应用和自动

的通知