2

我正在玩堆疊式通知,但無法使其工作,通知根本無法觸發。下面的代碼:Android可穿戴堆疊通知

private void sendSimpleStackedNotifications() { 
     NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender() 
       .setBackground(BitmapFactory.decodeResource(getResources(), R.drawable.notif_background)); 

     for (int i = 0; i < 5; i++) { 
      ...    
     } 
    } 

裏面的for循環我有:
版本1:

Notification n = new NotificationCompat.Builder(this) 
         .setContentTitle("New notification!") 
         .setContentText("Notification nº" + (i + 1)) 
         .extend(wearableExtender) 
         .setGroup(GROUP) 
         .build(); 
mNotificationManager.notify(i, n); 

版本2:

NotificationCompat.Builder nb = new NotificationCompat.Builder(this) 
         .setContentTitle("New notification!") 
         .setContentText("Notification nº" + (i + 1)) 
         .extend(wearableExtender) 
         .setGroup(GROUP);   
mNotificationManager.notify(i, nb.build()); 

但沒有辦法工作。我錯過了什麼?

編輯: 由於用戶艾爾我發現我失蹤了:

.setSmallIcon() 

現在通知顯示正確,但我有一個問題,他們不是在該分組-held設備即使我添加(在兩個版本1個2版):

.setGroup(GROUP) 
.setGroupSummary(true) 

在可穿戴設備中,它們被正確堆疊。

任何想法爲什麼?
謝謝。

回答

2

您需要設置SmallIcon的通知

 for(int i = 0 ; i < 5 ; i++){ 
     Notification n = new NotificationCompat.Builder(this) 
       .setContentTitle("New notification!") 
       .setContentText("Notification nº" + i + 1) 
       .extend(wearableExtender) 
       .setGroup(GROUP) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .build(); 
     mNotificationManager.notify(i, n); 
    } 

也許你會需要一個summaryNotification

private void sendNotification(){ 
    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender() 
      .setBackground(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher)); 

    NotificationManagerCompat mNotificationManager = NotificationManagerCompat.from(this); 

    String GROUP = "group"; 

    //send stack Notification (wearable) 
    for(int i = 0 ; i < 5 ; i++){ 
     Notification n = new NotificationCompat.Builder(this) 
       .setContentTitle("New notification!") 
       .setContentText("Notification nº" + i + 1) 
       .extend(wearableExtender) 
       .setGroup(GROUP) 
       .setSmallIcon(R.mipmap.ic_launcher) 
       .build(); 
     mNotificationManager.notify(i, n); 
    } 

    //send summary notification (only handheld) 
    NotificationCompat.Builder summaryNotification = new NotificationCompat.Builder(this) 
      .setGroupSummary(true) 
      .setGroup(GROUP) 
      .setContentText("New notification!") 
      .setContentTitle("5 New Notification!") 
      .setSmallIcon(R.mipmap.ic_launcher); 

    mNotificationManager.notify(-1 , summaryNotification.build()); 
} 

「重要的是,你還提供了在手持設備上顯示的彙總通知。因此,在除了將每個唯一通知添加到同一個堆棧組之外,還會在摘要通知中添加摘要通知並調用setGroupSummary()。該通知不會顯示在可穿戴設備上的通知堆棧中,但它似乎是唯一的notifi手持設備上的陽離子「。

https://developer.android.com/training/wearables/notifications/stacks.html

+0

你的答案是正確的,它回答我的問題,但我現在已經有點問題。讓我更新我的問題,看看你能否再次幫助我;) – GuilhE