new Notification(...) is deprecated

Earlier I showed this example of the Notification API:

Notification.requestPermission(perm => new Notification('HEY!!'));

Unfortunately this uses a deprecated feature. The window.Notification constructor is not marked as deprecated! However, it’s marked as deprecated in Chrome on Android. The Brave New World is service-worker notifications, which are triggered like this:

Notification.requestPermission(perm =>
  navigator.serviceWorker.ready.then(reg => reg.showNotification("HEY!!")));

In other words, ServiceWorkerRegistration.showNotification is the replacement for new Notification. For this to work, you need to install a service worker. I’ve updated the service worker at /service-worker.js so that I/you can run:

Notification.requestPermission((perm) => {
  if (perm == "granted") {
    window.navigator.serviceWorker.ready.then(reg =>
      reg.active.postMessage({
        method: "notify",
        title: "HEY!!",
        options: {body: "This is some extra text under the title."}
      }));
  }
});
Tagged .

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.