import { Ionicons } from "@expo/vector-icons"; import * as Notifications from "expo-notifications"; import { Redirect, Tabs, router } from "expo-router"; import { useEffect, useRef } from "react"; import { View } from "react-native"; import { useAuth } from "../../lib/auth"; import { DeviceProvider } from "../../lib/device"; import { t } from "../../lib/locales"; import { addNotificationReceivedListener, addNotificationResponseListener, getLastNotificationResponse, initializeNotifications, } from "../../lib/notifications"; import { colors } from "../../lib/theme"; import { LoadingScreen } from "../../lib/ui"; export default function TabsLayout() { const { isLoading, isAuthenticated } = useAuth(); const notificationListener = useRef( null ); const responseListener = useRef(null); useEffect(() => { if (!isAuthenticated) return; // Initialize notifications when authenticated initializeNotifications().then(({ permissionStatus, tokenRegistered }) => { console.log( `Notifications initialized: permission=${permissionStatus}, tokenRegistered=${tokenRegistered}` ); }); // Set up notification listeners notificationListener.current = addNotificationReceivedListener( (notification) => { console.log("Notification received:", notification); } ); responseListener.current = addNotificationResponseListener((response) => { console.log("Notification tapped:", response); const data = response.notification.request.content.data; // Navigate based on notification type if (data?.type === "dangerous_content") { router.push("/(tabs)/alerts"); } else if (data?.type === "new_contact") { // Navigate to contact detail screen with contact info router.push({ pathname: "/(tabs)/contact-detail", params: { contactName: String(data.contactName || "Unknown"), contactIdentifier: String(data.contactIdentifier || "Not provided"), contactType: String(data.contactType || "unknown"), deviceName: String(data.deviceName || "Unknown Device"), }, }); } }); // Check if app was opened from a notification (cold start) getLastNotificationResponse().then((response) => { if (response) { const data = response.notification.request.content.data; if (data?.type === "dangerous_content") { router.push("/(tabs)/alerts"); } else if (data?.type === "new_contact") { router.push({ pathname: "/(tabs)/contact-detail", params: { contactName: String(data.contactName || "Unknown"), contactIdentifier: String( data.contactIdentifier || "Not provided" ), contactType: String(data.contactType || "unknown"), deviceName: String(data.deviceName || "Unknown Device"), }, }); } } }); return () => { if (notificationListener.current) { notificationListener.current.remove(); } if (responseListener.current) { responseListener.current.remove(); } }; }, [isAuthenticated]); if (isLoading) { return ; } if (!isAuthenticated) { return ; } return ( ( ), }} /> ( ), }} /> ( ), }} /> ( ), }} /> ); }