-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathApp.tsx
More file actions
87 lines (75 loc) · 2.73 KB
/
App.tsx
File metadata and controls
87 lines (75 loc) · 2.73 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
import React, { useEffect } from 'react';
import { Alert } from 'react-native';
import messaging from '@react-native-firebase/messaging';
import { NavigationContainer } from '@react-navigation/native';
import { SafeAreaProvider } from 'react-native-safe-area-context';
import RootNavigator from './src/navigation/RootNavigator';
import { requestPushPermission } from './src/api/alert/fcm/fcmPermissions';
import { getFcmToken } from './src/api/alert/fcm/fcmTokenManager';
import { displayLocalNotification } from './src/api/alert/utils/showLocalNotification';
import { requestLocationPermission } from './src/api/global/utils/PermissionUtil';
import { navigationRef } from './src/navigation/AppNavigation';
// 릴리스에서 에러만 출력
const logError = (label: string, error: any) => {
if (!__DEV__) {
console.error(`${label}:`, error?.message ?? error);
}
};
const App = () => {
useEffect(() => {
const initFCM = async () => {
try {
const granted = await requestPushPermission();
const location = await requestLocationPermission();
if (!granted) {
return;
}
if (!location) {
Alert.alert('위치 권한이 필요합니다. 앱을 종료합니다.');
return;
}
await getFcmToken();
} catch (e) {
logError('initFCM 실패', e);
}
};
const unsubscribeForeground = messaging().onMessage(async remoteMessage => {
await displayLocalNotification(remoteMessage);
});
// 앱이 백그라운드 상태에서 Notification 클릭
const unsubscribeBackground = messaging().onNotificationOpenedApp(remoteMessage => {
const highlightParticipantId = remoteMessage.data?.highlightParticipantId
? Number(remoteMessage.data.highlightParticipantId)
: null;
if (highlightParticipantId) {
navigationRef.current?.navigate('UserParticipation', { highlightParticipantId});
}
});
// 앱이 종료 상태에서 Notification 클릭
messaging()
.getInitialNotification()
.then(remoteMessage => {
const highlightParticipantId = remoteMessage?.data?.highlightParticipantId
? Number(remoteMessage.data.highlightParticipantId)
: null;
if (highlightParticipantId) {
setTimeout(() => {
navigationRef.current?.navigate('UserParticipation', { highlightParticipantId });
}, 500); // navigation 준비 대기
}
});
initFCM();
return () => {
unsubscribeForeground();
unsubscribeBackground();
};
}, []);
return (
<SafeAreaProvider>
<NavigationContainer ref={navigationRef}>
<RootNavigator />
</NavigationContainer>
</SafeAreaProvider>
);
};
export default App;