From 5db712d4b9b751a4bb4a82cd8fd8479b78fc9b2d Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Wed, 6 Aug 2025 11:58:54 +0800 Subject: [PATCH] upd: notifikasi diskusi umum --- app/(application)/_layout.tsx | 24 ++++++++++++++++++++++-- lib/useNotification.ts | 14 ++++++++++++-- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/app/(application)/_layout.tsx b/app/(application)/_layout.tsx index eaf62fb..8292cf6 100644 --- a/app/(application)/_layout.tsx +++ b/app/(application)/_layout.tsx @@ -13,8 +13,9 @@ import { apiReadOneNotification } from "@/lib/api"; import { pushToPage } from "@/lib/pushToPage"; import store from "@/lib/store"; import { useAuthSession } from "@/providers/AuthProvider"; +import AsyncStorage from "@react-native-async-storage/async-storage"; import firebase from '@react-native-firebase/app'; -import { Redirect, router, Stack } from "expo-router"; +import { Redirect, router, Stack, usePathname } from "expo-router"; import { StatusBar } from 'expo-status-bar'; import { useEffect } from "react"; import { Easing, Notifier } from 'react-native-notifier'; @@ -22,6 +23,7 @@ import { Provider } from "react-redux"; export default function RootLayout() { const { token, decryptToken, isLoading } = useAuthSession() + const pathname = usePathname() async function handleReadNotification(id: string, category: string, idContent: string) { try { @@ -33,12 +35,30 @@ export default function RootLayout() { } } + useEffect(() => { + const checkNavigation = async () => { + const navData = await AsyncStorage.getItem('navigateOnOpen'); + if (navData) { + const { screen, content } = JSON.parse(navData); + await AsyncStorage.removeItem('navigateOnOpen'); // reset + pushToPage(screen, content) + } + }; + + checkNavigation(); + }, []); + useEffect(() => { const unsubscribe = firebase.app().messaging().onMessage(async remoteMessage => { const id = remoteMessage?.data?.id; const category = remoteMessage?.data?.category; const content = remoteMessage?.data?.content; - if (remoteMessage.notification != undefined && remoteMessage.notification.title != undefined && remoteMessage.notification.body != undefined) { + if ( + remoteMessage.notification != undefined && + remoteMessage.notification.title != undefined && + remoteMessage.notification.body != undefined && + pathname != `/${category}/${content}` + ) { Notifier.showNotification({ title: remoteMessage.notification?.title, description: remoteMessage.notification?.body, diff --git a/lib/useNotification.ts b/lib/useNotification.ts index 07f0f4b..da917c8 100644 --- a/lib/useNotification.ts +++ b/lib/useNotification.ts @@ -1,3 +1,4 @@ +import AsyncStorage from '@react-native-async-storage/async-storage'; import { getApp, getApps, initializeApp } from '@react-native-firebase/app'; import messaging, { getMessaging } from '@react-native-firebase/messaging'; import { useEffect } from 'react'; @@ -24,9 +25,18 @@ const initializeFirebase = async () => { // Set auto initialization and background message handler mess.setAutoInitEnabled(true); + // mess.setBackgroundMessageHandler(async remoteMessage => { + // // console.log('Message handled in the background!', remoteMessage); + // // pushToPage(String(remoteMessage?.data?.category), String(remoteMessage?.data?.content)) + // }); + mess.setBackgroundMessageHandler(async remoteMessage => { - // console.log('Message handled in the background!', remoteMessage); - // pushToPage(String(remoteMessage?.data?.category), String(remoteMessage?.data?.content)) + const screen = remoteMessage?.data?.category; + const content = remoteMessage?.data?.content; + + if (screen && content) { + await AsyncStorage.setItem('navigateOnOpen', JSON.stringify({ screen, content })); + } }); return mess