2014-06-26 42 views
6

我想創建一個通知,但不會從我的應用中取消/刪除以前的通知。 這裏是我創建通知代碼:Android:在創建新通知之後,舊版本會被替換

private void notification(Context context, String title, String content) { 
    NotificationCompat.Builder mBuilder = 
      new NotificationCompat.Builder(context) 
      .setSmallIcon(R.drawable.notification_icon) 
      .setContentTitle(title) 
      .setContentText(content); 

    Intent resultIntent = new Intent(context, MainActivity.class); 
    TaskStackBuilder stackBuilder = TaskStackBuilder.create(context); 
    // Adds the back stack for the Intent (but not the Intent itself) 
    stackBuilder.addParentStack(MainActivity.class); 
    // Adds the Intent that starts the Activity to the top of the stack 
    stackBuilder.addNextIntent(resultIntent); 
    PendingIntent resultPendingIntent = 
      stackBuilder.getPendingIntent(
       0, 
       PendingIntent.FLAG_UPDATE_CURRENT 
      ); 
    mBuilder.setContentIntent(resultPendingIntent); 
    NotificationManager mNotificationManager = 
     (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); 
    //Id allows you to update the notification later on. 
    mNotificationManager.notify(100, mBuilder.build()); 
} 

回答

12

您正在使用您的通知硬編碼的ID,當然這將取代舊的。嘗試使用可變ID而不是硬編碼100.

mNotificationManager.notify(100+x, mBuilder.build()); 

或類似的東西。

相關問題