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

👋 I'm Jim, a full-stack product engineer. Want to build an amazing product and a profitable business? Read more about me or Get in touch!

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