无法订阅推送通知 [英] Can't subscribe to Push notification
问题描述
我正在尝试订阅PushManager来发送推送通知,但是在调用subscribe时我总是收到错误。
I'm trying to subscribe to PushManager to send push notification, but I'm always getting an error when calling subscribe.
这是我的订阅电话
registration.pushManager.subscribe({ userVisibleOnly: true })
.then(function (subscription) { /* ... */})
.catch(function (err) { /* ... */})
你看不出任何奇怪的东西,而且这个电话总是在抛出
Nothing weird as you can see, and that call is always throwing
DOMException:注册失败 - 推送服务错误
DOMException: Registration failed - push service error
我不知道为什么会这样。
我到目前为止检查了什么
I'm not sure why is that. What I've checked so far
- 我有一个
gcm_sender_id
我的manifest.json
中的属性。 - 通知的权限在客户端授予。
-
registration.pushManager.getSubscription()
总是得到null
并且订阅总是抛出上述错误。
- I have a
gcm_sender_id
property in mymanifest.json
. - The permision for notification is granted in the client.
registration.pushManager.getSubscription()
always getnull
and subscribe always throw the above error.
什么我应该看看吗?如果您需要更多信息,请使用chrome 55,欢迎任何成功订阅推送通知的帮助。
What else should I look? Any help to succesfully subscribe to push notifications is welcome, by the way, I'm using chrome 55, if you need more info just let me know.
编辑:所有注册码均为这里 registerServiceWorker
函数。
EDIT: All of the registration code is here on the registerServiceWorker
function.
谢谢。
推荐答案
假设您已正确注册服务工作者,请使用以下内容:
Assuming you have registered the service worker properly, using something like :
navigator.serviceWorker.register('/serviceworker.js', {
scope: '/'
});
我建议你像我一样使用服务工作者的相对路径,而不是使用窗口.location.origin 就像你在代码中所做的那样,最好将serviceworker文件保存在 root 。
I suggest you use a relative path for your service worker like I did instead of using window.location.origin like you did in your code and it is always better to keep the serviceworker file at the root.
使用此方法订阅push并获取端点。
Use this approach to subscribe for push and get an endpoint.
navigator.serviceWorker.ready
.then(function(serviceWorkerRegistration) {
return serviceWorkerRegistration.pushManager.subscribe({
userVisibleOnly: true
});
})
.then(function(subscription) {console.log(subscription.endpoint);});
如果仍然无效:
- 尝试从chrome取消注册现有员工: // serviceworker-internals并确保在执行此操作时关闭所有选项卡。
If it still doesn't work: -Try unregistering the existing worker from chrome://serviceworker-internals and make sure you close all tabs when you do that.
如果仍然无效:
- 尝试创建新的chrome点击Chrome右上角的名称即可找到个人资料。 (我曾经面对这一次)
If it still doesn't work: -Try creating a new chrome profile by clicking on your name in the top right corner of chrome. (I faced this once)
检查这代码供您参考。
这篇关于无法订阅推送通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!