HTML5 Notification not working in Mobile Chrome

后端 未结 4 1655
谎友^
谎友^ 2020-12-02 08:30

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 b

4条回答
  •  囚心锁ツ
    2020-12-02 08:55

    Running this code:

     if ('Notification' in window) {
      Notification.requestPermission();
    }
    

    Console in Chrome DevTools shows this error:

    Uncaught TypeError: Failed to construct ‘Notification’: Illegal constructor. Use ServiceWorkerRegistration.showNotification() instead

    A better approach might be:

    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;  
    }
    

    Function Source: HTML5Rocks

提交回复
热议问题