fix(front): try to correct crash on ios
Some checks failed
Deploy to Development / build-and-deploy (push) Has been cancelled
Deploy to Production / build-and-deploy (push) Successful in 1m39s

This commit is contained in:
EvanChal
2026-01-26 23:03:49 +01:00
parent 08810440e0
commit 658b7a9dda
10 changed files with 149 additions and 48 deletions

View File

@@ -130,55 +130,101 @@ class NotificationService {
// Gestion des notifications push PWA
async requestNotificationPermission() {
console.log('🔔 requestNotificationPermission appelée')
console.log('🔔 isIOS:', this.isIOS())
console.log('🔔 isPWAInstalled:', this.isPWAInstalled())
console.log('🔔 Notification API disponible:', 'Notification' in window)
console.log('🔔 Permission actuelle:', Notification.permission)
if (!this.isNotificationSupported()) {
console.log('Notifications not supported on this platform')
console.warn('⚠️ Notifications not supported on this platform')
if (this.isIOS()) {
if (!this.isPWAInstalled()) {
console.warn('⚠️ iOS: PWA must be installed (added to home screen)')
}
const iosVersion = navigator.userAgent.match(/OS (\d+)_(\d+)/)
if (iosVersion) {
const major = parseInt(iosVersion[1], 10)
const minor = parseInt(iosVersion[2], 10)
if (major < 16 || (major === 16 && minor < 4)) {
console.warn(`⚠️ iOS: Version ${major}.${minor} - Push notifications require iOS 16.4+`)
}
}
}
return false
}
if (Notification.permission === 'granted') {
console.log('Notification permission already granted')
console.log('Notification permission already granted')
return true
}
if (Notification.permission === 'denied') {
console.warn('Notification permission denied by user')
console.warn('Notification permission denied by user')
return false
}
// Sur iOS, s'assurer que la PWA est installée avant de demander
if (this.isIOS() && !this.isPWAInstalled()) {
console.warn('iOS: Cannot request notification permission - PWA must be installed first')
console.warn('⚠️ iOS: Cannot request notification permission - PWA must be installed first')
console.warn('⚠️ Instructions: Add the app to home screen, then open it from home screen')
return false
}
try {
console.log('🔔 Demande de permission...')
const permission = await Notification.requestPermission()
const granted = permission === 'granted'
console.log('🔔 Résultat de la demande:', permission)
if (granted) {
console.log('Notification permission granted')
console.log('Notification permission granted')
// Sur iOS, vérifier que le service worker est prêt
if (this.isIOS() && 'serviceWorker' in navigator) {
try {
const registration = await navigator.serviceWorker.ready
console.log('✅ Service worker ready on iOS')
// Tester une notification pour vérifier que ça fonctionne
await registration.showNotification('Test LeDiscord', {
body: 'Les notifications sont activées !',
icon: '/icon-192x192.png',
badge: '/icon-96x96.png',
tag: 'test-notification'
})
console.log('✅ Test notification sent successfully')
} catch (error) {
console.error('❌ Error testing notification on iOS:', error)
}
}
} else {
console.warn('Notification permission denied:', permission)
console.warn('Notification permission denied:', permission)
}
return granted
} catch (error) {
console.error('Error requesting notification permission:', error)
console.error('Error requesting notification permission:', error)
return false
}
}
async showPushNotification(title, options = {}) {
console.log('🔔 showPushNotification appelée:', { title, options })
if (!this.isNotificationSupported()) {
console.warn('Cannot show notification - not supported on this platform')
console.warn('⚠️ Cannot show notification - not supported on this platform')
return null
}
const hasPermission = await this.requestNotificationPermission()
if (!hasPermission) {
console.warn('Cannot show notification - permission not granted')
console.warn('⚠️ Cannot show notification - permission not granted')
return null
}
console.log('✅ Permission granted, affichage de la notification...')
// Préparer les options de notification (iOS ne supporte pas vibrate)
const notificationOptions = {
@@ -203,29 +249,46 @@ class NotificationService {
// Cela permet aux notifications de fonctionner même quand l'app est fermée
if ('serviceWorker' in navigator) {
try {
console.log('🔔 Tentative d\'affichage via service worker...')
const registration = await navigator.serviceWorker.ready
console.log('🔔 Service worker ready, active:', !!registration.active)
if (!registration.active) {
console.warn('Service worker not active, using fallback')
console.warn('⚠️ Service worker not active, using fallback')
throw new Error('Service worker not active')
}
// Envoyer un message au service worker pour afficher la notification
registration.active.postMessage({
type: 'SHOW_NOTIFICATION',
title,
options: notificationOptions
})
// Sur iOS, utiliser directement l'API du service worker
// (les messages peuvent ne pas fonctionner correctement)
if (this.isIOS()) {
console.log('🔔 iOS: Utilisation directe de showNotification')
await registration.showNotification(title, notificationOptions)
console.log('✅ Notification affichée via service worker (iOS)')
} else {
// Envoyer un message au service worker pour afficher la notification
registration.active.postMessage({
type: 'SHOW_NOTIFICATION',
title,
options: notificationOptions
})
// Aussi utiliser l'API directe du service worker
await registration.showNotification(title, notificationOptions)
console.log('✅ Notification affichée via service worker')
}
// Aussi utiliser l'API directe du service worker
await registration.showNotification(title, notificationOptions)
console.log('Notification shown via service worker')
return null
} catch (error) {
console.error('Error showing notification via service worker:', error)
console.error('Error showing notification via service worker:', error)
console.error('❌ Error details:', {
message: error.message,
stack: error.stack,
name: error.name
})
// Continuer avec le fallback
}
} else {
console.warn('⚠️ Service worker not available')
}
// Fallback: notification native du navigateur (seulement si le SW n'est pas disponible)
@@ -270,6 +333,23 @@ class NotificationService {
})
}
}
// Fonction de test pour vérifier que les notifications fonctionnent
async testNotification() {
console.log('🧪 Test de notification...')
const result = await this.showPushNotification('Test LeDiscord', {
body: 'Si vous voyez cette notification, les notifications push fonctionnent !',
link: '/'
})
if (result) {
console.log('✅ Test réussi - notification affichée')
return true
} else {
console.warn('⚠️ Test échoué - notification non affichée')
return false
}
}
}
export default new NotificationService()