fix(pwa+timezone)
This commit is contained in:
97
frontend/public/sw-custom.js
Normal file
97
frontend/public/sw-custom.js
Normal file
@@ -0,0 +1,97 @@
|
||||
// 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
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(title, {
|
||||
icon: '/icon-192x192.png',
|
||||
badge: '/icon-96x96.png',
|
||||
tag: 'lediscord-notification',
|
||||
requireInteraction: false,
|
||||
vibrate: [200, 100, 200],
|
||||
data: {
|
||||
link: options.link || '/',
|
||||
notificationId: options.data?.notificationId
|
||||
},
|
||||
...options
|
||||
})
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
// É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)
|
||||
}
|
||||
}
|
||||
|
||||
event.waitUntil(
|
||||
self.registration.showNotification(notificationData.title, notificationData)
|
||||
)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user