Created
July 27, 2017 08:34
-
-
Save cristenicu/9777360305fb51ed596fa5d83a232f32 to your computer and use it in GitHub Desktop.
Web Push Notifications
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
var serviceWorkerRegistration; | |
if ('serviceWorker' in navigator) { | |
window.addEventListener('load', function () { | |
serviceWorkerRegistration = registerServiceWorker(); | |
}); | |
} | |
function registerServiceWorker() { | |
return navigator.serviceWorker.register('service_worker.js') | |
.then(function (registration) { | |
console.log('Service worker successfully registered.'); | |
askPermission(); | |
return registration; | |
}) | |
.catch(function (err) { | |
console.error('Unable to register service worker.', err); | |
}); | |
} | |
function askPermission() { | |
return new Promise(function (resolve, reject) { | |
const permissionResult = Notification.requestPermission(function (result) { | |
resolve(result); | |
}); | |
if (permissionResult) { | |
permissionResult.then(resolve, reject); | |
} | |
}) | |
.then(function (permissionResult) { | |
if (permissionResult !== 'granted') { | |
throw new Error('We weren\'t granted permission.'); | |
} else { | |
subscribeUserToPush(); | |
} | |
}); | |
} | |
function subscribeUserToPush() { | |
return serviceWorkerRegistration | |
.then(function (registration) { | |
const subscribeOptions = { | |
userVisibleOnly: true, | |
applicationServerKey: urlBase64ToUint8Array('BFTG0qcAZo1nFzwfsp1zIAX-YOw1PMG7Il2BnP7BebaCfdDGT21-G4prhzSuU5aW03i4mrYvkOF7yqLZmY8kYKk') | |
// Generate public/private key from here https://web-push-codelab.appspot.com/ | |
}; | |
return registration.pushManager.subscribe(subscribeOptions); | |
}) | |
.then(function (pushSubscription) { | |
// TODO: Send pushSubscription to backend | |
console.log('Received PushSubscription: ', JSON.stringify(pushSubscription)); | |
return pushSubscription; | |
}); | |
} | |
function urlBase64ToUint8Array(base64String) { | |
const padding = '='.repeat((4 - base64String.length % 4) % 4); | |
const base64 = (base64String + padding) | |
.replace(/\-/g, '+') | |
.replace(/_/g, '/'); | |
const rawData = window.atob(base64); | |
return Uint8Array.from([...rawData].map((char) => char.charCodeAt(0))); | |
} |
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('push', function (event) { | |
if (event.data) { | |
console.log('This push event has data: ', event.data.text()); | |
const promiseChain = self.registration.showNotification(event.data.text()); | |
event.waitUntil(promiseChain); | |
} else { | |
console.log('This push event has no data.'); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment