diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/App.tsx | 5 | ||||
-rw-r--r-- | src/RootNavigation.ts | 15 | ||||
-rw-r--r-- | src/index.ts | 1 | ||||
-rw-r--r-- | src/services/FCMService.ts | 54 |
4 files changed, 37 insertions, 38 deletions
diff --git a/src/App.tsx b/src/App.tsx index 92d26ba7..18fadf64 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -4,11 +4,12 @@ import Routes from './routes'; import {Provider} from 'react-redux'; import store from './store/configureStore'; import {fcmService} from './services/FCMService'; +import {navigationRef} from './RootNavigation'; const App = () => { useEffect(() => { fcmService.setUpPushNotifications(); - //If permissions are not there, deactivateFcmService + // TODO: If permissions are not there, deactivateFcmService }); return ( @@ -16,7 +17,7 @@ const App = () => { * This is the provider from the redux store, it acts as the root provider for our application */ <Provider store={store}> - <NavigationContainer> + <NavigationContainer ref={navigationRef}> <Routes /> </NavigationContainer> </Provider> diff --git a/src/RootNavigation.ts b/src/RootNavigation.ts new file mode 100644 index 00000000..827177a3 --- /dev/null +++ b/src/RootNavigation.ts @@ -0,0 +1,15 @@ +import {NavigationContainerRef} from '@react-navigation/native'; +import * as React from 'react'; + +export const navigationRef: React.RefObject<NavigationContainerRef> = React.createRef(); + +export function navigate(name: string) { + if (navigationRef.current) { + // Perform navigation if the app has mounted + //console.log('Reached root navigation'); + navigationRef.current.navigate(name); + } else { + // TODO: Decide what to do if the app hasn't mounted + // Ignore this, or add these actions to a queue you can call later + } +} diff --git a/src/index.ts b/src/index.ts index ab7fd11d..e8e7e50f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ export {default} from './App'; +export * from './RootNavigation'; diff --git a/src/services/FCMService.ts b/src/services/FCMService.ts index 11cb7510..b6cd18af 100644 --- a/src/services/FCMService.ts +++ b/src/services/FCMService.ts @@ -3,7 +3,7 @@ import messaging from '@react-native-firebase/messaging'; import {Platform} from 'react-native'; import {getDeviceId, getDeviceName} from 'react-native-device-info'; import {FCM_ENDPOINT} from '../constants'; - +import * as RootNavigation from '../RootNavigation'; class FCMService { setUpPushNotifications = () => { // Requesting user to permit notifications @@ -27,7 +27,7 @@ class FCMService { // }); // // Send local notification when app in foreground since remote notifications - // // aren't displayed when app is in the foreground + // // aren't displayed when app is in the foreground // PushNotification.localNotification({ // //... You can use all the options from localNotifications // message: 'My Notification Message', // (required) @@ -123,48 +123,30 @@ class FCMService { }; deactivateFcmService = async () => { - //Make PATCH call to deactivate device + // TODO: Make PATCH call to deactivate device console.log('Deactivating FCM device'); }; createNotificationListeners = () => { - // messaging().onNotificationOpenedApp((remoteMessage) => { - // console.log( - // '[FCMService] onNotificationOpenedApp Notification caused app to open', - // ); - // if (remoteMessage) { - // const notification = remoteMessage.notification; - // onOpenNotification(notification); - // } - // }); - - // messaging() - // .getInitialNotification() - // .then((remoteMessage) => { - // console.log( - // '[FCMService] getInitialNotification Notification caused app to open', - // ); - - // if (remoteMessage) { - // const notification = remoteMessage.notification; - // onOpenNotification(notification); - // } - // }); - - messaging().onMessage((remoteMessage) => { - console.log('Received a remote notification!!'); + // Called when app is opened from backrground state + messaging().onNotificationOpenedApp((remoteMessage) => { if (remoteMessage) { - let notification = remoteMessage.notification; - let notificationId = remoteMessage.messageId; - console.log( - 'notificationsId: ', - notificationId, - ' notification: ', - notification, - ); + // TODO: Get {name, params} of screen when user must be redirected to + // Redirected to Notification Screen for now + const redirectTo = 'Notifications'; + /* TODO: Check login status and redirect user/store screen to async as + initialRoute for NavigationBar Stack */ + RootNavigation.navigate(redirectTo); } }); + messaging().onMessage((remoteMessage) => { + console.log( + 'Received a remote notification!!', + remoteMessage.notification?.body, + ); + }); + messaging().onTokenRefresh((fcmToken) => { AsyncStorage.setItem('@fcmToken', fcmToken).catch((err) => { console.log('Failed to store new token!'); |