我已经创建了一个基本应用,尝试使用网络推送节点库发送推送通知。但是,我总是遇到错误:“您至少必须传递一个端点”这是我不理解的,因为我“订阅”(使用ServiceWorker Api subscription()方法),这创建了我的本地主机地址的端点
实际上,我真的不知道该如何解决该问题
这是我的“抄写”代码:
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:
{
"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错误:您必须传递至少包含一个端点的订阅。
执行console.log(subscription.endpoint)
就在之前调用webPush.sendNotification(subscription, payload);
。遇到相同的问题,并且未设置subscription.endpoint。然后确保已设置subscription.endpoint。