-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebaseConfig.js
More file actions
100 lines (83 loc) · 3.21 KB
/
Copy pathfirebaseConfig.js
File metadata and controls
100 lines (83 loc) · 3.21 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
import messaging from '@react-native-firebase/messaging';
import { patchNotification } from './src/apis/notification';
/* eslint-disable no-console */
export const setupFirebaseMessaging = (navigation, showToast) => {
// remoteMessage에 따라 navigate 처리
const navigateByNotification = async (remoteMessage) => {
console.log(remoteMessage);
const type = remoteMessage.data.type;
switch (type) {
case 'competition_invite':
const { screen: competitionScreen, competition_room_id } = remoteMessage.data;
if (competitionScreen && competition_room_id) {
navigation.navigate(competitionScreen, { competitionId: competition_room_id, isParticipant: false });
}
break;
case 'friends':
const { screen: friendScreen } = remoteMessage.data;
navigation.navigate(friendScreen, { initialState: 1 });
break;
}
await patchNotification(remoteMessage.data.notification_id, true);
};
// 푸시 알림 권한 요청
messaging()
.requestPermission()
.then((authStatus) => {
const enabled =
authStatus === messaging.AuthorizationStatus.AUTHORIZED ||
authStatus === messaging.AuthorizationStatus.PROVISIONAL;
if (enabled) {
console.log('Authorization status:', authStatus);
} else {
console.warn('Authorization status:', authStatus);
}
});
// 푸시 알림 수신 핸들러 설정
const unsubscribeOnMessage = messaging().onMessage(async (remoteMessage) => {
console.log('FCM Message Data:', remoteMessage);
showToast('새 알림이 도착했습니다!', 'success');
// 여기서 알림 처리 (예: UI 업데이트)
// 예: Alert.alert('푸시 알림', remoteMessage.notification.body);
});
// 백그라운드에서 메시지 수신 핸들러 설정
messaging().setBackgroundMessageHandler(async (remoteMessage) => {
console.log('FCM Background Message Data:', remoteMessage.data);
// 백그라운드에서 알림을 처리 (예: 로컬 알림 생성)
});
// 포그라운드에서 푸시 알림을 받았을 때
const unsubscribeOnNotificationOpenedApp = messaging().onNotificationOpenedApp((remoteMessage) => {
navigateByNotification(remoteMessage);
});
// 백그라운드에서 알림을 클릭하여 앱이 실행될 때
messaging()
.getInitialNotification()
.then((remoteMessage) => {
if (remoteMessage) {
navigateByNotification(remoteMessage);
}
});
// 푸시 알림 토큰 가져오기
const getToken = async () => {
try {
const token = await messaging().getToken();
console.log('FCM Token:', token);
// 서버에 토큰을 전송하여 푸시 알림을 전송할 수 있도록 설정
// 예: await sendTokenToServer(token);
} catch (error) {
console.error('Error getting FCM token:', error);
}
};
getToken();
// 토큰 갱신 처리
const unsubscribeOnTokenRefresh = messaging().onTokenRefresh((token) => {
console.log('Refreshed FCM Token:', token);
// 서버에 새로운 토큰을 업데이트
// 예: await updateTokenOnServer(token);
});
return {
unsubscribeOnMessage,
unsubscribeOnNotificationOpenedApp,
unsubscribeOnTokenRefresh,
};
};