From ed08953a14d31f55c2ed616d988b5405501abc77 Mon Sep 17 00:00:00 2001 From: amel Date: Thu, 26 Jun 2025 10:16:33 +0800 Subject: [PATCH] upd: notification --- app/(application)/_layout.tsx | 59 ++++++++++++++++++------ components/buttonHeader.tsx | 10 +++- lib/registerForPushNotificationsAsync.ts | 1 - 3 files changed, 52 insertions(+), 18 deletions(-) diff --git a/app/(application)/_layout.tsx b/app/(application)/_layout.tsx index 44028a5..8719f68 100644 --- a/app/(application)/_layout.tsx +++ b/app/(application)/_layout.tsx @@ -11,7 +11,7 @@ import { Headers } from "@/constants/Headers"; import store from "@/lib/store"; import { useAuthSession } from "@/providers/AuthProvider"; import messaging from "@react-native-firebase/messaging"; -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 { Text } from "react-native"; @@ -19,18 +19,50 @@ import { Easing, Notifier } from 'react-native-notifier'; import { Provider } from "react-redux"; export default function RootLayout() { + const pathname = usePathname(); + + const handleNotificationPress = (category: any, content: any) => { + switch (category) { + case 'announcement': + router.push(`/announcement/${content}`); + break; + case 'discussion': + router.push(`/discussion/${content}`); + break; + case 'division': + router.push(`/division/${content}`); + break; + case 'member': + router.push(`/member/${content}`); + break; + case 'project': + router.push(`/project/${content}`); + break; + case 'announcement': + router.push(`/announcement/${content}`); + break; + // Add other cases as needed + default: + // Handle unknown category + console.warn(`Unknown category: ${category}`); + } + }; + useEffect(() => { const unsubscribe = messaging().onMessage(async remoteMessage => { - console.log('pesan', remoteMessage.data) - Notifier.showNotification({ - title: remoteMessage.notification?.title, - description: remoteMessage.notification?.body, - duration: 5000, - animationDuration: 500, - showEasing: Easing.ease, - onPress: () => router.push('/notification'), - hideOnPress: false, - }); + const category = remoteMessage?.data?.category; + const content = remoteMessage?.data?.content; + if (category != pathname.substring(1)) { + Notifier.showNotification({ + title: remoteMessage.notification?.title, + description: remoteMessage.notification?.body, + duration: 3000, + animationDuration: 300, + showEasing: Easing.ease, + onPress: () => handleNotificationPress(category, content), + hideOnPress: true, + }); + } }); return unsubscribe; @@ -56,9 +88,6 @@ export default function RootLayout() { headerRight: () => , headerTitleAlign: 'left', headerBackVisible: false, - contentStyle:{ - - } }} /> @@ -110,7 +139,7 @@ export default function RootLayout() { }} /> - + ) } diff --git a/components/buttonHeader.tsx b/components/buttonHeader.tsx index 22838a8..f151775 100644 --- a/components/buttonHeader.tsx +++ b/components/buttonHeader.tsx @@ -1,17 +1,23 @@ import Styles from "@/constants/Styles"; -import { Pressable, View } from "react-native"; +import { Pressable, Text, View } from "react-native"; type PropsBtnHeader = { onPress: () => void; item: React.ReactNode; + valueTop?: boolean; }; -export function ButtonHeader({ onPress, item }: PropsBtnHeader) { +export function ButtonHeader({ onPress, item, valueTop }: PropsBtnHeader) { return ( { onPress() }}> {item} + { + valueTop != undefined && valueTop && ( + + ) + } ) } \ No newline at end of file diff --git a/lib/registerForPushNotificationsAsync.ts b/lib/registerForPushNotificationsAsync.ts index 616328d..866a50f 100644 --- a/lib/registerForPushNotificationsAsync.ts +++ b/lib/registerForPushNotificationsAsync.ts @@ -32,7 +32,6 @@ export async function registerForPushNotificationsAsync() { try { const pushTokenString = (await Notifications.getExpoPushTokenAsync({ projectId })).data - console.log(pushTokenString) return pushTokenString } catch (error) { throw new Error(`Error getting push token: ${error}`)