HTML5 Notification not working in Mobile Chrome

后端 未结 4 1653
谎友^
谎友^ 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:53

    Try the following:

    navigator.serviceWorker.register('sw.js');
    Notification.requestPermission(function(result) {
      if (result === 'granted') {
        navigator.serviceWorker.ready.then(function(registration) {
          registration.showNotification('Notification with ServiceWorker');
        });
      }
    });
    

    That should work on Android both in Chrome and in Firefox (and on iOS in Safari, too).

    (The sw.js file can just be a zero-byte file.)

    One caveat is that you must run it from a secure origin (an https URL, not an http URL).

    See https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification.

提交回复
热议问题