Failed to construct Notification: Illegal constructor Failed to construct Notification: Illegal constructor google-chrome google-chrome

Failed to construct Notification: Illegal constructor


See crbug.com/481856 on the Chrome issue tracker:

new Notification() is on the path to deprecation, 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() (see spec) 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();}