Android通知点击后没有消失

138

如果我遇到了在通知栏中显示通知的问题。虽然我将通知标志设置为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);
7个回答

318
在使用NotificationBuilder构建Notification时,您可以使用notificationBuilder.setAutoCancel(true);

2
那么,使用NotificationmNotificationManager.notify(1,notification);和使用NotificationBuildermNotificationManager.notify(1, mBuilder.build());创建通知有什么区别?谢谢。 - Yohanim
9
这个答案应该被接受,因为它更符合当前 Android 设计原则。 - jmaculate
这个答案是正确的。被接受的那个方案有时候不起作用。当 GCM(或者你使用的其他服务)上有多个通知时会出现问题。一旦你向通知服务器发送请求,它会返回很多通知,有时候会导致通知重复出现。 - Nikola Milutinovic
7
notificationBuilder.setAutoCancel(true);对我不起作用。我应该在我的Pending Intent之前放置它吗? - Kairi San

131
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

根据文档:

要进行按位或运算的位,应设置该位以取消在用户单击通知时应该取消的通知中的标志字段


3
这不是正确的答案。Notification.DEFAULT_LIGHTSNotification.defaults 类的一部分,而不是 Notification.flags 类。请参考我的回答以获取适当的设置器。 - Darcy
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL; 这个方法可行,谢谢 Synic。 - Ravikumar11
1
这个答案中的代码导致通知声音被多次播放。请查看其他答案。 - ban-geoengineering

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

1
我已经阅读了Android文档。我不太明白什么时候应该使用标志(flags)。为什么只有notification.defaults=notification.DEFAULT_LIGHTS就足以显示灯光,而不需要标志呢?因为振动和声音可以在没有标志的情况下工作。 - Ashwin
我正在使用NotificationBuilder,NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(this) .setSmallIcon(android.R.drawable.ic_popup_sync) .setContentTitle("新推文") .setContentText("有 " + count + " 条推文"); mBuilder.setDefaults(NotificationCompat.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL); - Joseph

14

3
对我来说,答案是使用 mBuilder.setOngoing(false)

1
使用标志 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);

1

移除通知

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

  1. 用户关闭通知。
  2. 用户点击通知,并且在创建通知时调用了setAutoCancel()方法。
  3. 您为特定的通知ID调用cancel()方法。该方法还会删除正在进行中的通知。
  4. 您调用cancelAll()方法,这将删除您之前发布的所有通知。
  5. 如果您在使用setTimeoutAfter()方法创建通知时设置了超时时间,系统将在指定的持续时间后取消通知。如果需要,在指定的超时时间到期之前,您可以取消通知。

更多细节请参见: https://developer.android.com/training/notify-user/build-notification?hl=zh-cn


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接