6

I'm trying to piece together the general workflow of giving a user push notifications via the service worker.

I have followed this Google Developers service worker push notifications tutorial and am currently thinking about how I can implement this sort of thing in a small user based web app for experimentation.

In my mind, the general workflow of an web app supporting push notifications is as follows:

  • Client visits app
  • Service worker yields a push notification endpoint
  • Client sends the endpoint to the server
  • Server associates the endpoint with the current user that the endpoint was generated for
  • Every time something that your app would say is notification worthy happens, the server grabs the push notification endpoint(s) associated with the user, and hits it to send a push notification to any user devices (possibly with a data payload in Chrome 50+, etc)

Basically I just want to confirm that my general implementation thoughts with this technology are accurate, else get feedback if I am missing something.

2
  • Sorry, it's not clear what your question is, could you rephrase where is there is confusion / concern? Cheers, Matt
    – Matt Gaunt
    Apr 5, 2016 at 18:27
  • @GauntFace Sorry! I reworded for clarity. I just want to see if my thoughts on implementing this in an app are accurate, or if there is some simpler way of doing it Apr 5, 2016 at 19:05

1 Answer 1

8

You are pretty much bang on, there are some specifics that aren't quite right (but this is largely phrasing and may be done to personally taste).

  • Client visits app
  • Register a Service Worker that you want to use for push messaging
  • Use the service worker registration to subscribe the user to push messaging, at which point the user agent will configure an endpoint + additional values for encrypting payloads (If the the user agent supports it).
  • Client sends the endpoint to the server
  • Server store the the endpoint and data for later use (The server can associate the endpoint with the current user if the server if the web app has user accounts).
  • When ever the server wishes to send a notification to a user(s), it grabs the appropriate endpoints and calls them that will wake up the service worker which can then display a notification.

Payload support in coming in Chrome 50+ and at the time of writing payload is support in Firefox, but there are 3 different versions of encryption used for the payloads in 3 different versions of Firefox, so I'd wait for the payload support story to be ironed out a little before using it / relying on it.

3
  • Thanks! Push notification endpoints are associated to a specific service worker correct? One thing I'm confused about is, say a user loads the site on two different devices (generates two different push notification endpoints that the server keeps). Say a site update comes in and a new service worker gets registered (new push notification endpoint generated), Now the user has potentially 4 endpoints associated with them in a database, but only two valid ones...this pattern continues and we've got 50 endpoints/user with only a couple being "up to date"...seems conducive to tons of leaking Apr 7, 2016 at 20:29
  • It seems kind of tough to dispose of the right endpoint as a new one gets generated Apr 7, 2016 at 20:30
  • Few things on this: - A user agent may not necessarily issue a new push subscription (Endpoint + other info) on a new service worker - When you send a push message using a push subcription, the response will tell you if the subscription is still valid or not - in which case you can clear out old ones - If it becomes a concern / problem, it is possible to track the previously none subscription using something like indexdb and detect the change and remove the old push subscription with the new
    – Matt Gaunt
    Apr 7, 2016 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.