为什么Notification API在Android版chrome上无法使用?

时间:2018-12-08 10:55:35

标签: javascript google-chrome notifications

我正在使用WDN记录的W3C Notifications API。

但是,在Android Chrome上似乎提示用户允许/禁止通知请求,但是如果用户单击“允许”,则似乎什么也没有发生。 Android Chrome支持吗?

Notification.requestPermission(function (permission) {
    // If the user accepts, let's create a notification
    if (permission === "granted") {
        var options = {
            body: 'something in the body'
        }
        var n = new Notification('My title', options);
    }
}

然后我创建了一个minimun仓库来尝试一下: https://github.com/agrcrobles/minimum-web-notification

它部署在这里: https://minimum-web-notification-n8jmmz0fz.now.sh/

1 个答案:

答案 0 :(得分:0)

以下内容适用于Android chrome上的服务人员:

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

它必须从安全来源(https URL,而不是http URL)运行。

https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerRegistration/showNotification有更多信息。

https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/BygptYClroM包含为何在Android上的Chrome中不支持Notification构造函数的信息,尽管它仍在台式机Chrome中。