0

I'm trying to follow Google's Web Push tutorial: https://developers.google.com/web/fundamentals/push-notifications/sending-messages-with-web-push-libraries

I have the node server set up with the ability to receive subscriptions. What I'm unsure about is how to send a subscription request from the client-side?

The tutorial only shows you how to make a post request, but what the hell is the payload you're actually sending? What is the endpoint that the server is checking for? This is not in the tutorial.

function sendSubscriptionToBackEnd(subscription) {
  return fetch('/api/save-subscription/', {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(subscription)
  })
  .then(function(response) {
    if (!response.ok) {
      throw new Error('Bad status code from server.');
    }

    return response.json();
  })
  .then(function(responseData) {
    if (!(responseData.data && responseData.data.success)) {
      throw new Error('Bad response from server.');
    }
  });
}

So when I do

sendSubscriptionToBackEnd(subscription)

What is subscription supposed to look like?

Harry
  • 52,711
  • 71
  • 177
  • 261
  • In the above call in the tutorial, the `sendSubscriptionToBackEnd` is your own custom backend , which is then saved to the database in `saveSubscriptionToDatabase`. I'd normally map such subscriptions to FCM topics though. There is no client-side API to subscribe to topics in the JavaScript SDK for Firebase Cloud Messaging. You'll have to subscribe from a trusted environment, using the REST API. For an example of the payload, see https://stackoverflow.com/questions/40389335/how-to-subscribe-to-topics-with-web-browser-using-firebase-cloud-messaging – Frank van Puffelen Aug 31 '20 at 14:23
  • @FrankvanPuffelen yes I have the backend, that's no problem, but what is the front end? I'm saving >what< into the database? I prefer to keep my own subscriptions rather than use firebase if possible. Firebase would be only for the actual final delivery. – Harry Aug 31 '20 at 15:55
  • Ah, in that case you need to store the FCM tokens in your own database. The Firebase documentation covers getting the token in https://firebase.google.com/docs/cloud-messaging/js/client#access_the_registration_token – Frank van Puffelen Aug 31 '20 at 16:11

0 Answers0