tosi
tosi

Reputation: 621

webpush notifications with service workers

I have a few questions regarding webpush notifications and service workers

How long does a granted permission last for a website?

Does the subscription object that contains the webpush endpoint and key change?

If it changes, how would I know it changed?

Should the subscription object be regenerated at any time?

Thanks very much

Upvotes: 9

Views: 832

Answers (1)

Varit J Patel
Varit J Patel

Reputation: 3520

Here is the answer to your questions!

  1. How long does a granted permission last for a website?

Always check for permission to use the Notifications API. It is important to keep checking that permission has been granted because the status may change. A user can manually disable permission hence checking every time to avoid breaking the code

  1. Does the subscription object that contains the web push endpoint and key change?
  2. If it changes, how would I know it changed?

Your server needs to generate a pair of application server keys — these are also known as VAPID keys, which are unique to your server. They are a pair of a public and a private key. The private key is secretly stored on your end, while the public one gets exchanged with the client. The keys allow a push service to know which application server subscribed a user and ensure that it’s the same server that triggers the push messages to that particular user.

You need to create the private/public key pair only once for your application. One way of doing it is going to https://web-push-codelab.glitch.me/

endpoint and public key will remain the same so it doesn't need to change.

  1. Should the subscription object be regenerated at any time?

When you want to send a push message to your users, the first thing you need is a push service. You’re telling the push service (via API call) what data to send, who to send the message to and any criteria about how to send the message. Normally, this API call is done from your server.

References are taken:

Official Google Developers docs: https://developers.google.com/web/ilt/pwa/introduction-to-push-notifications

https://blog.sessionstack.com/how-javascript-works-the-mechanics-of-web-push-notifications-290176c5c55d

Hope this helps!

Upvotes: 1

Related Questions