Now we are past the initial sign in bugs
This commit is contained in:
parent
770d04540c
commit
7fbaad97a1
@ -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<Notifications.Notification | undefined>(undefined);
|
||||
return token;
|
||||
}
|
||||
|
||||
export function PushNotificationManager({ children }: { children: React.ReactNode }) {
|
||||
const [expoPushToken, setExpoPushToken] = useState<string | undefined>('');
|
||||
const [notification, setNotification] = useState<Notifications.Notification | undefined>(undefined);
|
||||
const notificationListener = useRef<Notifications.Subscription>();
|
||||
const responseListener = useRef<Notifications.Subscription>();
|
||||
|
||||
useEffect(() => {
|
||||
registerforPushNotificationsAsync()
|
||||
.then(token => setExpoPushToken(token ?? ''))
|
||||
.catch((error: any) => {
|
||||
setExpoPushToken('');
|
||||
console.error(error);
|
||||
});
|
||||
registerForPushNotificationsAsync().then(token => setExpoPushToken(token));
|
||||
|
||||
notificationListener.current = Notifications.addNotificationReceivedListener(
|
||||
notification => {
|
||||
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
|
||||
setNotification(notification);
|
||||
});
|
||||
responseListener.current = Notifications.addNotificationResponseReceivedListener(
|
||||
response => {
|
||||
|
||||
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 ( <ErrorBoundary>{children}</ErrorBoundary> );
|
||||
};
|
||||
|
||||
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 <ThemedText>Something went wrong.</ThemedText>;
|
||||
}
|
||||
|
||||
return this.props.children;
|
||||
}
|
||||
return <>{children}</>;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user