upd: notification

This commit is contained in:
amel
2025-06-26 10:16:33 +08:00
parent ffe53e46b1
commit ed08953a14
3 changed files with 52 additions and 18 deletions

View File

@@ -11,7 +11,7 @@ import { Headers } from "@/constants/Headers";
import store from "@/lib/store"; import store from "@/lib/store";
import { useAuthSession } from "@/providers/AuthProvider"; import { useAuthSession } from "@/providers/AuthProvider";
import messaging from "@react-native-firebase/messaging"; 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 { StatusBar } from 'expo-status-bar';
import { useEffect } from "react"; import { useEffect } from "react";
import { Text } from "react-native"; import { Text } from "react-native";
@@ -19,18 +19,50 @@ import { Easing, Notifier } from 'react-native-notifier';
import { Provider } from "react-redux"; import { Provider } from "react-redux";
export default function RootLayout() { 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(() => { useEffect(() => {
const unsubscribe = messaging().onMessage(async remoteMessage => { const unsubscribe = messaging().onMessage(async remoteMessage => {
console.log('pesan', remoteMessage.data) const category = remoteMessage?.data?.category;
Notifier.showNotification({ const content = remoteMessage?.data?.content;
title: remoteMessage.notification?.title, if (category != pathname.substring(1)) {
description: remoteMessage.notification?.body, Notifier.showNotification({
duration: 5000, title: remoteMessage.notification?.title,
animationDuration: 500, description: remoteMessage.notification?.body,
showEasing: Easing.ease, duration: 3000,
onPress: () => router.push('/notification'), animationDuration: 300,
hideOnPress: false, showEasing: Easing.ease,
}); onPress: () => handleNotificationPress(category, content),
hideOnPress: true,
});
}
}); });
return unsubscribe; return unsubscribe;
@@ -56,9 +88,6 @@ export default function RootLayout() {
headerRight: () => <HeaderRightHome />, headerRight: () => <HeaderRightHome />,
headerTitleAlign: 'left', headerTitleAlign: 'left',
headerBackVisible: false, headerBackVisible: false,
contentStyle:{
}
}} /> }} />
<Stack.Screen name="feature" options={{ title: 'Fitur' }} /> <Stack.Screen name="feature" options={{ title: 'Fitur' }} />
<Stack.Screen name="search" options={{ title: 'Pencarian' }} /> <Stack.Screen name="search" options={{ title: 'Pencarian' }} />
@@ -110,7 +139,7 @@ export default function RootLayout() {
}} }}
/> />
</Stack> </Stack>
<StatusBar style="light" translucent={false} backgroundColor="black"/> <StatusBar style="light" translucent={false} backgroundColor="black" />
</Provider> </Provider>
) )
} }

View File

@@ -1,17 +1,23 @@
import Styles from "@/constants/Styles"; import Styles from "@/constants/Styles";
import { Pressable, View } from "react-native"; import { Pressable, Text, View } from "react-native";
type PropsBtnHeader = { type PropsBtnHeader = {
onPress: () => void; onPress: () => void;
item: React.ReactNode; item: React.ReactNode;
valueTop?: boolean;
}; };
export function ButtonHeader({ onPress, item }: PropsBtnHeader) { export function ButtonHeader({ onPress, item, valueTop }: PropsBtnHeader) {
return ( return (
<Pressable onPress={() => { onPress() }}> <Pressable onPress={() => { onPress() }}>
<View style={[Styles.btnIconHeader]}> <View style={[Styles.btnIconHeader]}>
{item} {item}
</View> </View>
{
valueTop != undefined && valueTop && (
<View style={{ position: 'absolute', top: 0, right: 0, backgroundColor: 'red', width: 10, height: 10, borderRadius: 100 }}> </View>
)
}
</Pressable> </Pressable>
) )
} }

View File

@@ -32,7 +32,6 @@ export async function registerForPushNotificationsAsync() {
try { try {
const pushTokenString = (await Notifications.getExpoPushTokenAsync({ projectId })).data const pushTokenString = (await Notifications.getExpoPushTokenAsync({ projectId })).data
console.log(pushTokenString)
return pushTokenString return pushTokenString
} catch (error) { } catch (error) {
throw new Error(`Error getting push token: ${error}`) throw new Error(`Error getting push token: ${error}`)