Подтвердить что ты не робот

Как отменить/отменить уведомление в строке состояния в программном обеспечении Android

Я создал уведомление о статусной строке в android программно, используя приведенный ниже код с конкретным id

    Notification notification;
    public static NotificationManager myNotificationManager;
    public static final int NOTIFICATION_ID = 1;

private static void createStatusBarNotification(Context context,CharSequence NotificationTicket, CharSequence NotificationTitle,CharSequence NotificationContent, int icon) {
            myNotificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
            long when = System.currentTimeMillis();
            notification = new Notification(icon, NotificationTicket, when);
            Intent notificationIntent = new Intent(context, MyActivity.class);
            PendingIntent contentIntent = PendingIntent.getActivity(context, 0,notificationIntent, 0);
            notification.setLatestEventInfo(context, NotificationTitle,NotificationContent, contentIntent);
            notification.flags |= Notification.FLAG_ONGOING_EVENT;
            myNotificationManager.notify(NOTIFICATION_ID, notification);

        }

Теперь я хочу удалить это уведомление при нажатии кнопки в приложении программно.

Как я могу это достичь?

4b9b3361

Ответ 1

Вы можете использовать это:

public void clearNotification() {
    NotificationManager notificationManager = (NotificationManager) mContext
            .getSystemService(Context.NOTIFICATION_SERVICE);
    notificationManager.cancel(NOTIFICATION_ID);
}

Ответ 2

myNotificationManager.cancelAll();

Ответ 3

Используйте тот же идентификатор, что и при уведомлении

 myNotificationManager.cancel(NOTIFICATION_ID)