2017-09-29 19 views
11

Tôi đang cố hiển thị thông báo thông qua Android Auto. Thông báo hiện hiển thị trên điện thoại của tôi. Tuy nhiên, nó là không phải hiển thị trên trình mô phỏng Android Auto. Đây là một ứng dụng truyền thông.Thông báo tự động Android không hiển thị

automotvie_app_desc.xml:

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

Mã này là trong MediaBrowserService lớp học của tôi:

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

Đây là nơi thông báo được tạo ra:

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

tôi thiếu gì có được điều này để hiển thị trên màn hình tự động và không hiển thị trên điện thoại?

Trả lời

0

Hãy thử mã này để hiển thị các thông báo,

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

Điều này vẫn chỉ hiển thị thông báo trên điện thoại và không hiển thị trên đơn vị đầu tự động. – mattfred

1

NotificationCompat.CarExtender có vẻ là một lựa chọn duy nhất cho ứng dụng tuyên bố là "thông báo" (tin nhắn đọc và tính năng đáp ứng cho một ứng dụng tin nhắn ví dụ) .

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

Hiển thị thông báo ở nhà trong ngữ cảnh "Tự động" với ô tô "phương tiện" Ứng dụng dường như không được phép trong phiên bản api thực tế.

Để có thông báo lỗi liên quan đến ứng dụng phát media (như trường hợp của bạn), bạn có thể sử dụng trạng thái lỗi sẽ được hệ thống tự động diễn giải và hiển thị trực tiếp.

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

Hãy làm theo từng bước từ here

mẫu này thể hiện đầy đủ bản demo

EDIT

Đối với truyền thông thông báo bạn có thể sử dụng this. Ở đây từng bước giải thích về ứng dụng phương tiện và thông báo cho tự động

+0

Ứng dụng demo là một ứng dụng thông báo. Ứng dụng có thể là cả thông báo và phương tiện không? – mattfred

+0

để nhận thông báo phương tiện, hãy kiểm tra câu trả lời đã chỉnh sửa của tôi –

+0

Ngoài ra, điều này có thể hữu ích https://developer.android.com/training/auto/audio/index.html –

Các vấn đề liên quan