Created
September 7, 2020 13:00
-
-
Save bahuma20/2d875912f52fcee95cf81ec01f488107 to your computer and use it in GitHub Desktop.
Push Notification aktualisieren
This file contains 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
let reg; | |
navigator.serviceWorker.register('/service_worker.js').then(registration => { | |
console.log('service worker registered'); | |
reg = registration; | |
}).catch(e => console.error(e)); | |
let timeremaing = 10; | |
let interval; | |
document.getElementById('send').addEventListener('click', e => { | |
Notification.requestPermission().then(result => { | |
// Show the initial notification. | |
reg.showNotification('Live in ' + timeremaing + ' Minuten: MoinMoin', { | |
body: 'Mit Florentin', | |
tag: 'notification_123', | |
}); | |
// Add an interval that updates the notification once every minute (could be changed to every 5 minutes for example). | |
interval = setInterval(() => { | |
timeremaing--; | |
if (timeremaing == 0) { | |
// Clear the interval and change the text to "Jetzt live". | |
clearInterval(interval); | |
reg.showNotification('Jetzt live: MoinMoin', { | |
body: 'Mit Florentin', | |
tag: 'notification_123', | |
}); | |
// Add timeout that removes the notification after the show has ended | |
setTimeout(() => { | |
reg.getNotifications({ | |
tag: 'notification_123', | |
}).then(notifications => { | |
notifications.forEach(notification => { | |
notification.close(); | |
}); | |
}) | |
}, 5000) // The duration of the show. | |
} else { | |
// This updates the existing notification, as the tag is the same. | |
reg.showNotification('Live in ' + timeremaing + ' Minuten: MoinMoin', { | |
body: 'Mit Florentin', | |
tag: 'notification_123', | |
}); | |
} | |
}, 1000); // Should be 60.000 (every minute). 1000 for testing. | |
}); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment