• service-worker.js

  • Register event listener for the ‘push’ event.

    self.addEventListener('push', function(event) {
  • Retrieve the textual payload from event.data (a PushMessageData object). Other formats are supported (ArrayBuffer, Blob, JSON), check out the documentation on https://developer.mozilla.org/en-US/docs/Web/API/PushMessageData.

      var payload = event.data ? event.data.text() : 'no payload';
  • Keep the service worker alive until the notification is created.

      event.waitUntil(
  • Show a notification with title ‘ServiceWorker Cookbook’ and use the payload as the body.

        self.registration.showNotification('ServiceWorker Cookbook', {
          body: payload,
        })
      );
    });

Has it been useful?

Tell us what you think of this recipe by leaving a comment!