5

I have created a basic app trying to send Push Notifications, using the web-push node library. However, I am always confronted to the error :" You must at least pass in an endpoint" Which I do not understand, because I "suscribe" ( using the ServiceWorker Api subscribe() method ) , which create an endpoint to my localhost adress

In fact, I do not really know what to do to try to fix the problem

here is my code of "susbcribing" :

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);

On the console I can see that in the "subscription" object there is an endpoint and here is the code to send the notification :

console.log("Juste avant d'envoyer la notif on veut envoyer à  :")
console.log(this.state.endpoint)

webpush.sendNotification(
  this.state.endpoint,
  payload,
  options
);

but I get on the console :

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.

François LG
  • 63
  • 1
  • 6

2 Answers2

1

Do console.log(subscription.endpoint) right before calling webPush.sendNotification(subscription, payload);. Had the same issue and the subscription.endpoint was indeed not set. Then make sure the subscription.endpoint is set.

antrunner
  • 87
  • 1
  • 6
1

Error: You must pass in a subscription with at least an endpoint.

I encountered the same error, doing a hard reload (CTRL + SHIFT + R) of my web page solved my problem.

You can avoid caching of data also: https://stackoverflow.com/a/2068407/13248217 so that you need not hard reload every time.

Using HTML:

<meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
<meta http-equiv="Pragma" content="no-cache">
<meta http-equiv="Expires" content="0">
kuspia
  • 55
  • 8