Fra*_* LG 5 javascript service-worker web-push
我创建了一个基本的应用程序,尝试使用 web-push 节点库发送推送通知。\n但是,我总是遇到错误:“你必须至少传入一个端点”\n我不明白,因为我“subscribe”(使用 ServiceWorker Api subscribe() 方法),它创建一个到我的本地主机地址的端点
\n\n事实上,我真的不知道该怎么做才能解决这个问题
\n\n这是我的“订阅”代码:
\n\nasync subscribe() {\n const sw = await navigator.serviceWorker.ready; \n const applicationServerPublicKey = \'BADCyHKPTCj1B9Knvmz2_EF1iLieHVZX2eEcCnMLvmMJA2ADlcDILgDC31ztGp1T1TznLpaJ14GQbdIM4APrr6k\';\n const applicationServerKey = this.urlB64ToUint8Array(applicationServerPublicKey);\n console.log("le service worker est " + sw);\n sw.pushManager.subscribe({\n userVisibleOnly: true,\n applicationServerKey: applicationServerKey,\n }).then(\n subscription => {\n console.log("User is subscribed with the subscription : ");\n console.log(subscription);\nRun Code Online (Sandbox Code Playgroud)\n\n在控制台上,我可以看到在“订阅”对象中有一个端点\n这里是发送通知的代码:
\n\nconsole.log("Juste avant d\'envoyer la notif on veut envoyer \xc3\xa0 :")\nconsole.log(this.state.endpoint)\n\nwebpush.sendNotification(\n this.state.endpoint,\n payload,\n options\n);\nRun Code Online (Sandbox Code Playgroud)\n\n但我进入控制台:
\n\n\n\n\nJuste avant d\'envoyer la notif on veut envoyer \xc3\xa0 :
\n
{\n "endpoint":"https://updates.push.services.mozilla.com/wpush/v2/gAAAAABdR_E1S96XSL7LdqLk2i5alH49Ae9v-XeOn1nX6Su9xkY6ETv16NybutJ5pBoA7q4nqHkNxFFJvVpP15JSsIiGPL8vQHSKAkyReS1CpdhTXc13IWllOb0qnFnfD5dFhNFqA9nIlJgAOz-rB3cy9v-AFbnhNh8S_Ea68WLVjIt8WKtPZWc",\n "keys": {\n "auth": "uXYi9oYQArgv_3Z1wD_gMQ",\n "p256dh": "BDpEEP6NWHFGNKY9PWx3wMzjirY98m4S4ntN0E8A0FlVZHZXaeCbc7WO0S0pqwju2SWM0-ivDkAvaFetPrBKMAU"\n }\n} \nRun Code Online (Sandbox Code Playgroud)\n\n\n\nindex.js:271
\n
\n 错误:您必须传入至少包含一个端点的订阅。
小智 1
打电话console.log(subscription.endpoint) 之前先做正确的事webPush.sendNotification(subscription, payload);。有同样的问题,并且 subscription.endpoint 确实没有设置。然后确保设置了 subscription.endpoint。