1

I'm building a web app that has a service worker, which displays a notification when a message is sent. I've been using the Web Push API example (https://web-push-codelab.glitch.me/) as it's backend. But I'm not clear on how notifications are sent to a service worker. Do I need to create a backend service that pushes out notifications, and as part of the registration of the service worker, I have to subscribe to an endpoint that the backend server provides. Also do I need a 3rd party service that sends out the notification? That my backend service talks to? I know how to setup a service worker, but not how to create the service that the Push Manager subscribes to.

StephenAdams
  • 521
  • 2
  • 9
  • 26
  • Basically you need to activate the service worker with a push message, then use the callback in the service worker to display the notification. **The push message can be delivered only through the browser push service** (which is different for each browser). See also this [introduction to web push](https://pushpad.xyz/docs/web_push_intro). – collimarco Oct 31 '17 at 15:30
  • Thanks, but how do I send a notification message from the backend so that it is picked up by the service worker? I'd like to be able to send a message to our client, even if it is closed. – StephenAdams Oct 31 '17 at 16:05
  • You need to **make an HTTP POST request from your backend to the browser push service** (e.g. Mozilla autopush for Firefox). **Then it's the browser push service that will deliver the push message to your service worker** (which will be activated). You can contact the browser push service using the device *endpoint* (you can get it using `pushManager.subscribe()`). Search "W3C Push API" for more details. – collimarco Oct 31 '17 at 16:59

0 Answers0