From 7fbaad97a183d46b3077a8c35cd14c2ff179e0bd Mon Sep 17 00:00:00 2001 From: gibbyb Date: Fri, 18 Oct 2024 13:24:12 -0500 Subject: [PATCH] Now we are past the initial sign in bugs --- .../notifications/PushNotificationManager.tsx | 75 ++++++------------- 1 file changed, 23 insertions(+), 52 deletions(-) diff --git a/components/services/notifications/PushNotificationManager.tsx b/components/services/notifications/PushNotificationManager.tsx index cdacb70..69bd8e0 100644 --- a/components/services/notifications/PushNotificationManager.tsx +++ b/components/services/notifications/PushNotificationManager.tsx @@ -1,15 +1,15 @@ -import React, { useState, useEffect, useRef, ErrorInfo } from 'react'; +import React, { useState, useEffect, useRef } from 'react'; import { Alert, Platform } from 'react-native'; import * as Device from 'expo-device'; import * as Notifications from 'expo-notifications'; import Constants from 'expo-constants'; -import type { NotificationMessage } from '@/constants/Types'; +import { NotificationMessage } from '@/constants/Types'; Notifications.setNotificationHandler({ handleNotification: async () => ({ shouldShowAlert: true, - shouldPlaySound: true, - shouldSetBadge: true, + shouldPlaySound: false, + shouldSetBadge: false, }), }); @@ -48,7 +48,7 @@ const handleRegistrationError = (errorMessage: string) => { throw new Error(errorMessage); }; -const registerforPushNotificationsAsync = async () => { +async function registerForPushNotificationsAsync() { let token; if (Platform.OS === 'android') { @@ -73,69 +73,40 @@ const registerforPushNotificationsAsync = async () => { } const projectId = Constants.expoConfig?.extra?.eas?.projectId; if (!projectId) { - alert('Project ID not found in eas.json'); + alert('Project ID not found'); return; } token = (await Notifications.getExpoPushTokenAsync({ projectId })).data; } else { alert('Must use physical device for Push Notifications'); } - return token; -}; -export const PushNotificationManager = ({children}: {children: React.ReactNode}) => { - const [expoPushToken, setExpoPushToken] = useState(''); - const [notification, setNotification] = - useState(undefined); + return token; +} + +export function PushNotificationManager({ children }: { children: React.ReactNode }) { + const [expoPushToken, setExpoPushToken] = useState(''); + const [notification, setNotification] = useState(undefined); const notificationListener = useRef(); const responseListener = useRef(); useEffect(() => { - registerforPushNotificationsAsync() - .then(token => setExpoPushToken(token ?? '')) - .catch((error: any) => { - setExpoPushToken(''); - console.error(error); - }); + registerForPushNotificationsAsync().then(token => setExpoPushToken(token)); - notificationListener.current = Notifications.addNotificationReceivedListener( - notification => { - setNotification(notification); + notificationListener.current = Notifications.addNotificationReceivedListener(notification => { + setNotification(notification); }); - responseListener.current = Notifications.addNotificationResponseReceivedListener( - response => { - console.log(response); - // Handle notification response here + + responseListener.current = Notifications.addNotificationResponseReceivedListener(response => { + console.log(response); + // Handle notification response here }); + return () => { - notificationListener.current && - Notifications.removeNotificationSubscription(notificationListener.current); - responseListener.current && - Notifications.removeNotificationSubscription(responseListener.current); + Notifications.removeNotificationSubscription(notificationListener.current!); + Notifications.removeNotificationSubscription(responseListener.current!); }; }, []); - return ( {children} ); -}; -class ErrorBoundary extends React.Component<{children: React.ReactNode}, {hasError: boolean}> { - constructor(props: {children: React.ReactNode}) { - super(props); - this.state = { hasError: false }; - } - - static getDerivedStateFromError(_: Error) { - return { hasError: true }; - } - - componentDidCatch(error: Error, errorInfo: ErrorInfo) { - console.log('Error caught by ErrorBoundary:', error, errorInfo); - } - - render() { - if (this.state.hasError) { - return Something went wrong.; - } - - return this.props.children; - } + return <>{children}; }