diff --git a/public/service-worker.js b/public/service-worker.js index 54724e0..f83e2be 100644 --- a/public/service-worker.js +++ b/public/service-worker.js @@ -23,7 +23,7 @@ self.addEventListener('push', (event) => { vibrate: [200, 100, 200], requireInteraction: true, data: { - url: notification.url + url: notification.url || null } }) ); @@ -32,17 +32,20 @@ self.addEventListener('push', (event) => { self.addEventListener('notificationclick', (event) => { event.notification.close(); + const url = event.notification.data && event.notification.data.url; + + if (!url) return; + event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { - // Check if there's already a tab open with this URL for (const client of clientList) { - if (client.url === event.notification.data.url && 'focus' in client) { + if (client.url === url && 'focus' in client) { return client.focus(); } } // If not, open a new window if (clients.openWindow) { - return clients.openWindow(event.notification.data.url); + return clients.openWindow(url); } }) );