notification.js 625 B

123456789101112131415161718192021
  1. function sendNotification() {
  2. new Notification('Notification title', {
  3. body: 'This is the notification body'
  4. })
  5. }
  6. document.getElementById('notification').addEventListener('click', function () {
  7. if (Notification.permission === 'default') {
  8. Notification.requestPermission().then(function (response) {
  9. if (response === 'granted') {
  10. sendNotification()
  11. } else {
  12. registerResponse('Permission is ' + response)
  13. }
  14. }).catch(registerResponse)
  15. } else if (Notification.permission === 'granted') {
  16. sendNotification()
  17. } else {
  18. registerResponse('Permission is denied')
  19. }
  20. })