Merge pull request 'upd: notifikasi diskusi umum' (#14) from amalia/06-agustus-25 into join

Reviewed-on: bip/mobile-darmasaba#14
This commit is contained in:
2025-08-06 12:00:02 +08:00
2 changed files with 34 additions and 4 deletions

View File

@@ -13,8 +13,9 @@ import { apiReadOneNotification } from "@/lib/api";
import { pushToPage } from "@/lib/pushToPage";
import store from "@/lib/store";
import { useAuthSession } from "@/providers/AuthProvider";
import AsyncStorage from "@react-native-async-storage/async-storage";
import firebase from '@react-native-firebase/app';
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 { Easing, Notifier } from 'react-native-notifier';
@@ -22,6 +23,7 @@ import { Provider } from "react-redux";
export default function RootLayout() {
const { token, decryptToken, isLoading } = useAuthSession()
const pathname = usePathname()
async function handleReadNotification(id: string, category: string, idContent: string) {
try {
@@ -33,12 +35,30 @@ export default function RootLayout() {
}
}
useEffect(() => {
const checkNavigation = async () => {
const navData = await AsyncStorage.getItem('navigateOnOpen');
if (navData) {
const { screen, content } = JSON.parse(navData);
await AsyncStorage.removeItem('navigateOnOpen'); // reset
pushToPage(screen, content)
}
};
checkNavigation();
}, []);
useEffect(() => {
const unsubscribe = firebase.app().messaging().onMessage(async remoteMessage => {
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) {
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,

View File

@@ -1,3 +1,4 @@
import AsyncStorage from '@react-native-async-storage/async-storage';
import { getApp, getApps, initializeApp } from '@react-native-firebase/app';
import messaging, { getMessaging } from '@react-native-firebase/messaging';
import { useEffect } from 'react';
@@ -24,9 +25,18 @@ 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))
// });
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 }));
}
});
return mess