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

时间:2010-04-13 18:45:40

标签: android notifications

如果我想在通知栏中显示通知问题。虽然我将通知标记设置为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 个答案:

答案 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)

删除通知

在以下情况之一发生之前​​,通知一直保持可见:

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

有关更多详细信息,请参见: https://developer.android.com/training/notify-user/build-notification?hl=en