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

如果我想在通知栏中显示一些通知, 尽pipe我将通知标志设置为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); 

在通过NotificationBuilder构buildNotification ,可以使用notificationBuilder.setAutoCancel(true);

 notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL 

从文档:

如果通知被用户点击时应该取消,则位位应该被设置为应该设置的标志字段

 // 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; 

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);

//取消通知后selectnotification.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);