Comment #3 on issue 481856 by
joh...@chromium.org: new Notification() no
`new Notification()` is on the path to deprecation[1], because it
implicitly assumes that the page will outlive the notification, which is
very unlikely on mobile (and far from guaranteed on desktop too).
Hence we will never implement it on Android. We might one day remove it on
desktop too, after a deprecation period.
Websites should use ServiceWorkerRegistration.showNotification() instead
whenever it is available.
The best way I can think of to feature-detect `new Notification()` is to
try it (*before* you have permission) and catch the error:
function isNewNotificationSupported() {
if (!window.Notification || !Notification.requestPermission)
return false;
if (Notification.permission == 'granted')
throw new Error('You must only call this *before* calling
Notification.requestPermission(), otherwise this feature detect would bug
the user with an actual notification!');
try {
new Notification('');
} catch (e) {
if (
e.name == 'TypeError')
return false;
}
return true;
}
You could then use it like this:
if (window.Notification && Notification.permission == 'granted') {
// We would only have prompted the user for permission if new
// Notification was supported (see below), so assume it is supported.
doStuffThatUsesNewNotification();
} else if (isNewNotificationSupported()) {
// new Notification is supported, so prompt the user for permission.
showOptInUIForNotifications();
}
[1]:
https://github.com/whatwg/notifications/issues/26