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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2632272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 06:40:24  来源:igfitidea点击:

Android notification doesn't disappear after clicking the notifcation

androidnotifications

提问by Flo

If got some issues with a notification I want to show in the notification bar. Although I set the notification flag to Notification.DEFAULT_LIGHTS & Notification.FLAG_AUTO_CANCELthe notification doesn't disappear after clicking it. Any ideas what I'm doing wrong?

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

回答by Kamil Lelonek

While building Notificationby NotificationBuilderyou can use notificationBuilder.setAutoCancel(true);.

在构建时NotificationNotificationBuilder您可以使用notificationBuilder.setAutoCancel(true);.

回答by synic

notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

From the documentation:

从文档:

Bit to be bitwise-ored into the flags field that should be set if the notification should be canceled when it is clicked by the user

如果在用户单击通知时应取消通知,则按位编入标志字段的位应设置该位

回答by Darcy

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

回答by artdias90

2016 state: you can use mBuilder.setAutoCancel(true).

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

Source: https://developer.android.com/reference/android/app/Notification.Builder.html

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

回答by Machine Tribe

The answer for me was to use mBuilder.setOngoing(false)

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

回答by Sachin Suthar

Use the flag Notification.FLAG_AUTO_CANCEL

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

and to launch the app:

并启动应用程序:

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

回答by Angel

Remove a notification

删除通知

Notifications remain visible until one of the following happens:

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

  1. The user dismisses the notification.
  2. The user clicks the notification, and you called setAutoCancel() when you created the notification.
  3. You call cancel() for a specific notification ID. This method also deletes ongoing notifications.
  4. You call cancelAll(), which removes all of the notifications you previously issued.
  5. If you set a timeout when creating a notification using setTimeoutAfter(), the system cancels the notification after the specified duration elapses. If required, you can cancel a notification before the specified timeout duration elapses.
  1. 用户取消通知。
  2. 用户单击通知,并在创建通知时调用了 setAutoCancel()。
  3. 您为特定的通知 ID 调用 cancel()。此方法还会删除正在进行的通知。
  4. 您调用 cancelAll(),它会删除您之前发出的所有通知。
  5. 如果您在使用 setTimeoutAfter() 创建通知时设置了超时,系统会在指定的持续时间过后取消通知。如果需要,您可以在指定的超时持续时间结束之前取消通知。

For more details see: https://developer.android.com/training/notify-user/build-notification?hl=en

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