Skip to content

Commit

Permalink
[wip] add auth getSession() to notification handler to try and resolv…
Browse files Browse the repository at this point in the history
…e notifications issue.
  • Loading branch information
ronniebeggs committed Aug 22, 2024
1 parent 44b17fb commit da08a4a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 53 deletions.
33 changes: 1 addition & 32 deletions src/app/(BottomTabNavigation)/AllCases/index.tsx
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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<Notifications.Subscription>();

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 (
<View style={device.safeArea}>
<View style={styles.casesContainer}>
Expand Down
13 changes: 7 additions & 6 deletions src/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
});
}, []);
}
Expand Down
26 changes: 11 additions & 15 deletions src/context/AuthContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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.
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
});
Expand Down
15 changes: 15 additions & 0 deletions src/supabase/pushNotifications.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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}`);
}

0 comments on commit da08a4a

Please sign in to comment.