尝试将推送通知发送到Chrome和Opera时,WebPushError和UnauthorizedRegistration可以正常运行 [英] WebPushError and UnauthorizedRegistration when try to send push notification to Chrome and Opera, Firefox is OK
问题描述
我设法运行了一个用于发送Web推送通知的示例-订阅用户推送通知,获取端点,并从订阅对象生成两个browser
密钥-p256dh
和auth
.在服务器端,我生成VAPID
密钥.
因此,通过所有这些,我在web-push
Node.js包上调用了sendNotification
,而且-我正在传递有效负载.
I managed to run a working example for sending web-push notifications - subscribe user for push notifications, get the endpoint, generate the two browser
keys from the subscription object - p256dh
and auth
. On the server side I generate the VAPID
keys.
So, with all these I call sendNotification
on the web-push
Node.js package, and also - I'm passing a payload.
在Firefox上-我收到带有有效负载的通知.
On Firefox - I get the notification with the payload.
在Chrome和Opera上,我会得到WebPushError: Received unexpected response code
以及UnauthorizedRegistration
和Error 400
.
On Chrome and Opera I get WebPushError: Received unexpected response code
and furthermore - UnauthorizedRegistration
and Error 400
.
我用来发送内容的服务器端代码是:
The server side code I'm using to send the stuff is:
// import our web-push package ..
var webPush = require('web-push');
webPush.setGCMAPIKey('MY_GCM_SENDER_ID');
// we generated the VAPID keys ..
var vapidKeys = {
"publicKey": "...",
"privateKey": "..."
};
// set our VAPID credentials ..
webPush.setVapidDetails(
'mailto:{my email}',
vapidKeys.publicKey,
vapidKeys.privateKey
);
var device_endpoint = "https://android.googleapis.com/gcm/send/...";
var device_key = "...";
var device_auth = "...";
/*
* Sending the notification ..
*/
webPush.sendNotification(
{
endpoint: device_endpoint,
keys: {
p256dh: device_key,
auth: device_auth
}
},
'My payload',
{
TTL: 86400, // 24 hours ..
}
)
.then(function() {
console.log('SUCCESS');
})
.catch(function(err) {
console.log('Unsuccessful');
console.log(err);
});
我也将MY_GCM_SENDER_ID作为gcm_sender_id
放置在manifest.json
文件中.
我从https://console.firebase.google.com/
中获取了它-创建了一个项目,并从Settings - Cloud Messaging
中获取了Sender ID
.
I also have put MY_GCM_SENDER_ID as gcm_sender_id
in manifest.json
file.
I took it from https://console.firebase.google.com/
- created a project and got the Sender ID
from Settings - Cloud Messaging
.
我在这里阅读的说明: https://firebase.google.com/docs/cloud-messaging/js/客户
Instructions for this I read here: https://firebase.google.com/docs/cloud-messaging/js/client
所以...有人能帮我找出我在做什么错吗?
So ... Could anyone help to identify what am I doing wrong?
推荐答案
我设法弄清楚了.现在,我成功发送了带有有效负载的PUSH
:)
I managed to figure it out. And now I'm sending PUSH
with payload successfully :)
问题是当我注册PUSH
时-我这样做是这样的:
The problem was that when I was registering for PUSH
- I was doing it like this:
reg.pushManager.subscribe({
userVisibleOnly: true
})
当我这样做时:
reg.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: new urlBase64ToUint8Array([VAPID public key)
})
然后工作正常.现在,我可以发送正常的PUSH或带有有效载荷的PUSH了,没有问题.
then it worked just fine. Now I'm able to send normal PUSH or PUSH with payload, with no problems.
更多信息:
- https://rossta.net/blog /using-the-web-push-api-with-vapid.html
- https://web-push- book.gauntface.com/chapter-02/01-subscribing-a-user/
- https://rossta.net/blog/using-the-web-push-api-with-vapid.html
- https://web-push-book.gauntface.com/chapter-02/01-subscribing-a-user/
关于所有告诉您仅使用userVisibleOnly
属性的教程...这是行不通的.我不知道他们为什么会推荐这个.
And regarding all the tutorials that tells you to use only the userVisibleOnly
property...that doesn't work. I don't know why they would be recommending this.
这篇关于尝试将推送通知发送到Chrome和Opera时,WebPushError和UnauthorizedRegistration可以正常运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!