我正在开展项目,我们正在为每个相册更新显示通知。但是,只需点击一个通知,它就会从通知栏中删除所有通知。
我尝试了以下代码:
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)
每次可能出现问题时,请检查作为通知ID工作的“i”的值是否相同... 的这里强>
int i=0;
if(!TextUtils.isEmpty(notificationModel.getAlbum_id()))
i=Integer.parseInt(notificationModel.getAlbum_id());
请检查“i”的值,我认为在您使用以下代码的情况下它总是相同的
PendingIntent pendingIntent = PendingIntent.getActivity(getApplicationContext(),**i**,intent,PendingIntent.FLAG_UPDATE_CURRENT);
并且
notificationManager.notify(i,notification);