我创建了一个基本的应用程序,尝试使用 web-push 节点库发送推送通知。
但是,我总是遇到错误:“您至少必须传入一个端点”
我不明白,因为我“订阅”(使用 ServiceWorker Api subscribe() 方法),它为我的本地主机地址创建了一个端点
事实上,我真的不知道该怎么做才能解决问题
这是我的“订阅”代码:
async subscribe() {
const sw = await navigator.serviceWorker.ready;
const applicationServerPublicKey = 'BADCyHKPTCj1B9Knvmz2_EF1iLieHVZX2eEcCnMLvmMJA2ADlcDILgDC31ztGp1T1TznLpaJ14GQbdIM4APrr6k';
const applicationServerKey = this.urlB64ToUint8Array(applicationServerPublicKey);
console.log("le service worker est " + sw);
sw.pushManager.subscribe({
userVisibleOnly: true,
applicationServerKey: applicationServerKey,
}).then(
subscription => {
console.log("User is subscribed with the subscription : ");
console.log(subscription);
在控制台上,我可以看到在“订阅”对象中有一个端点
这是发送通知的代码:
console.log("Juste avant d'envoyer la notif on veut envoyer à :")
console.log(this.state.endpoint)
webpush.sendNotification(
this.state.endpoint,
payload,
options
);
但我进入控制台:
Juste avant d'envoyer la notif on veut envoyer à :
{
"endpoint":"https://updates.push.services.mozilla.com/wpush/v2/gAAAAABdR_E1S96XSL7LdqLk2i5alH49Ae9v-XeOn1nX6Su9xkY6ETv16NybutJ5pBoA7q4nqHkNxFFJvVpP15JSsIiGPL8vQHSKAkyReS1CpdhTXc13IWllOb0qnFnfD5dFhNFqA9nIlJgAOz-rB3cy9v-AFbnhNh8S_Ea68WLVjIt8WKtPZWc",
"keys": {
"auth": "uXYi9oYQArgv_3Z1wD_gMQ",
"p256dh": "BDpEEP6NWHFGNKY9PWx3wMzjirY98m4S4ntN0E8A0FlVZHZXaeCbc7WO0S0pqwju2SWM0-ivDkAvaFetPrBKMAU"
}
}
index.js:271
Error: You must pass in a subscription with at least an endpoint.
最佳答案
做 console.log(subscription.endpoint)
就在之前 调用 webPush.sendNotification(subscription, payload);
.有同样的问题,并且确实没有设置 subscription.endpoint 。然后确保设置了 subscription.endpoint。
关于javascript - 发送网页推送通知 : no endpoint? 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57495393/