Fix mobile notification:

> - Bug penerima pesan 2 kali

Fix:
modified:   hooks/use-foreground-notifications.ts

### No Issue
This commit is contained in:
2026-01-09 14:46:21 +08:00
parent 57ac1eb45e
commit 33cd47aaed

View File

@@ -4,6 +4,7 @@ import {
onMessage, onMessage,
FirebaseMessagingTypes, FirebaseMessagingTypes,
} from "@react-native-firebase/messaging"; } from "@react-native-firebase/messaging";
import { useAuth } from "./use-auth";
// Gunakan tipe resmi dari library // Gunakan tipe resmi dari library
type RemoteMessage = FirebaseMessagingTypes.RemoteMessage; type RemoteMessage = FirebaseMessagingTypes.RemoteMessage;
@@ -11,17 +12,26 @@ type RemoteMessage = FirebaseMessagingTypes.RemoteMessage;
export function useForegroundNotifications( export function useForegroundNotifications(
onMessageReceived: (message: RemoteMessage) => void onMessageReceived: (message: RemoteMessage) => void
) { ) {
const { user } = useAuth();
useEffect(() => { useEffect(() => {
const messaging = getMessaging(); const messaging = getMessaging();
const unsubscribe = onMessage(messaging, (remoteMessage) => { const unsubscribe = onMessage(messaging, (remoteMessage) => {
const data = remoteMessage.data;
// console.log("DATA NOTIFIKASI DARI SERVER", data)
if (data?.recipientId && data?.recipientId !== user?.id) {
console.log("📵 Notification untuk user lain", data);
return;
}
console.log( console.log(
"🔔 Notifikasi diterima saat app aktif:", "🔔 Notifikasi diterima saat app aktif:",
JSON.stringify(remoteMessage, null, 2) JSON.stringify(data, null, 2)
); );
onMessageReceived(remoteMessage); onMessageReceived(remoteMessage);
}); });
return unsubscribe; return unsubscribe;
}, [onMessageReceived]); }, [user?.id, onMessageReceived]);
} }