mardi 24 avril 2018

Push API get endpoint from user

I am using Web Notifications to inform my users about changes on my website. They are working as long as the website is loaded, so now I want to take the next step and use the push API to display the notifications even if my website is not loaded. There is no app, just a website, it would be enough if it only works in chrome and messages don't have to be stored if the user's browser is currently closed.

I am following this answer How can a web application send push notifications to iOS devices? but am stuck on step 2 " you can read the device token (endpoint) which should be sent to the server and stored".

My understanding of the endpoint is the following:

  1. the user and his browser manufacturer generate a token and an url.
  2. Somehow, I get this URL and store it on my server.
  3. Anyone who knows this url can send push events to it, therefore the connection between the user and my website must be encrypted so nobody can hijack the url.
  4. When I want to send a notification to a user, I send a request to his respective url, the browser manufacturer forwards it to the user and the user executes the javascript code from the service worker.

I tried

navigator.serviceWorker.ready.then(function(reg) {
  reg.pushManager.subscribe({userVisibleOnly: true}).then(function(subscription) {
    console.log(subscription.endpoint);
  })
})

but receive the error "Uncaught (in promise) DOMException: Registration failed - missing applicationServerKey, and manifest empty or missing" which is comprehensible since there is no manifest.json file. However, I fail to see how I am involved in the token creation process, besides enabling SSL so that the user can securely send me the endpoint.

I also had a look at https://serviceworke.rs/push-subscription-management_index_doc.html but I'm not complety sure if this is what I need.




Aucun commentaire:

Enregistrer un commentaire