Уведомление Android не исчезает после нажатия на уведомление - PullRequest
120 голосов
/ 13 апреля 2010

Если возникли проблемы с уведомлением, я хочу показать его в панели уведомлений. Хотя я установил флаг уведомления 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);

Ответы [ 6 ]

278 голосов
/ 31 мая 2013

При построении Notification на NotificationBuilder вы можете использовать notificationBuilder.setAutoCancel(true);.

129 голосов
/ 13 апреля 2010
notification.flags = Notification.DEFAULT_LIGHTS | Notification.FLAG_AUTO_CANCEL

Из документации:

Бит, который должен быть побитовым, - или , введенный в поле флагов, которое должно быть установлено, если уведомление должно быть отменено при нажатиипользователем

27 голосов
/ 12 октября 2011
// 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;
13 голосов
/ 23 июня 2016

2016 состояние: вы можете использовать mBuilder.setAutoCancel(true).

Источник: https://developer.android.com/reference/android/app/Notification.Builder.html

1 голос
/ 11 июня 2018

Ответом для меня было использовать mBuilder.setOngoing(false)

1 голос
/ 07 октября 2016

Используйте флаг 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);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...