diff --git a/app/(application)/_layout.tsx b/app/(application)/_layout.tsx index 8292cf6..2c80f19 100644 --- a/app/(application)/_layout.tsx +++ b/app/(application)/_layout.tsx @@ -25,10 +25,12 @@ export default function RootLayout() { const { token, decryptToken, isLoading } = useAuthSession() const pathname = usePathname() - async function handleReadNotification(id: string, category: string, idContent: string) { + async function handleReadNotification(id: string, category: string, idContent: string, title: string) { try { - const hasil = await decryptToken(String(token?.current)) - const response = await apiReadOneNotification({ user: hasil, id: id }) + if (title != "Komentar Baru") { + const hasil = await decryptToken(String(token?.current)) + const response = await apiReadOneNotification({ user: hasil, id: id }) + } pushToPage(category, idContent) } catch (error) { console.error(error) @@ -53,26 +55,27 @@ export default function RootLayout() { 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 && - pathname != `/${category}/${content}` - ) { - Notifier.showNotification({ - title: remoteMessage.notification?.title, - description: remoteMessage.notification?.body, - duration: 3000, - animationDuration: 300, - showEasing: Easing.ease, - onPress: () => handleReadNotification(String(id), String(category), String(content)), - hideOnPress: true, - }); + const title = remoteMessage?.notification?.title; + + if (remoteMessage.notification != undefined && remoteMessage.notification.title != undefined && remoteMessage.notification.body != undefined) { + if (category == 'discussion-general' && pathname == '/discussion/' + content) { + return null + } else if (pathname != `/${category}/${content}`) { + Notifier.showNotification({ + title: title, + description: remoteMessage.notification?.body, + duration: 3000, + animationDuration: 300, + showEasing: Easing.ease, + onPress: () => handleReadNotification(String(id), String(category), String(content), String(title)), + hideOnPress: true, + }); + } } }); return unsubscribe; - }, []); + }, [pathname]); if (isLoading) { diff --git a/app/_layout.tsx b/app/_layout.tsx index 5fe9fee..4fb2042 100644 --- a/app/_layout.tsx +++ b/app/_layout.tsx @@ -4,9 +4,9 @@ import { Stack } from 'expo-router'; import * as SplashScreen from 'expo-splash-screen'; import { StatusBar } from 'expo-status-bar'; import { useEffect } from 'react'; -import 'react-native-reanimated'; -import { NotifierWrapper } from 'react-native-notifier'; import { GestureHandlerRootView } from 'react-native-gesture-handler'; +import { NotifierWrapper } from 'react-native-notifier'; +import 'react-native-reanimated'; // Prevent the splash screen from auto-hiding before asset loading is complete. SplashScreen.preventAutoHideAsync(); diff --git a/index.js b/index.js new file mode 100644 index 0000000..b4ec209 --- /dev/null +++ b/index.js @@ -0,0 +1,16 @@ +// index.js +import 'expo-router/entry'; // ⬅️ wajib ada agar expo-router tetap bekerja + +import messaging from '@react-native-firebase/messaging'; +import AsyncStorage from '@react-native-async-storage/async-storage'; + +// ✅ Firebase background handler — wajib diletakkan DI SINI +messaging().setBackgroundMessageHandler(async remoteMessage => { + + const screen = remoteMessage?.data?.category; + const content = remoteMessage?.data?.content; + + if (screen && content) { + await AsyncStorage.setItem('navigateOnOpen', JSON.stringify({ screen, content })); + } +}); diff --git a/lib/api.ts b/lib/api.ts index 8b55194..250c139 100644 --- a/lib/api.ts +++ b/lib/api.ts @@ -2,8 +2,8 @@ import axios from 'axios'; const api = axios.create({ // baseURL: 'http://10.0.2.2:3000/api', - baseURL: 'https://stg-darmasaba.wibudev.com/api', - // baseURL: 'http://192.168.1.89:3000/api', + // baseURL: 'https://stg-darmasaba.wibudev.com/api', + baseURL: 'http://192.168.1.89:3000/api', }); export const apiCheckPhoneLogin = async (body: { phone: string }) => { diff --git a/lib/useNotification.ts b/lib/useNotification.ts index da917c8..fcc22d3 100644 --- a/lib/useNotification.ts +++ b/lib/useNotification.ts @@ -26,19 +26,14 @@ 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)) + // const screen = remoteMessage?.data?.category; + // const content = remoteMessage?.data?.content; + + // if (screen && content) { + // await AsyncStorage.setItem('navigateOnOpen', JSON.stringify({ screen, content })); + // } // }); - mess.setBackgroundMessageHandler(async remoteMessage => { - const screen = remoteMessage?.data?.category; - const content = remoteMessage?.data?.content; - - if (screen && content) { - await AsyncStorage.setItem('navigateOnOpen', JSON.stringify({ screen, content })); - } - }); - return mess } catch (error) { console.error('Failed to initialize Firebase:', error); diff --git a/package.json b/package.json index 2b68bed..61d2a97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "mobile-darmasaba", - "main": "expo-router/entry", + "main": "index.js", "version": "1.0.0", "scripts": { "start": "expo start",