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 . All content copyright James Fisher 2017. This post is not associated with my employer.