Automatically cancel a notification at a specific time

I am new to Java Coding and am currently coding an Android project. Now I am facing a problem. I want mine to applicationautomatically delete notificationat a specific time.

I managed to disconnect notificationafter the user clicks on notification. However, at the same time, I also want it to notificationautomatically disappear after a certain time if the user did not respond to notification.

Please advise me how to do this. If possible, give me some examples.

+4
source share
4 answers

, , onFinish() - :

NotificationManager nMgr = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
nMgr.cancelAll();
+1

- , , , :

 //clear all pending notifications
        String ns = Context.NOTIFICATION_SERVICE;
        NotificationManager nMgr = (NotificationManager) context.getSystemService(ns);
        nMgr.cancelAll();
0

Timer Notification cancel()

nMgr.cancel(<notification-id>); // here you have to pass your notification id.

,

nMgr.cancelAll();

2 , :

    Timer timer=new Timer();
    TimerTask task=new TimerTask() {

        @Override
        public void run() {
            nMgr.cancel(notification-id);
        }
    };

    timer.schedule(task, 2000);
0

. miliseconds . .

 Handler h = new Handler();
long delayInMilliseconds = 5000;
h.postDelayed(new Runnable() {
    public void run() {
        mNotificationManager.cancel(YourNotificationId);
    }
}, delayInMilliseconds);
0

Source: https://habr.com/ru/post/1621582/


All Articles