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
| importScripts(‘/<Path to library file>.js’); | |
| self.addEventListener('push', (event) => { | |
| self._handlePushEvent(event); | |
| }); |
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
| self._handlePushEvent = (event) => { | |
| const data = event.data.json(); | |
| const promiseChain = self.registration.showNotification(data.title, { | |
| body: data.body, | |
| }); | |
| event.waitUntil(promiseChain); | |
| }; |
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
| self.addEventListener('push', (event) => { | |
| const data = event.data.json(); | |
| const promiseChain = self.registration.showNotification(data.title, { | |
| body: data.body, | |
| }); | |
| event.waitUntil(promiseChain); | |
| }); |
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
| const event = new NotificationEvent('notificationclose', { | |
| notification: notifications[0], | |
| }); |
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
| const event = new NotificationEvent('notificationclick', { | |
| notification: notifications[0], | |
| }); |
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
| return new Promise((resolve, reject) => { | |
| const event = new FetchEvent('fetch', { | |
| request: new Request('/index.html'), | |
| }); | |
| event.respondWith = (promiseChain) => { | |
| if (promiseChain) { | |
| // Check if promise was returned - otherise | |
| // it could be a response | |
| if (promiseChain instanceof Promise) { | |
| promiseChain.then(resolve, reject); |
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
| const event = new FetchEvent('fetch', { | |
| request: new Request('/index.html'), | |
| }); |
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
| // This could be in our test directly or imported using importScripts(); | |
| self.addEventListener('push', (event) => { | |
| const data = event.data.json(); | |
| const promiseChain = self.registration.showNotification(data.title, data); | |
| event.waitUntil(promiseChain); | |
| }); | |
| describe('First SW Test Suite', function() { | |
| it('should be able to test a push event', function() { |
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
| it('should be able to test a push event', function() { | |
| const pushData = { | |
| title: 'Example Title.', | |
| body: 'Example Body.', | |
| }; | |
| return new Promise((resolve, reject) => { | |
| const fakePushEvent = new PushEvent('push', { | |
| data: JSON.stringify(pushData), | |
| }); |
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
| self.dispatchEvent(fakePushEvent); |