2017-09-29 3 views
11

Ich versuche, eine Benachrichtigung über Android Auto anzuzeigen. Die Benachrichtigung zeigt auf meinem Telefon. Es ist jedoch nicht zeigt auf Android Auto-Emulator. Dies ist eine Medienanwendung.Android Auto-Benachrichtigung wird nicht angezeigt

automotvie_app_desc.xml:

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

Dieser Code ist in meiner MediaBrowserService Klasse:

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

Hier ist, wo die Meldung erstellt:

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

Was bin ich Holen Sie dies auf der Auto-Display und nicht auf dem Handy anzeigen?

Antwort

0

diesen Code Versuchen Sie, die Benachrichtigung zu zeigen,

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()); 
    } 
+0

Dies zeigt immer noch nur die Benachrichtigung auf dem Telefon und nicht auf der Auto-Head-Unit. – mattfred

1

NotificationCompat.CarExtender scheint nur eine Option zu sein für App erklären als „Benachrichtigung“ (Nachricht für eine Messaging-Anwendung zum Beispiel lesen und Antwort-Funktion) .

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

Benachrichtigung zu Hause in "Auto" -Kontext mit einer "media" -AutomotiveApp angezeigt scheint nicht in der tatsächlichen API-Version.

Für eine Fehlermeldung in Verbindung mit einer Media Playing App (wie es in Ihrem Fall zu sein scheint) können Sie den Fehlerstatus verwenden, der direkt von Auto System interpretiert und angezeigt wird.

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

Bitte folgen Sie Schritt für Schritt von here

Diese Probe

EDIT

Für Medien Mitteilung zeigen vollständige Demo Sie this verwenden können. Hier Schritt für Schritt erklärt über Medien-App und Benachrichtigung für Auto

+0

Die Demo-App ist eine Benachrichtigungsanwendung. Kann eine Anwendung sowohl eine Benachrichtigung als auch ein Medium sein? – mattfred

+0

für Medienbenachrichtigung, überprüft meine bearbeitete Antwort –

+0

Auch kann dies hilfreich sein https://developer.android.com/training/auto/audio/index.html –

Verwandte Themen