【问题标题】:Error when sending web push Notification : no endpoint?发送网络推送通知时出错:没有端点?
【发布时间】:2019-08-14 13:09:08
【问题描述】:

我创建了一个基本应用程序,尝试使用 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
错误:您必须传入至少包含一个端点的订阅。

【问题讨论】:

    标签: javascript service-worker web-push


    【解决方案1】:

    console.log(subscription.endpoint) 之前拨打webPush.sendNotification(subscription, payload);。有同样的问题,subscription.endpoint 确实没有设置。然后确保设置了 subscription.endpoint。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-05-13
      相关资源
      最近更新 更多