diff --git a/src/app/(BottomTabNavigation)/AllCases/index.tsx b/src/app/(BottomTabNavigation)/AllCases/index.tsx index 205d43ad..183bac22 100644 --- a/src/app/(BottomTabNavigation)/AllCases/index.tsx +++ b/src/app/(BottomTabNavigation)/AllCases/index.tsx @@ -1,6 +1,4 @@ -// import * as Notifications from 'expo-notifications'; -// import { router } from 'expo-router'; -import React, { useEffect, useRef } from 'react'; +import React from 'react'; import { FlatList, Text, View } from 'react-native'; import styles from './styles'; @@ -12,38 +10,9 @@ import { device } from '../../../styles/global'; import 'react-native-url-polyfill/auto'; -// Notifications.setNotificationHandler({ -// handleNotification: async () => ({ -// shouldShowAlert: true, -// shouldPlaySound: false, -// shouldSetBadge: false, -// }), -// }); - function CasesScreen() { - // const responseListener = useRef(); - const { allCases, loading } = useCaseContext(); - // function routeUserToUpdate(response: Notifications.NotificationResponse) { - // const updateId = response.notification.request.content.data.updateId; - // const caseId = response.notification.request.content.data.caseId; - // router.push(`/AllCases/CaseScreen/${caseId}`); - // router.push(`/AllCases/Updates/${caseId}`); - // router.push(`/AllCases/Updates/UpdateView/${updateId}`); - // } - - // useEffect(() => { - // // triggered when a user presses on the notification - // responseListener.current = - // Notifications.addNotificationResponseReceivedListener(response => { - // routeUserToUpdate(response); - // }); - - // return () => - // Notifications.removeNotificationSubscription(responseListener.current!); - // }, []); - return ( diff --git a/src/app/index.tsx b/src/app/index.tsx index 6af753a7..91a6fa3a 100644 --- a/src/app/index.tsx +++ b/src/app/index.tsx @@ -15,15 +15,16 @@ function StartScreen() { if (_event !== 'USER_UPDATED') { resetAndPushToRoute('/AllCases'); } + + // generate a new push token on sign in + if (_event === 'SIGNED_IN') { + registerForPushNotifications().then(async (token: string) => { + updatePushToken(session.user.id, token); + }); + } } else { resetAndPushToRoute('/Welcome'); } - // generate a new push token on sign in - if (session && _event === 'SIGNED_IN') { - registerForPushNotifications().then(async (token: string) => { - updatePushToken(session.user.id, token); - }); - } }); }, []); } diff --git a/src/context/AuthContext.tsx b/src/context/AuthContext.tsx index 106a2418..10ea62eb 100644 --- a/src/context/AuthContext.tsx +++ b/src/context/AuthContext.tsx @@ -7,7 +7,6 @@ import { isAuthError, } from '@supabase/supabase-js'; import * as Notifications from 'expo-notifications'; -import { router } from 'expo-router'; import React, { createContext, useContext, @@ -19,7 +18,10 @@ import React, { import supabaseAdmin from '../supabase/createAdminClient'; import supabase from '../supabase/createClient'; -import { removePushToken } from '../supabase/pushNotifications'; +import { + removePushToken, + routeUserToUpdate, +} from '../supabase/pushNotifications'; /** * To use AuthContext, import useSession() in whichever file you prefer. @@ -62,14 +64,6 @@ Notifications.setNotificationHandler({ }), }); -function routeUserToUpdate(response: Notifications.NotificationResponse) { - const updateId = response.notification.request.content.data.updateId; - const caseId = response.notification.request.content.data.caseId; - router.push(`/AllCases/CaseScreen/${caseId}`); - router.push(`/AllCases/Updates/${caseId}`); - router.push(`/AllCases/Updates/UpdateView/${updateId}`); -} - export function useSession() { return useContext(AuthContext); } @@ -90,16 +84,18 @@ export function AuthContextProvider({ .then(({ data: { session: newSession } }) => { setSession(newSession); setUser(newSession ? newSession.user : null); - - responseListener.current = - Notifications.addNotificationResponseReceivedListener(response => { - routeUserToUpdate(response); - }); }) .finally(() => { setIsLoading(false); }); + responseListener.current = + Notifications.addNotificationResponseReceivedListener(response => { + supabase.auth.getSession().then(() => { + routeUserToUpdate(response); + }); + }); + supabase.auth.onAuthStateChange((_event, newSession) => { setSession(newSession); }); diff --git a/src/supabase/pushNotifications.ts b/src/supabase/pushNotifications.ts index d69d6543..f6a09aef 100644 --- a/src/supabase/pushNotifications.ts +++ b/src/supabase/pushNotifications.ts @@ -1,5 +1,6 @@ import * as Device from 'expo-device'; import * as Notifications from 'expo-notifications'; +import { router } from 'expo-router'; import { Platform } from 'react-native'; import supabase from './createClient'; @@ -74,3 +75,17 @@ export async function removePushToken(userId: string) { throw error; } } + +/** + * Route user to the correct update upon pressing a notification. + * @param response response generated when a user presses a notification. + */ +export function routeUserToUpdate( + response: Notifications.NotificationResponse, +) { + const updateId = response.notification.request.content.data.updateId; + const caseId = response.notification.request.content.data.caseId; + router.push(`/AllCases/CaseScreen/${caseId}`); + router.push(`/AllCases/Updates/${caseId}`); + router.push(`/AllCases/Updates/UpdateView/${updateId}`); +}