Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to construct Notification: Illegal constructor

My site uses desktop notifications which have never worked on mobile devices but I've recently started to receive the following exception in Chrome Version 42.0.2311.108 on Android 4.4:

Failed to construct 'Notification': Illegal constructor. Use ServiceWorkerRegistration.showNotification() instead. TypeError: Failed to construct 'Notification': Illegal constructor. Use ServiceWorkerRegistration.showNotification() instead. 

My notification code is simple, after checking if the user has granted permissions I initialize a new Notification object as follows:

var notification = new Notification(messageOptions.title, { icon: messageOptions.icon }); 

How do I change this code to use the ServiceWorkerRegistration.showNotification, which comes up as undefined, to support notifications in the mobile version of Chrome or if that isn't possible do a feature detection and prevent the exceptions from happening if this really isn't supported [yet].

like image 472
SignalRichard Avatar asked Apr 21 '15 14:04

SignalRichard


1 Answers

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(); } 
like image 129
Matt Gaunt Avatar answered Sep 20 '22 05:09

Matt Gaunt