Skip to content

Instantly share code, notes, and snippets.

@antonybudianto
Created February 15, 2019 08:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonybudianto/bf48d5eb4fc436f908fcfb411d98b7f6 to your computer and use it in GitHub Desktop.
Save antonybudianto/bf48d5eb4fc436f908fcfb411d98b7f6 to your computer and use it in GitHub Desktop.
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