如果我想在通知栏中显示通知问题。虽然我将通知标记设置为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);
答案 0 :(得分:277)
在Notification
之前构建NotificationBuilder
时,您可以使用notificationBuilder.setAutoCancel(true);
。
答案 1 :(得分:129)
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL
来自文档:
按位 - 或加入应设置的flags字段 如果在用户点击通知时取消通知
答案 2 :(得分: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;
答案 3 :(得分:13)
2016状态:您可以使用mBuilder.setAutoCancel(true)
。
来源:https://developer.android.com/reference/android/app/Notification.Builder.html
答案 4 :(得分: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);
答案 5 :(得分:1)
我的答案是使用mBuilder.setOngoing(false)
答案 6 :(得分:0)
在以下情况之一发生之前,通知一直保持可见:
有关更多详细信息,请参见: https://developer.android.com/training/notify-user/build-notification?hl=en