3

Once you get permission from a user to show a browser notification, there seem to be two ways to do it.

The simple way:

new Notification(title, options);

The complicated way:

navigator.serviceWorker.register(pathToServiceWorkerScript)
  .then(function (registration) {
    registration.showNotification(title, options);
  });

In the simplest case, these seem to do the same thing. But from what I've read, you can't put action buttons in the notification unless you go the complicated service worker route. This entails writing a whole service worker script.

I'd like to be able to show action buttons without a service worker. I'm having a hard time understanding service workers and they seem way too overwrought for my purposes. The only thing they have that I want is custom buttons on a notification. Is there any way to do this without service workers?

John Alexander
  • 822
  • 1
  • 12
  • 21
  • You found the difference? – hldev Aug 10 '22 at 17:08
  • 1
    It seems like `new Notification()` is going to be deprecated eventually: https://stackoverflow.com/a/29915743/5744858. "new Notification() is on the path to deprecation, because it implicitly assumes that the page will outlive the notification, which is very unlikely on mobile (and far from guaranteed on desktop too)". – Adrian Apr 03 '23 at 20:17

0 Answers0