I'm using the HTML5 notification API to notify the user in Chrome or Firefox. On desktop browsers, it works. However in Chrome 42 for Android, the permission is requested but the notification itself is not displayed.
The request code, works on all devices:
if ('Notification' in window) {
Notification.requestPermission();
}
The sending code, works on desktop browser but not on mobile:
if ('Notification' in window) {
new Notification('Notify you');
}
Try the following:
That should work on Android both in Chrome and in Firefox Nightly (but not Firefox beta yet).
(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 anhttp
URL).https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification has more info.
https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/BygptYClroM has info about why the
Notification
constructor is not supported in Chrome on Android though it still is in desktop Chrome.Running this code:
Console in Chrome DevTools shows this error:
A better approach might be:
Function Source: HTML5Rocks