Files
gamercomp/frontend/public/push-handler.js
T
2026-04-30 02:14:25 +00:00

57 lines
1.4 KiB
JavaScript

// Push notification handler for GamerComp PWA
// This file is loaded by the service worker via importScripts
self.addEventListener('push', function(event) {
if (!event.data) return;
try {
const data = event.data.json();
const options = {
body: data.body || 'You have a new notification',
icon: '/icon.svg',
badge: '/icon.svg',
vibrate: [100, 50, 100],
data: {
url: data.url || '/'
},
actions: [
{ action: 'open', title: 'Open' }
]
};
event.waitUntil(
self.registration.showNotification(data.title || 'GamerComp', options)
);
} catch (err) {
// Fallback for non-JSON payloads
event.waitUntil(
self.registration.showNotification('GamerComp', {
body: event.data.text(),
icon: '/icon.svg'
})
);
}
});
self.addEventListener('notificationclick', function(event) {
event.notification.close();
const url = event.notification.data?.url || '/';
event.waitUntil(
clients.matchAll({ type: 'window', includeUncontrolled: true }).then(function(clientList) {
// Try to focus an existing window
for (const client of clientList) {
if (client.url.includes('gamercomp.com') && 'focus' in client) {
client.navigate(url);
return client.focus();
}
}
// Open new window if none exists
if (clients.openWindow) {
return clients.openWindow(url);
}
})
);
});