Merge pull request 'upd: push notification on background' (#17) from amalia/07-agustus-25 into join

Reviewed-on: bip/mobile-darmasaba#17
This commit is contained in:
2025-08-07 17:43:02 +08:00
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 { token, decryptToken, isLoading } = useAuthSession()
const pathname = usePathname() const pathname = usePathname()
async function handleReadNotification(id: string, category: string, idContent: string) { async function handleReadNotification(id: string, category: string, idContent: string, title: string) {
try { try {
const hasil = await decryptToken(String(token?.current)) if (title != "Komentar Baru") {
const response = await apiReadOneNotification({ user: hasil, id: id }) const hasil = await decryptToken(String(token?.current))
const response = await apiReadOneNotification({ user: hasil, id: id })
}
pushToPage(category, idContent) pushToPage(category, idContent)
} catch (error) { } catch (error) {
console.error(error) console.error(error)
@@ -53,26 +55,27 @@ export default function RootLayout() {
const id = remoteMessage?.data?.id; const id = remoteMessage?.data?.id;
const category = remoteMessage?.data?.category; const category = remoteMessage?.data?.category;
const content = remoteMessage?.data?.content; const content = remoteMessage?.data?.content;
if ( const title = remoteMessage?.notification?.title;
remoteMessage.notification != undefined &&
remoteMessage.notification.title != undefined && if (remoteMessage.notification != undefined && remoteMessage.notification.title != undefined && remoteMessage.notification.body != undefined) {
remoteMessage.notification.body != undefined && if (category == 'discussion-general' && pathname == '/discussion/' + content) {
pathname != `/${category}/${content}` return null
) { } else if (pathname != `/${category}/${content}`) {
Notifier.showNotification({ Notifier.showNotification({
title: remoteMessage.notification?.title, title: title,
description: remoteMessage.notification?.body, description: remoteMessage.notification?.body,
duration: 3000, duration: 3000,
animationDuration: 300, animationDuration: 300,
showEasing: Easing.ease, showEasing: Easing.ease,
onPress: () => handleReadNotification(String(id), String(category), String(content)), onPress: () => handleReadNotification(String(id), String(category), String(content), String(title)),
hideOnPress: true, hideOnPress: true,
}); });
}
} }
}); });
return unsubscribe; return unsubscribe;
}, []); }, [pathname]);
if (isLoading) { if (isLoading) {

View File

@@ -4,9 +4,9 @@ import { Stack } from 'expo-router';
import * as SplashScreen from 'expo-splash-screen'; import * as SplashScreen from 'expo-splash-screen';
import { StatusBar } from 'expo-status-bar'; import { StatusBar } from 'expo-status-bar';
import { useEffect } from 'react'; import { useEffect } from 'react';
import 'react-native-reanimated';
import { NotifierWrapper } from 'react-native-notifier';
import { GestureHandlerRootView } from 'react-native-gesture-handler'; 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. // Prevent the splash screen from auto-hiding before asset loading is complete.
SplashScreen.preventAutoHideAsync(); 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({ const api = axios.create({
// baseURL: 'http://10.0.2.2:3000/api', // baseURL: 'http://10.0.2.2:3000/api',
baseURL: 'https://stg-darmasaba.wibudev.com/api', // baseURL: 'https://stg-darmasaba.wibudev.com/api',
// baseURL: 'http://192.168.1.89:3000/api', baseURL: 'http://192.168.1.89:3000/api',
}); });
export const apiCheckPhoneLogin = async (body: { phone: string }) => { export const apiCheckPhoneLogin = async (body: { phone: string }) => {

View File

@@ -26,19 +26,14 @@ const initializeFirebase = async () => {
// Set auto initialization and background message handler // Set auto initialization and background message handler
mess.setAutoInitEnabled(true); mess.setAutoInitEnabled(true);
// mess.setBackgroundMessageHandler(async remoteMessage => { // mess.setBackgroundMessageHandler(async remoteMessage => {
// // console.log('Message handled in the background!', remoteMessage); // const screen = remoteMessage?.data?.category;
// // pushToPage(String(remoteMessage?.data?.category), String(remoteMessage?.data?.content)) // 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 return mess
} catch (error) { } catch (error) {
console.error('Failed to initialize Firebase:', error); console.error('Failed to initialize Firebase:', error);

View File

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