Try the following:
navigator.serviceWorker.register('sw.js');
Notification.requestPermission(function(result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification('Notification with ServiceWorker');
});
}
});
That should work on Android both in Chrome and in Firefox (and on iOS in Safari, too).
(The sw.js
file can just be a zero-byte file.)
One caveat is that you must run it from a secure origin (an https
URL, not an http
URL).
See https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…