Android Auto notification not showing
Asked Answered
H

3

13

I'm attempting to show a notification through Android Auto. The notification does show on my phone. However, it is not showing on Android Auto emulator. This is a media application.

automotvie_app_desc.xml:

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

This code is in my MediaBrowserService class:

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;
}

Here is where the notification is created:

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;
}

What am I missing to get this to show on the auto display and not on the phone?

Horowitz answered 29/9, 2017 at 21:45 Comment(0)
K
2

NotificationCompat.CarExtender seems to be an option only for app declare as "notification" (message read and response feature for a messaging app for example).

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

Display notification on home in "Auto" context with a "media" automotiveApp seems not allowed in actual api version.

For an error message associated to a media playing app (like it seems to be in your case) you can use error state which will be interpreted and displayed directly by Auto system.

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

Kass answered 8/10, 2017 at 16:46 Comment(0)
S
0

Try this code to show the notification,

 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());
    }
Shaunshauna answered 4/10, 2017 at 7:14 Comment(1)
This is still only showing the notification on the phone and not on the auto head unit.Horowitz
B
0

Please follow step by step from here

This sample demonstrate full demo

EDIT

For Media Notification you can use this . Here step by step explained about media app and notification for auto

Bussard answered 10/10, 2017 at 4:19 Comment(2)
The demo app is a notification application. Can an application be both notification and media?Horowitz
Also,this can be helpful developer.android.com/training/auto/audio/index.htmlBussard

© 2022 - 2024 — McMap. All rights reserved.