This commit is contained in:
2025-12-13 17:31:48 +08:00
parent 43c8c105cf
commit 34680a4c38
5 changed files with 166 additions and 22 deletions

View File

@@ -4,7 +4,14 @@ import { useEffect } from "react";
import "react-native-gesture-handler";
import { SafeAreaProvider } from "react-native-safe-area-context";
import Toast from "react-native-toast-message";
import messaging from "@react-native-firebase/messaging";
import messaging, {
FirebaseMessagingTypes,
} from "@react-native-firebase/messaging";
import { useForegroundNotifications } from "@/hooks/use-foreground-notifications";
import {
NotificationProvider,
useNotificationStore,
} from "@/hooks/use-notification-store";
export default function RootLayout() {
useEffect(() => {
@@ -27,14 +34,41 @@ export default function RootLayout() {
testFCM();
}, []);
const { addNotification } = useNotificationStore();
const handleForegroundNotification = (
message: FirebaseMessagingTypes.RemoteMessage
) => {
const title = message.notification?.title || "Notifikasi";
const body = message.notification?.body || "";
const rawData = message.data || {};
const safeData: Record<string, string> = {};
for (const key in rawData) {
if (typeof rawData[key] === "string") {
safeData[key] = rawData[key] as string;
} else {
// Jika object/array/number → ubah ke JSON string
safeData[key] = JSON.stringify(rawData[key]);
}
}
// ✅ Simpan ke state → akan trigger update UI (termasuk icon bell)
addNotification({ body, title, data: safeData });
};
useForegroundNotifications(handleForegroundNotification);
return (
<>
<SafeAreaProvider>
<AuthProvider>
<AppRoot />
</AuthProvider>
</SafeAreaProvider>
<Toast />
<NotificationProvider>
<SafeAreaProvider>
<AuthProvider>
<AppRoot />
</AuthProvider>
</SafeAreaProvider>
<Toast />
</NotificationProvider>
</>
);
}