From 3f2006270e2f8ad8f83b84fd7835d621b8a2d01b Mon Sep 17 00:00:00 2001 From: Sungho Park <43461389+psh320@users.noreply.github.com> Date: Tue, 12 Jul 2022 02:09:05 +0900 Subject: [PATCH] Feat: receive FCM token --- App.tsx | 23 ++++++++++++++++++++++- src/api/login.ts | 11 ++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/App.tsx b/App.tsx index 9eb2ab4..1d4ae51 100644 --- a/App.tsx +++ b/App.tsx @@ -10,7 +10,19 @@ import 'react-native-gesture-handler'; import {enableScreens} from 'react-native-screens'; import AsyncStorage from '@react-native-async-storage/async-storage'; import {QueryClient, QueryClientProvider} from 'react-query'; -import {getRegisterStatus, postToken} from './src/api'; +import {getRegisterStatus, postFcmToken, postToken} from './src/api'; +import messaging from '@react-native-firebase/messaging'; + +async function requestUserPermission() { + const authStatus = await messaging().requestPermission(); + const enabled = + authStatus === messaging.AuthorizationStatus.AUTHORIZED || + authStatus === messaging.AuthorizationStatus.PROVISIONAL; + + if (enabled) { + console.log('Authorization status:', authStatus); + } +} enableScreens(); @@ -24,6 +36,15 @@ export default function App() { // 실행하면 가장 먼저 로컬에 로그인 정보 있는지 확인 useEffect(() => { getToken(); + + requestUserPermission(); + + messaging() + .getToken() + .then((token) => { + return postFcmToken(token); + }); + const id = setTimeout(() => { setLoading(false); }, 2000); diff --git a/src/api/login.ts b/src/api/login.ts index 4502680..5e3fd3c 100644 --- a/src/api/login.ts +++ b/src/api/login.ts @@ -33,6 +33,15 @@ export const getRegisterStatus = async () => { const response = await customAxios().get('/api/v1/users/me/register-status'); return response.data.result.registerStatus; } catch (error) { - console.log('register Status', error); + console.log('회원가입 상태 정보 받기 실패', error); + } +}; + +export const postFcmToken = async (token: string) => { + try { + const response = await customAxios().post('/api/v1/fcm/me', token); + return response.data; + } catch (error) { + console.log('파이어베이스 토큰 받기 실패', error); } };