-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.tsx
More file actions
314 lines (261 loc) · 10.9 KB
/
App.tsx
File metadata and controls
314 lines (261 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
import './app/constants/translations'
import 'react-native-gesture-handler'
import { Alert, ToastAndroid } from 'react-native'
import * as Clipboard from 'expo-clipboard'
import React, { useEffect, useRef, useState, createContext, useContext } from 'react'
import * as Notifications from 'expo-notifications'
import * as Device from 'expo-device'
import { Platform } from 'react-native'
import * as SplashScreen from 'expo-splash-screen'
import * as TaskManager from 'expo-task-manager'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { DataProvider } from './app/hooks'
import AppNavigation from './app/navigation/App'
import { View, Text, Image, StyleSheet, FlatList } from 'react-native'
import Menu from './app/navigation/Menu'
import 'intl-pluralrules'
import Constants from 'expo-constants'
import { LogBox } from 'react-native'
import Toast from 'react-native-toast-message'
import * as eva from '@eva-design/eva'
import { ApplicationProvider } from '@ui-kitten/components'
import { KeyboardProvider } from 'react-native-keyboard-controller'
import { createNavigationContainerRef } from '@react-navigation/native'
// Créer une référence de navigation globale
export const navigationRef = createNavigationContainerRef()
// 🆕 Créer un contexte pour le token push
interface PushTokenContextType {
expoPushToken: string
}
const PushTokenContext = createContext<PushTokenContextType>({ expoPushToken: '' })
export const usePushToken = () => useContext(PushTokenContext)
// 🆕 Importer le contexte de notification en attente
import { PendingNotificationProvider, usePendingNotification } from './app/context/PendingNotificationContext'
LogBox.ignoreAllLogs() // si tu veux ignorer les warnings
// Pour capturer les erreurs globales :
ErrorUtils.setGlobalHandler((error, isFatal) => {
console.log('❌ Erreur non capturée : ', error, 'Fatal: ', isFatal)
})
SplashScreen.preventAutoHideAsync()
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
}),
})
// 🆕 Définir le nom de la tâche en arrière-plan
const BACKGROUND_NOTIFICATION_TASK = 'BACKGROUND-NOTIFICATION-TASK'
// 🆕 Définir la tâche en arrière-plan pour les notifications
TaskManager.defineTask(BACKGROUND_NOTIFICATION_TASK, async ({ data, error, executionInfo }) => {
console.log('🔔 Background notification task triggered!')
console.log('📦 Data:', data)
console.log('⚙️ Execution info:', executionInfo)
if (error) {
console.error('❌ Background task error:', error)
return
}
if (data) {
const notification = data as any
console.log('📬 Background notification received:', notification)
// Vous pouvez ajouter ici une logique personnalisée
// Par exemple: sauvegarder dans AsyncStorage, mettre à jour un badge, etc.
// Note: Vous ne pouvez PAS naviguer directement depuis une tâche en arrière-plan
// La navigation se fera quand l'utilisateur tapera sur la notification
}
})
export default function App() {
const [expoPushToken, setExpoPushToken] = useState('')
return (
<PendingNotificationProvider>
<PushTokenContext.Provider value={{ expoPushToken }}>
<AppContent expoPushToken={expoPushToken} setExpoPushToken={setExpoPushToken} />
</PushTokenContext.Provider>
</PendingNotificationProvider>
)
}
interface AppContentProps {
expoPushToken: string
setExpoPushToken: (token: string) => void
}
// 🆕 Clé AsyncStorage pour les notifications traitées
const PROCESSED_NOTIFICATIONS_KEY = '@processed_notifications'
function AppContent({ expoPushToken, setExpoPushToken }: AppContentProps) {
const [channels, setChannels] = useState<Notifications.NotificationChannel[]>(
[],
)
const [notification, setNotification] = useState<
Notifications.Notification | undefined
>(undefined)
const notificationListener = useRef<Notifications.EventSubscription>()
const responseListener = useRef<Notifications.EventSubscription>()
// 🆕 Tracker pour éviter de retraiter les mêmes notifications
// Utilise une clé métier au lieu de l'ID Expo pour permettre le retry
const processedNotificationKeys = useRef<Set<string>>(new Set())
// 🆕 Utiliser le contexte de notification en attente (file d'attente)
const { addNotification, clearQueue } = usePendingNotification()
// 🆕 Charger les clés traitées depuis AsyncStorage au démarrage
useEffect(() => {
const loadProcessedKeys = async () => {
try {
const stored = await AsyncStorage.getItem(PROCESSED_NOTIFICATIONS_KEY)
if (stored) {
const keys = JSON.parse(stored) as string[]
processedNotificationKeys.current = new Set(keys)
console.log(`📂 ${keys.length} notifications déjà traitées chargées depuis le stockage`)
}
} catch (error) {
console.error('❌ Erreur chargement notifications traitées:', error)
}
}
loadProcessedKeys()
}, [])
// 🆕 Fonction centralisée pour gérer la navigation depuis les notifications
// IMPORTANT: Doit être définie AVANT le useEffect
const handleNotificationResponse = (response: Notifications.NotificationResponse) => {
const notificationId = response.notification.request.identifier
// Extraire les données de la notification
const notificationData = response.notification.request.content.data
console.log('🆔 Notification Expo ID:', notificationId)
console.log('📦 Notification Data:', notificationData)
// Vérifier si des données sont présentes
if (notificationData && Object.keys(notificationData).length > 0) {
console.log('✅ Data présente dans la notification')
// Vérifier si un écran est spécifié
if (notificationData.screen) {
console.log(`🎯 Écran cible: ${notificationData.screen}`)
console.log('📋 Paramètres:', notificationData)
// 🆕 Utiliser l'ID Expo comme clé unique (chaque notification est unique)
console.log('🔑 Clé unique:', notificationId)
// 🆕 Vérifier si cette notification a déjà été traitée avec succès
if (processedNotificationKeys.current.has(notificationId)) {
console.log('⏭️ Notification déjà traitée avec succès, ignorée')
return
}
// 🆕 Ajouter la notification à la file d'attente
// Marquer comme traitée APRÈS navigation réussie
addNotification(notificationData as any, async () => {
console.log('✅ Navigation réussie, marquage de la notification comme traitée')
processedNotificationKeys.current.add(notificationId)
// 🆕 Sauvegarder dans AsyncStorage pour persistence
try {
const keys = Array.from(processedNotificationKeys.current)
await AsyncStorage.setItem(PROCESSED_NOTIFICATIONS_KEY, JSON.stringify(keys))
console.log(`💾 ${keys.length} notifications traitées sauvegardées`)
} catch (error) {
console.error('❌ Erreur sauvegarde notifications traitées:', error)
}
})
} else {
console.log('⚠️ Pas de paramètre "screen" dans les données')
}
} else {
console.log('⚠️ Aucune data dans la notification')
}
}
useEffect(() => {
registerForPushNotificationsAsync().then(
(token) => token && setExpoPushToken(token),
)
if (Platform.OS === 'android') {
Notifications.getNotificationChannelsAsync().then((value) =>
setChannels(value ?? []),
)
}
// 🆕 Enregistrer la tâche en arrière-plan pour les notifications
Notifications.registerTaskAsync(BACKGROUND_NOTIFICATION_TASK)
.then(() => {
console.log('✅ Background notification task registered')
})
.catch((error) => {
console.error('❌ Error registering background task:', error)
})
// 🆕 Vérifier si l'app a été ouverte par une notification (état fermé)
// IMPORTANT: Ceci est appelé UNE SEULE FOIS au démarrage
Notifications.getLastNotificationResponseAsync()
.then((response) => {
if (response) {
console.log('🔔 App ouverte depuis une notification (état fermé):', response)
handleNotificationResponse(response)
}
})
.catch((error) => {
console.error('❌ Erreur getLastNotificationResponseAsync:', error)
})
notificationListener.current = Notifications.addNotificationReceivedListener(
(notification) => {
console.log('📬 Notification reçue (foreground):', notification)
setNotification(notification)
},
)
responseListener.current = Notifications.addNotificationResponseReceivedListener(
(response) => {
console.log('📱 Notification Response (app active/background):', response)
handleNotificationResponse(response)
},
)
return () => {
if (notificationListener.current) {
notificationListener.current.remove()
}
if (responseListener.current) {
responseListener.current.remove()
}
}
}, [addNotification])
return (
<>
<PushTokenContext.Provider value={{ expoPushToken }}>
<ApplicationProvider {...eva} theme={eva.light}>
<DataProvider>
<AppNavigation />
</DataProvider>
<Toast />
</ApplicationProvider>
</PushTokenContext.Provider>
</>
)
}
// Dans votre App.js ou service de notifications
const registerForPushNotificationsAsync = async () => {
try {
// Vérifier si nous sommes dans un environnement approprié (pas Expo Go)
if (!Device.isDevice) {
console.log(
'Doit utiliser un appareil physique pour les notifications push',
)
return null
}
if (Platform.OS === 'android') {
await Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#A127417C',
})
}
const { status: existingStatus } = await Notifications.getPermissionsAsync()
let finalStatus = existingStatus
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync()
finalStatus = status
}
if (finalStatus !== 'granted') {
console.log("Échec de l'obtention du token push pour les notifications !")
return null
}
const projectId =
Constants?.expoConfig?.extra?.eas?.projectId ??
Constants?.easConfig?.projectId
if (!projectId) {
throw new Error('Project ID non trouvé dans la configuration')
}
const token = (await Notifications.getExpoPushTokenAsync({ projectId }))
.data
console.log('Token push obtenu:', token)
return token
} catch (error) {
console.error("Erreur lors de l'enregistrement des notifications:", error)
return null
}
}