2015-01-09 71 views
0

我在更新通知內容時沒有在通知窗口內重新排列時遇到問題。如何正確更新通知內容?

官方文檔說只要notification IDTAG是相同的,通知應該被替換。這正是發生的情況,除了一個問題 - 每次更新它時它都會被推到列表的頂部(除非優先級設置爲LOWMIN,我不想這麼做)。

通過使用setDefaults()未在構建器上設置任何默認值來發布無提示通知會有訣竅,但這又不是必需的。

我在做什麼錯?我知道這是可能的,因爲Gmail應用程序可以將其通知更改爲「撤消」,而無需重新排列。

下面是我使用的代碼:

void showFirstNotif(){ 

    mNotifBuilder.setContentTitle("First") 
      .setContentText("Body") 
      .setWhen(activityStartTime) 
      .setSmallIcon(PhotoUtils.getAppIconResId()) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setOnlyAlertOnce(true); 

    NotificationManagerCompat.from(this).notify("Test", NOTIF_ID, mNotifBuilder.build()); 

} 

void showSecondNotif(){ 

    mNotifBuilder.setContentTitle("Second") 
      .setContentText("Body") 
      .setWhen(activityStartTime) 
      .setSmallIcon(PhotoUtils.getAppIconResId()) 
      .setDefaults(Notification.DEFAULT_ALL) 
      .setOnlyAlertOnce(true); 

    NotificationManagerCompat.from(this).notify("Test", NOTIF_ID, mNotifBuilder.build()); 

} 

回答

1

我猜你的情況Android是從頭再來重建視圖。您只需致電updateNotification(String)並留言即可更新通知,而不會讓用戶惱火併重新安排您的Notification

我創建了一個例子,它工作得很好。看一看:

public class MainActivity extends Activity { 

    private NotificationManager mNotifyManager; 
    private NotificationCompat.Builder mBuilder; 
    private int mNotificationId1 = 1; 
    private int mNotificationId2 = 2; 
    private int mNotificationId3 = 3; 
    private int mNotificationId4 = 4; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mNotifyManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 
     mBuilder = new NotificationCompat.Builder(MainActivity.this); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 1"); 
     mNotifyManager.notify(mNotificationId1, mBuilder.build()); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 2"); 
     mNotifyManager.notify(mNotificationId2, mBuilder.build()); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 3"); 
     mNotifyManager.notify(mNotificationId3, mBuilder.build()); 

     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setContentText("Notification No 4"); 
     mNotifyManager.notify(mNotificationId4, mBuilder.build()); 

     findViewById(R.id.button).setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       updateNotification("Notification 2 changed silently"); 
      } 
     }); 
    } 

    private void updateNotification(String message) { 
     mBuilder.setSmallIcon(R.drawable.ic_launcher) 
       .setContentTitle("Notification") 
       .setDefaults(Notification.DEFAULT_ALL); 
     mBuilder.setContentText(message); 
     mNotifyManager.notify(mNotificationId2, mBuilder.build()); 
    } 
} 

但是如果你使用:

private void updateNotification(String message) { 
     NotificationCompat.Builder builder = new NotificationCompat.Builder(
       MainActivity.this); 
     builder.setSmallIcon(R.drawable.ic_launcher).setContentTitle(
       "Notification").setDefaults(Notification.DEFAULT_ALL); 
     builder.setContentText(message); 
     mNotifyManager.notify(mNotificationId2, builder.build()); 
    } 

在這種情況下,雖然使用的是相同的通知ID,但它的重建,從而推到頂部,如果它是一個新的Notification本身。

+0

沒有。我之前已經嘗試過,只需設置必須更新的屬性。沒有工作:( – Saket

+0

Ohkay,謝謝你! – Saket

+0

@Saket我已經更新了我的答案,看看。 – CodeWarrior