3

I am trying to subscribe to web push via the function below.

function postSubscriptionBo() {
  return navigator.serviceWorker.register('/service-worker.js')
  .then(function(registration) {
    const subscribeOptions = {
      userVisibleOnly: true,
      applicationServerKey: APPLICATION_SERVER_KEY
    };

    return registration.pushManager.subscribe(subscribeOptions);
  })
  .then(function(pushSubscription) {
    console.log('Received PushSubscription: ', JSON.stringify(pushSubscription));
    return pushSubscription;
  });
}

These are the current steps:
1. Get the application server key from Firebase console > cloud messaging tab > server key
2. Encoded application server key with window.btoa(server key)
3. Removed tailing = from the server key
4. Browser throws exception: The provided web push applicationServerKey is not valid.

Suggestions on what I am doing wrong here? Thanks.

antrunner
  • 87
  • 1
  • 6

1 Answers1

2

In the cloud messaging tab instead of using the server key, after generating a Web Push certificates public key (at the bottom of the same cloud messaging tab) and adding it instead of the server key, it worked.

antrunner
  • 87
  • 1
  • 6
  • 1
    But i got error `MismatchSenderId` when I run send message via Postman. ` curl --request POST \ --url https://fcm.googleapis.com/fcm/send \ --header 'Authorization: key=xxx' \ --header 'Content-Type: application/json' \ --data '{ "to": "xxx", "priority": "high", "notification": { "title": "Title", "body" : "First Notification", "text": "Text" } }' ` – Binh Ho Jul 19 '21 at 14:43