Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 659
tosi Avatar asked Nov 12 '18 20:11

tosi


People also ask

What are Webpush notifications?

Web push notifications are notifications that can be sent to a user via desktop web and mobile web.

How does a push notification service work?

A push notification is a message that pops up on a mobile device, such as a sports score, an invitation to a flash sale or a coupon for downloading. App publishers can send them at any time, since users don't have to be in the app or using their devices to receive them.

What are the types of push notifications?

Push notifications can consist of a title, a message, an image, and a URL. They can also include logos, emojis, and other elements. Push notifications look different across distinct operating systems, such as Google Android and Apple OS. Learn more about the design and anatomy of a push notification.


1 Answers

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!

like image 113
Varit J Patel Avatar answered Oct 22 '22 22:10

Varit J Patel