diff --git a/src/app/(BottomTabNavigation)/_layout.tsx b/src/app/(BottomTabNavigation)/_layout.tsx index 6a61f26a..f21dbb0e 100644 --- a/src/app/(BottomTabNavigation)/_layout.tsx +++ b/src/app/(BottomTabNavigation)/_layout.tsx @@ -1,5 +1,6 @@ +import * as Notifications from 'expo-notifications'; import { Tabs } from 'expo-router/tabs'; -import React from 'react'; +import React, { useEffect } from 'react'; import GreyHomeIcon from '../../../assets/bottom-tab-home-inactive.svg'; import RedHomeIcon from '../../../assets/bottom-tab-home.svg'; @@ -9,8 +10,43 @@ import GreyGearIcon from '../../../assets/bottom-tab-settings-gear-inactive.svg' import RedGearIcon from '../../../assets/bottom-tab-settings-gear.svg'; import TabBarItem from '../../Components/TabBarItem/TabBarItem'; import { CaseContextProvider } from '../../context/CaseContext'; +import { routeUserToUpdate } from '../../supabase/pushNotifications'; + +Notifications.setNotificationHandler({ + handleNotification: async () => ({ + shouldShowAlert: true, + shouldPlaySound: false, + shouldSetBadge: false, + }), +}); + +function useNotificationObserver() { + useEffect(() => { + let isMounted = true; + + Notifications.getLastNotificationResponseAsync().then(response => { + if (!isMounted || !response?.notification) { + return; + } + routeUserToUpdate(response); + }); + + const subscription = Notifications.addNotificationResponseReceivedListener( + response => { + routeUserToUpdate(response); + }, + ); + + return () => { + isMounted = false; + subscription.remove(); + }; + }, []); +} export default function AppLayout() { + useNotificationObserver(); + return ( ({ - shouldShowAlert: true, - shouldPlaySound: false, - shouldSetBadge: false, - }), -}); - export function useSession() { return useContext(AuthContext); } @@ -76,7 +68,7 @@ export function AuthContextProvider({ const [session, setSession] = useState(null); const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); - const responseListener = useRef(); + // const responseListener = useRef(); useEffect(() => { supabase.auth @@ -89,19 +81,25 @@ export function AuthContextProvider({ setIsLoading(false); }); - responseListener.current = - Notifications.addNotificationResponseReceivedListener(response => { - supabase.auth.getSession().then(() => { - routeUserToUpdate(response); - }); - }); + // Notifications.getLastNotificationResponseAsync() + // .then(response => { + // if (!isMounted || !response?.notification) { + // return; + // } + // routeUserToUpdate(response); + // }); + + // responseListener.current = + // Notifications.addNotificationResponseReceivedListener(response => { + // routeUserToUpdate(response); + // }); supabase.auth.onAuthStateChange((_event, newSession) => { setSession(newSession); }); - return () => - Notifications.removeNotificationSubscription(responseListener.current!); + // return () => + // Notifications.removeNotificationSubscription(responseListener.current!); }, []); const signInWithEmail = async (