63 lines
1.8 KiB
TypeScript
63 lines
1.8 KiB
TypeScript
import "../globals.css";
|
|
import { useEffect } from 'react';
|
|
import { Stack } from 'expo-router';
|
|
import { SafeAreaProvider } from 'react-native-safe-area-context';
|
|
import {
|
|
getMessaging,
|
|
onMessage,
|
|
onNotificationOpenedApp,
|
|
getInitialNotification,
|
|
} from '@react-native-firebase/messaging';
|
|
import { registerDeviceToken, listenForTokenRefresh } from '@/services/notifications';
|
|
import { AuthProvider, useAuth } from "@/context/AuthContext";
|
|
import Toast from 'react-native-toast-message';
|
|
|
|
function NotificationSetup() {
|
|
const { user, loading } = useAuth();
|
|
|
|
useEffect(() => {
|
|
if (loading || !user) return;
|
|
|
|
registerDeviceToken(user.token);
|
|
|
|
const messaging = getMessaging();
|
|
|
|
const unsubRefresh = listenForTokenRefresh(user.token);
|
|
const unsubForeground = onMessage(messaging, async (msg) => {
|
|
console.log('Foreground notification:', msg);
|
|
Toast.show({
|
|
type: 'info',
|
|
text1: msg.notification?.title ?? 'New Notification',
|
|
position: 'top',
|
|
visibilityTime: 4000,
|
|
});
|
|
});
|
|
|
|
onNotificationOpenedApp(messaging, (msg) => {
|
|
console.log('Opened from background:', msg);
|
|
});
|
|
|
|
getInitialNotification(messaging).then((msg) => {
|
|
if (msg) console.log('Opened from quit state:', msg);
|
|
});
|
|
|
|
return () => {
|
|
unsubRefresh();
|
|
unsubForeground();
|
|
};
|
|
}, [user, loading]);
|
|
|
|
return null;
|
|
}
|
|
|
|
export default function RootLayout() {
|
|
return (
|
|
<AuthProvider>
|
|
<SafeAreaProvider>
|
|
<NotificationSetup />
|
|
<Stack screenOptions={{ headerShown: false }} />
|
|
<Toast />
|
|
</SafeAreaProvider>
|
|
</AuthProvider>
|
|
);
|
|
} |