// Service Worker personnalisé pour gérer les notifications push // Ce fichier sera fusionné avec le service worker généré par vite-plugin-pwa // Écouter les événements de notification self.addEventListener('notificationclick', (event) => { console.log('Notification clicked:', event.notification) event.notification.close() // Récupérer le lien depuis les données de la notification const link = event.notification.data?.link || event.notification.data?.url || '/' // Ouvrir ou focus la fenêtre/clients event.waitUntil( clients.matchAll({ type: 'window', includeUncontrolled: true }).then((clientList) => { // Si une fenêtre est déjà ouverte, la focus for (let i = 0; i < clientList.length; i++) { const client = clientList[i] if (client.url && 'focus' in client) { // Naviguer vers le lien si nécessaire if (link && !client.url.includes(link.split('/')[1])) { return client.navigate(link).then(() => client.focus()) } return client.focus() } } // Sinon, ouvrir une nouvelle fenêtre if (clients.openWindow) { return clients.openWindow(link || '/') } }) ) }) // Écouter les messages du client pour afficher des notifications self.addEventListener('message', (event) => { console.log('Service Worker received message:', event.data) if (event.data && event.data.type === 'SHOW_NOTIFICATION') { const { title, options } = event.data // Préparer les options de notification const notificationOptions = { icon: '/icon-192x192.png', badge: '/icon-96x96.png', tag: 'lediscord-notification', requireInteraction: false, data: { link: options.link || options.data?.link || '/', notificationId: options.data?.notificationId || options.notificationId }, body: options.body || options.message || '', ...options } // Retirer vibrate si présent (iOS ne le supporte pas) // Le client devrait déjà l'avoir retiré, mais on s'assure ici aussi if (notificationOptions.vibrate) { // Vérifier si on est sur iOS (approximatif via user agent du client) // Note: dans le SW on n'a pas accès direct à navigator.userAgent // mais on peut retirer vibrate de toute façon car iOS l'ignore delete notificationOptions.vibrate } event.waitUntil( self.registration.showNotification(title, notificationOptions).catch(error => { console.error('Error showing notification in service worker:', error) }) ) } }) // Écouter les push events (pour les vraies push notifications depuis le serveur) self.addEventListener('push', (event) => { console.log('Push event received:', event) let notificationData = { title: 'LeDiscord', body: 'Vous avez une nouvelle notification', icon: '/icon-192x192.png', badge: '/icon-96x96.png', tag: 'lediscord-notification', data: { link: '/' } } // Si des données sont envoyées avec le push if (event.data) { try { const data = event.data.json() notificationData = { ...notificationData, title: data.title || notificationData.title, body: data.body || data.message || notificationData.body, data: { link: data.link || '/', notificationId: data.notificationId } } } catch (e) { console.error('Error parsing push data:', e) } } // Retirer vibrate pour compatibilité iOS if (notificationData.vibrate) { delete notificationData.vibrate } event.waitUntil( self.registration.showNotification(notificationData.title, notificationData).catch(error => { console.error('Error showing push notification:', error) }) ) })