upd: push notification on background

Deskripsi:
- on klik pada background notification
- hide and show notificationn foreground

No Issues
This commit is contained in:
2025-08-07 16:25:45 +08:00
parent 9cb848ddd2
commit 7555ece0fa
6 changed files with 49 additions and 35 deletions

View File

@@ -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) {

View File

@@ -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();

16
index.js Normal file
View File

@@ -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 }));
}
});

View File

@@ -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 }) => {

View File

@@ -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);

View File

@@ -1,6 +1,6 @@
{
"name": "mobile-darmasaba",
"main": "expo-router/entry",
"main": "index.js",
"version": "1.0.0",
"scripts": {
"start": "expo start",