WebPush-and-photo-upload-fixes

This commit is contained in:
sysadmin
2025-09-01 06:01:05 +01:00
parent 5eb7647faf
commit c8a55c143b
16 changed files with 2000 additions and 6 deletions

View File

@@ -47,6 +47,80 @@ self.addEventListener('activate', (event) => {
);
});
// Push event - handle incoming push notifications
self.addEventListener('push', (event) => {
console.log('SW: Push notification received');
if (!event.data) {
console.log('SW: Push notification received without data');
return;
}
try {
const data = event.data.json();
console.log('SW: Push notification data:', data);
const notificationOptions = {
body: data.body || 'New notification',
icon: data.icon || '/icons/icon-192x192.png',
badge: data.badge || '/icons/icon-72x72.png',
tag: 'littleshop-notification',
requireInteraction: false,
silent: false,
data: data.data || {}
};
event.waitUntil(
self.registration.showNotification(data.title || 'LittleShop Admin', notificationOptions)
);
} catch (error) {
console.error('SW: Error parsing push notification data:', error);
// Show generic notification
event.waitUntil(
self.registration.showNotification('LittleShop Admin', {
body: 'New notification received',
icon: '/icons/icon-192x192.png',
badge: '/icons/icon-72x72.png',
tag: 'littleshop-notification'
})
);
}
});
// Notification click event - handle user interaction with notifications
self.addEventListener('notificationclick', (event) => {
console.log('SW: Notification clicked');
event.notification.close();
const notificationData = event.notification.data || {};
// Determine URL to open
let urlToOpen = '/Admin/Dashboard';
if (notificationData.url) {
urlToOpen = notificationData.url;
} else if (notificationData.orderId) {
urlToOpen = `/Admin/Orders/Details/${notificationData.orderId}`;
}
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true })
.then((clientList) => {
// Check if there's already a window/tab open with the target URL
for (const client of clientList) {
if (client.url.includes(urlToOpen) && 'focus' in client) {
return client.focus();
}
}
// If no window/tab is already open, open a new one
if (clients.openWindow) {
return clients.openWindow(urlToOpen);
}
})
);
});
// Fetch event - serve from cache, fallback to network
self.addEventListener('fetch', (event) => {
// Only handle GET requests