Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide a push notification after some time?

I have to hide a push notification after 1 minute. What should I do in my service worker to achieve this?

like image 755
Supermacy Avatar asked Sep 12 '25 20:09

Supermacy


1 Answers

You can use the Notification.close() method. You can get the Notification object with ServiceWorkerRegistration.getNotifications.

For example:

self.addEventListener('push', event => {
  event.waitUntil(
    self.registration.showNotification('Title', {
      body: 'Body.',
    })
    .then(() => self.registration.getNotifications())
    .then(notifications => {
      setTimeout(() => notifications.forEach(notification => notification.close()), 60000);
    })
  );
});
like image 149
Marco Castelluccio Avatar answered Sep 15 '25 19:09

Marco Castelluccio