Skip to content

Instantly share code, notes, and snippets.

@bahuma20
Created September 7, 2020 13:00
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 bahuma20/2d875912f52fcee95cf81ec01f488107 to your computer and use it in GitHub Desktop.
Save bahuma20/2d875912f52fcee95cf81ec01f488107 to your computer and use it in GitHub Desktop.
Push Notification aktualisieren
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