【问题标题】:Web push notifications in chromechrome 中的网络推送通知
【发布时间】:2018-11-03 18:25:31
【问题描述】:

我已经创建了网络推送通知,它们在 Firefox 中正常工作(服务工作者已注册,订阅已创建,订阅数据已存储,使用订阅数据通知已发送正常)。我在 Chrome 和 Opera 中尝试过相同的方法,但没有任何反应。我尝试调试,发送推送通知后,浏览器收到它,执行代码,但没有任何反应。没有错误,代码运行到最后。 Service Worker 代码如下:

'use strict';
self.addEventListener('push', function(event) {
  console.log('Push started');
  const promiseChain = self.registration.showNotification('Hello, World.');

  event.waitUntil(promiseChain);
  console.log('Push finished');
});

我在控制台中看到“推送开始”和“推送完成”。服务器使用 https。有什么想法,有什么问题?

【问题讨论】:

  • 我不知道它在 Firefox 中是如何工作的,所以我可能听起来很愚蠢,但是您是否记得向用户请求允许向他们发送通知? developers.google.com/web/fundamentals/codelabs/…
  • 是的,我在订阅前请求允许发送通知。当我检查权限时,它显示允许通知。可能那是因为我在网页中请求许可,而不是在服务人员中。它是这样发生的: function askp() { return new Promise(function(resolve, reject) { const permissionResult = Notification.requestPermission(function(result) { resolve(result); }); if (permissionResult) { permissionResult.then(解决, 拒绝); } }) .then(function(permissionResult) { // 获取订阅 }); }

标签: javascript google-chrome web-push


【解决方案1】:

假设您有一个没有 https 的开发域。 Google Chrome 仅在使用已配置的 SSL 运行时发送通知。

如何绕过:对于本地开发,您可以使用 unsafely-treat-insecure-origin-as-secure 选项启动 Chrome。

open -a /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome \
     --args \
     --user-data-dir=$HOME \
     --unsafely-treat-insecure-origin-as-secure=http://your-insecure-domain.dev \

【讨论】:

    【解决方案2】:

    我已经在我的应用程序中配置了 SSL 并使用 https 运行它。直到我在 chrome 中没有收到任何通知,但在 firefox 和 edge 中它的工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      • 1970-01-01
      • 1970-01-01
      • 2019-12-22
      相关资源
      最近更新 更多