Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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!')
- })
- }
Add Comment
Please, Sign In to add comment