I'm working with the JavaScript Notification API to show a small message to my users. It works on every desktop browser I've tested with, including Chrome...but not Chrome for Android (KitKat, at least). Everything I've read says that Android Chrome supports the Notification API as of April 2015, and the app even has settings for Notification permissions...yet nothing happens when I call new Notification(...) from within it. In fact, even the official Mozilla Notification API demo page doesn't show them, despite being able to grab permissions information, etc.
Is there something special I need to do to make Notifications compatible with Android Chrome?
I'm working with the JavaScript Notification API to show a small message to my users. It works on every desktop browser I've tested with, including Chrome...but not Chrome for Android (KitKat, at least). Everything I've read says that Android Chrome supports the Notification API as of April 2015, and the app even has settings for Notification permissions...yet nothing happens when I call new Notification(...) from within it. In fact, even the official Mozilla Notification API demo page doesn't show them, despite being able to grab permissions information, etc.
Is there something special I need to do to make Notifications compatible with Android Chrome?
Share Improve this question edited Dec 7, 2015 at 12:45 Kinlan 16.6k5 gold badges58 silver badges88 bronze badges asked Nov 22, 2015 at 20:45 IceMetalPunkIceMetalPunk 5,5564 gold badges20 silver badges28 bronze badges 3 |2 Answers
Reset to default 8On Chrome for Android you can only open a notification via a Service Worker. A service worker is a script that runs alongside the browser and can be loaded even when the page or browser are currently closed.
The main reasoning behind the service worker requirement is that a notification can outlive the length of the browser's lifetime (i.e, the user can close the browser) and the notification will still be active, therefore when a user clicks on the notification Android needs to wake "something" up, this "something" is the Service Worker.
This Notification Guide is a good introduction to the current state of Notifications on the Web and in Chrome - it focuses a little on Push messaging but also has all the details of how to trigger a notification from the Service Worker.
This might seem like an extra hoop to jump through but it actually is a net benefit: your notification will still work when clicked even when the browser is closed.
Check this for compatibility: http://caniuse.com/#feat=notifications
"Partial Support" Although as Kinlan mentioned, it's possible with Web Service Workers.
navigator.serviceWorker.register('sw.js');
Notification.requestPermission(function(result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification('Notification with ServiceWorker');
});
}
});
The sw.js
file can just be a zero-byte file.
Credit Due: HTML5 Notification not working in Mobile Chrome
Chrome Notification API
that provided for Chrome Extensions, andChrome Push Notification API
for Mobile usage. Here is an example for notification API for chrome apps. Before days ago they talked about Push Notification API in ChromeDevSummit and still there're some of limitations around. – Mohammed AlBanna Commented Nov 29, 2015 at 16:13