Skip to content

Commit

Permalink
fix: checks if notification is granted
Browse files Browse the repository at this point in the history
  • Loading branch information
ArnaudLigny committed Apr 3, 2023
1 parent 54ae8b7 commit 0658c16
Showing 1 changed file with 18 additions and 5 deletions.
23 changes: 18 additions & 5 deletions layouts/serviceworker.js.twig
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,25 @@ self.addEventListener('activate', function(event) {
const options = {
icon: '{{ asset(site.manifest.icons|first) }}'
};
self.registration.showNotification(title, options);
self.addEventListener('notificationclick', function (event) {
const clickedNotification = event.notification;
clickedNotification.close();
});
// Notification granted?
if (Notification && Notification.permission === 'granted') {
notification(title, options);
} else if (Notification && Notification.permission !== 'denied') {
Notification.requestPermission(function (status) {
if (status === 'granted') {
notification(title, options);
}
});
}
// Notification function
function notification(title, options) {
self.registration.showNotification(title, options);
self.addEventListener('notificationclick', function (event) {
const clickedNotification = event.notification;
clickedNotification.close();
//event.waitUntil(clients.openWindow('{{ site.baseurl }}'));
});
}
{%- endif ~%}
// Flush cache
console.log(`[SW] Remove old cache '${key}'`);
Expand Down

0 comments on commit 0658c16

Please # to comment.