This file contains 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
interface PushSubscriptionChangeEvent extends ExtendableEvent { | |
readonly newSubscription?: PushSubscription; | |
readonly oldSubscription?: PushSubscription; | |
} |
This file contains 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('pushsubscriptionchange', (event: PushSubscriptionChangeEvent) => { | |
//Renew your subscription here. | |
}); |
This file contains 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
async handlePush(event: PushSubscriptionChangeEvent) { | |
const req = new Request('/refreshpushsubscription', { | |
method: 'POST', | |
body: JSON.stringify( | |
{oldSubscription: event.oldSubscription, newSubscription: event.newSubscription}) | |
}); | |
const response = await self.fetch(req); | |
} | |
self.addEventListener('pushsubscriptionchange', (event: PushSubscriptionChangeEvent) => |
This file contains 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
// To use IndexedDB like localStorage use this library. | |
// https://github.com/RyotaSugawara/serviceworker-storage | |
const storage = new ServiceWorkerStorage('sw:storage', 1); | |
async handlePush() { | |
const newSubscription = await self.registration.pushManager.getSubscription(); | |
const oldSubscription = JSON.parse(await storage.getItem('subscription')); | |
if(newSubscription.endpoint !== oldSubscription.endpoint) { | |
storage.setItem('subscription', newSubscription); | |
const req = new Request('/refreshpushsubscription', { |
This file contains 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
navigator.serviceWorker.ready.then(serviceWorkerRegistration => { | |
const options = {userVisibleOnly: true, applicationServerKey: applicationServerKey}; | |
serviceWorkerRegistration.pushManager.subscribe(options).then(pushSubscription => { | |
navigator.serviceWorker.controller.postMessage({ | |
action: 'REQUEST_SUBSCRIPTION', | |
subscription: pushSubscription | |
}); | |
uploadSubscriptionToTheServer(pushSubscription, userId); | |
}); | |
}); |
This file contains 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
navigator.serviceWorker.ready.then(serviceWorkerRegistration => { | |
const options = {userVisibleOnly: true, applicationServerKey: applicationServerKey}; | |
serviceWorkerRegistration.pushManager.subscribe(options).then(pushSubscription => { | |
// Send subscription to service worker to save it. | |
navigator.serviceWorker.controller.postMessage({ | |
action: 'REQUEST_SUBSCRIPTION', | |
subscription: pushSubscription | |
}); | |
// Upload first subscription to server using userId or any identifier | |
// to match user. |
This file contains 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
// environment.ts configuration | |
export const environment = { | |
production: false, | |
appVersion: '1.0' | |
endpoint: 'mysite.dev/rest', | |
browserSupport: [ | |
{ | |
name: 'Chrome', | |
version: 45 | |
}, |
This file contains 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
// global.ts | |
export const ENVIRONMENT_GLOBAL = { | |
appVersion: '1.0', | |
browserSupport: [ | |
{ | |
name: 'Chrome', | |
version: 45 | |
}, | |
{ | |
name: 'Firefox', |