Created
February 15, 2019 08:04
-
-
Save antonybudianto/bf48d5eb4fc436f908fcfb411d98b7f6 to your computer and use it in GitHub Desktop.
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 registerServiceWorker = async () => { | |
const swRegistration = await navigator.serviceWorker.register( | |
'/service-worker.js' | |
) | |
return swRegistration | |
} | |
const requestNotificationPermission = async () => { | |
const permission = await window.Notification.requestPermission() | |
// value of permission can be 'granted', 'default', 'denied' | |
// granted: user has accepted the request | |
// default: user has dismissed the notification permission popup by clicking on x | |
// denied: user has denied the request. | |
if (permission !== 'granted') { | |
throw new Error('Permission not granted for Notification') | |
} | |
} | |
registerServiceWorker().then(() => { | |
requestNotificationPermission() | |
}) | |
if (Notification.permission == 'granted') { | |
navigator.serviceWorker.getRegistration().then(function(reg) { | |
reg.showNotification('Hello world!') | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment