Android 通知不会在点击通知后消失

如果有一些通知问题,我想在通知栏中显示。虽然我将通知标志设置为 Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL,但是点击之后通知并没有消失。知道我哪里做错了吗?

NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


int icon = R.drawable.icon;
CharSequence tickerText = "Ticker Text";
long time = System.currentTimeMillis();


Notification notification = new Notification(icon, tickerText, time);
notification.flags = Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCEL;


Context context = getApplicationContext();
CharSequence contentTitle = "Title";
CharSequence contentText = "Text";
Intent notificationIntent = new Intent(this, SilentFlipConfiguration.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent);
mNotificationManager.notify(1,notification);
77329 次浏览
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

根据文件:

按位 -或者ed 到应该设置的标志字段中 如果用户单击通知时应取消该通知,则为

// Uses the default lighting scheme
notification.defaults |= Notification.DEFAULT_LIGHTS;


// Will show lights and make the notification disappear when the presses it
notification.flags |= Notification.FLAG_AUTO_CANCEL | Notification.FLAG_SHOW_LIGHTS;

通过 NotificationBuilder构建 Notification时,可以使用 notificationBuilder.setAutoCancel(true);

2016年状态: 你可以使用 mBuilder.setAutoCancel(true)

资料来源: https://developer.android.com/reference/android/app/Notification.Builder.html

使用标志 Notification.FLAG _ AUTO _ CANCEL

Notification notification = new Notification(icon, tickerText, when);
notification.setLatestEventInfo(context, contentTitle, contentText, pendingIntent);


// Cancel the notification after its selected
notification.flags |= Notification.FLAG_AUTO_CANCEL;

启动应用程序:

NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);


Intent intent = new Intent(context, App.class);


PendingIntent pendingIntent = PendingIntent.getActivity(context, intent_id, intent, PendingIntent.FLAG_UPDATE_CURRENT);

我的答案是使用 mBuilder.setOngoing(false)

删除通知

通知保持可见,直到发生下列情况之一:

  1. 用户将通知解除。
  2. 用户单击通知,并在创建通知时调用 setAutoCancel ()。
  3. 您可以为特定的通知 ID 调用 Cancel ()。此方法还会删除正在进行的通知。
  4. 您调用 CancelAll () ,它将删除以前发出的所有通知。
  5. 如果在使用 setTimeoutAfter ()创建通知时设置超时,系统会在指定的持续时间过去后取消通知。如果需要,可以在指定的超时持续时间过去之前取消通知。

详情请参阅: Https://developer.android.com/training/notify-user/build-notification?hl=en