Files
cat-sitting-planner/public/push-sw.js

44 lines
1.3 KiB
JavaScript

self.addEventListener('push', function (event) {
if (!event.data) return;
try {
const data = event.data.json();
const title = data.title || 'Cat Sitting Planner';
const options = {
body: data.body || '',
icon: '/icon.png',
badge: '/icon.png',
data: {
url: data.url || '/'
}
};
event.waitUntil(
self.registration.showNotification(title, options)
);
} catch (err) {
console.error('Error processing push event:', err);
}
});
self.addEventListener('notificationclick', function (event) {
event.notification.close();
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(windowClients => {
const urlToOpen = event.notification.data.url;
// Check if there is already a window/tab open with the target URL
for (let i = 0; i < windowClients.length; i++) {
const client = windowClients[i];
if (client.url === urlToOpen && 'focus' in client) {
return client.focus();
}
}
// If not, open a new window
if (clients.openWindow) {
return clients.openWindow(urlToOpen);
}
})
);
});