Guest User

Untitled

a guest
Feb 17th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. const registerServiceWorker = async () => {
  2. const swRegistration = await navigator.serviceWorker.register(
  3. '/service-worker.js'
  4. )
  5. return swRegistration
  6. }
  7.  
  8. const requestNotificationPermission = async () => {
  9. const permission = await window.Notification.requestPermission()
  10. // value of permission can be 'granted', 'default', 'denied'
  11. // granted: user has accepted the request
  12. // default: user has dismissed the notification permission popup by clicking on x
  13. // denied: user has denied the request.
  14. if (permission !== 'granted') {
  15. throw new Error('Permission not granted for Notification')
  16. }
  17. }
  18.  
  19. registerServiceWorker().then(() => {
  20. requestNotificationPermission()
  21. })
  22.  
  23. if (Notification.permission == 'granted') {
  24. navigator.serviceWorker.getRegistration().then(function(reg) {
  25. reg.showNotification('Hello world!')
  26. })
  27. }
Add Comment
Please, Sign In to add comment