diff --git a/readme.md b/readme.md index d3436a8..8eaba0b 100644 --- a/readme.md +++ b/readme.md @@ -4,12 +4,16 @@ ## How to get the Service Worker on my Meteor App ? 1. Download sw.js and put it in the **root** of the /public folder. -2. Register your Service Worker to the client in your client : - `Meteor.startup(() => { +2. Register your Service Worker to the client in your client: + +``` +Meteor.startup(() => { navigator.serviceWorker.register('/sw.js') .then() .catch(error => console.log('ServiceWorker registration failed: ', error)); - });` +}); +``` + Service Workers are only available to **secure origins**. So be sure your server has https (localhost is considered as secure). And that the website you made request to are considered as secure. So your subdomain or your CDN must have HTTPS enabled. ## How it works diff --git a/sw.js b/sw.js index f1a3c90..46e6ba8 100644 --- a/sw.js +++ b/sw.js @@ -72,9 +72,17 @@ self.addEventListener('fetch', (event) => { }); }) ); - } - +}); + +self.addEventListener('push', function (event) { + const data = event.data.json(); // Assumes JSON bodies + const options = { + body: data.body, + // icon: '/image/icon/192.png', + // badge: '/image/badge/192.png', + }; + event.waitUntil(self.registration.showNotification(data.title, options)); }); function removeHash(element) {