-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
94 lines (80 loc) · 2.56 KB
/
Copy pathApp.js
File metadata and controls
94 lines (80 loc) · 2.56 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
import React, { useEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import Toast from 'react-native-toast-message';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import { LanguageProvider } from '@context/LanguageContext';
import { AuthProvider } from '@context/AuthContext';
import { NetworkProvider } from '@context/NetworkContext';
import {
navigationRef,
flushPendingNavigation,
} from '@navigation/navigationRef';
import RootNavigator from '@navigation';
import {
requestUserPermission,
getFcmToken,
onMessageListener,
onNotificationOpenedAppListener,
getInitialNotificationListener,
} from '@services/firebaseService';
import {
requestNotificationPermission,
createDefaultChannel,
} from '@services/notifications';
/* ---------------- ROOT APP ---------------- */
export default function App() {
useEffect(() => {
async function initNotifications() {
try {
// Request local notification permission
const permission = await requestNotificationPermission();
console.log('[App] Local notification permission:', permission);
// Create the reminders channel
await createDefaultChannel();
} catch (error) {
console.error('[App] Failed to initialize notifications:', error);
}
}
async function initFCM() {
try {
const enabled = await requestUserPermission();
if (enabled) {
const token = await getFcmToken();
console.log('Device FCM Token:', token);
}
} catch (error) {
console.error('[App] FCM initialization error:', error);
}
}
// Initialize both local and remote notifications
initNotifications();
initFCM();
const unsubscribeMessage = onMessageListener();
const unsubscribeOpen = onNotificationOpenedAppListener(remoteMessage => {
console.log('Opened from background:', remoteMessage);
});
getInitialNotificationListener().then(remoteMessage => {
if (remoteMessage) {
console.log('Opened from quit state:', remoteMessage);
}
});
return () => {
unsubscribeMessage();
unsubscribeOpen();
};
}, []);
return (
<AuthProvider>
<NetworkProvider>
<LanguageProvider>
<SafeAreaProvider>
<NavigationContainer ref={navigationRef} onReady={flushPendingNavigation}>
<RootNavigator />
<Toast />
</NavigationContainer>
</SafeAreaProvider>
</LanguageProvider>
</NetworkProvider>
</AuthProvider>
);
}