DOMException:注册失败 - 权限被拒绝

Har*_*nan 2 javascript google-chrome service-worker web-push

我有一个网络通知脚本,工作正常.但是当我在chrome版本43中测试时,我收到错误"DOMException:注册失败 - 权限被拒绝".订阅阶段发生此错误.

这是通常的注册过程的代码片段

navigator.serviceWorker.register('sw.js').then(function(reg) {
    console.log('[ServiceWorker] Registration Obj', reg);
    reg.pushManager.subscribe({
        userVisibleOnly: true
    }).then(function(sub) {
        //This never executes and catch gets called
        console.log('Subscription successful, Subscription endpoint:', sub.endpoint);
    }).catch(function(error) {
        console.log("Error during subscription ", error);
    });
}).catch(function(error) {
    if (Notification.permission === 'denied') {
        console.warn('Permission for Notifications was denied');
    } else {
        console.error('Unable to subscribe to push.', error);
    }
});
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,错误发生在我执行的步骤中reg.pushManager.subscribe().

  • 这不会发生在chrome 49中.
  • 此错误始终发生在chrome版本43上.

[注意]我认为它是铬版本43的一般问题但是当我检查像goroost.com和pushcrew这样的网站时,它们似乎工作正常.只有我的不工作

pra*_*tha 5

我遇到了类似的问题.你特别关注chrome 43版.

  1. 检查你的manifest.json.既然你已经告诉过它适用于更高版本,我认为你已经错过了清单中的某些东西.在此链接中,Matt Gaunt建议如果清单出现问题,可能会发生此错误.

  2. 确保最后没有尾随逗号.

  3. 此外,版本43不支持通知对象中的数据属性,并且不允许在单击通知时打开窗口,因此您还需要处理这些问题.


MJ *_*nto 5

我遇到过这个问题.除了prajnavantha答案之外,这可能是由于在阻止它时意外点击浏览器通知引起的.要在Chrome中修复此问题,请转到设置 - >点击"显示高级设置" - >在"隐私"下点击"内容设置" - >然后在"通知"下选择"管理摘要".进入此处后,请允许通知受影响的网址.