Created
February 20, 2020 12:10
-
-
Save SuryaSankar/e9d0adbadda0a9ee7cc909610778873d to your computer and use it in GitHub Desktop.
Basic Service worker
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 'use strict'; | |
| /* eslint-enable max-len */ | |
| self.addEventListener('install', function(event) { | |
| console.log('Service Worker installing.'); | |
| }); | |
| self.addEventListener('activate', function(event) { | |
| console.log('Service Worker activating.'); | |
| }); | |
| self.addEventListener('push', function(event) { | |
| console.log('[Service Worker] Push Received.'); | |
| const pushData = event.data.text(); | |
| console.log(`[Service Worker] Push received this data - "${pushData}"`); | |
| let data, title, body; | |
| try { | |
| data = JSON.parse(pushData); | |
| title = data.title; | |
| body = data.body; | |
| } catch(e) { | |
| title = "Untitled"; | |
| body = pushData; | |
| } | |
| const options = { | |
| body: body | |
| }; | |
| console.log(title, options); | |
| event.waitUntil( | |
| self.registration.showNotification(title, options) | |
| ); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment