2017-08-11 82 views
0

我正在項目中,我們正在顯示每個相冊更新的通知。但是在點擊一個通知後,它會從通知欄中刪除所有通知。點擊一個通知,刪除所有通知

我嘗試下面的代碼:

private void handleNotification(NotificationModel notificationModel) { 
    // display the notification in the notification bar 
    int i=0; 
    if(!TextUtils.isEmpty(notificationModel.getAlbum_id())) 
     i=Integer.parseInt(notificationModel.getAlbum_id()); 

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this); 

    //icon appears in device notification bar and right hand corner of notification 
    builder.setSmallIcon(R.mipmap.ic_launcher); 

    Intent intent=new Intent(this,MainActivity.class); 
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
    intent.putExtra(MainActivity.BUNDLE_ALBUM_ID_KEY, notificationModel.getAlbum_id()); 
    intent.putExtra(MainActivity.BUNDLE_ALBUM_NAME_KEY, notificationModel.getAlbum_title()); 

    /*intent = new Intent(this, NotificationActivity.class);*/ 
    PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),i,intent,PendingIntent.FLAG_UPDATE_CURRENT); 
    // Set the intent that will fire when the user taps the notification. 
    builder.setContentIntent(pendingIntent); 
    /*builder.setAutoCancel(true);*/ 
    // Large icon appears on the left of the notification 
    builder.setLargeIcon(BitmapFactory.decodeResource(getApplicationContext().getResources(), R.mipmap.ic_launcher)); 

    // Content title, which appears in large type at the top of the notification 
    builder.setContentTitle(notificationModel.getTitle()); 
    builder.setContentText(notificationModel.getMessage()); 
    builder.setAutoCancel(true); 
    NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 
    Notification notification=builder.build(); 

    notification.defaults = Notification.DEFAULT_SOUND 
      | Notification.DEFAULT_VIBRATE; 
    notification.flags |= Notification.FLAG_AUTO_CANCEL; 

    notificationManager.notify(i,notification); 
} 

我在做什麼錯?

+0

或檢查值我保持相同,每次可能是問題... –

+0

@MohdSaquib試圖。但不工作。也檢查了我的價值。它是獨一無二的。 – Subhra

+0

@Subhra你的代碼絕對正常。在另一臺設備上檢查相同的項目或檢查您的設備通知設置。 – Nikhil

回答

0

請檢查的值「i」,即工作作爲通知ID每次都保持相同,可能是問題... 這裏

int i=0; 
    if(!TextUtils.isEmpty(notificationModel.getAlbum_id())) 
     i=Integer.parseInt(notificationModel.getAlbum_id()); 

請檢查的「我」,我覺得值它總是在你的情況,你使用的是與下面的代碼相同

PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),**i**,intent,PendingIntent.FLAG_UPDATE_CURRENT); 

而且

notificationManager.notify(i,notification); 
+0

檢查'我'的價值。它是獨一無二的。如果'我'是相同的,單獨的通知不會爲每個專輯建立。每張專輯都會顯示單獨的通知。 – Subhra

+0

是兄弟,這是我的主要問題價值不應該是唯一的看到我已經給她的解決方案,她嘗試瞭解通知ID的邏輯值,即我在你的代碼應該是每次不同。 當我的價值會不同於你能達到你想要的。 –