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:
@@ -13,8 +13,9 @@ import { apiReadOneNotification } from "@/lib/api";
|
|||||||
import { pushToPage } from "@/lib/pushToPage";
|
import { pushToPage } from "@/lib/pushToPage";
|
||||||
import store from "@/lib/store";
|
import store from "@/lib/store";
|
||||||
import { useAuthSession } from "@/providers/AuthProvider";
|
import { useAuthSession } from "@/providers/AuthProvider";
|
||||||
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
||||||
import firebase from '@react-native-firebase/app';
|
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 { StatusBar } from 'expo-status-bar';
|
||||||
import { useEffect } from "react";
|
import { useEffect } from "react";
|
||||||
import { Easing, Notifier } from 'react-native-notifier';
|
import { Easing, Notifier } from 'react-native-notifier';
|
||||||
@@ -22,6 +23,7 @@ import { Provider } from "react-redux";
|
|||||||
|
|
||||||
export default function RootLayout() {
|
export default function RootLayout() {
|
||||||
const { token, decryptToken, isLoading } = useAuthSession()
|
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) {
|
||||||
try {
|
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(() => {
|
useEffect(() => {
|
||||||
const unsubscribe = firebase.app().messaging().onMessage(async remoteMessage => {
|
const unsubscribe = firebase.app().messaging().onMessage(async remoteMessage => {
|
||||||
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 (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({
|
Notifier.showNotification({
|
||||||
title: remoteMessage.notification?.title,
|
title: remoteMessage.notification?.title,
|
||||||
description: remoteMessage.notification?.body,
|
description: remoteMessage.notification?.body,
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||||
import { getApp, getApps, initializeApp } from '@react-native-firebase/app';
|
import { getApp, getApps, initializeApp } from '@react-native-firebase/app';
|
||||||
import messaging, { getMessaging } from '@react-native-firebase/messaging';
|
import messaging, { getMessaging } from '@react-native-firebase/messaging';
|
||||||
import { useEffect } from 'react';
|
import { useEffect } from 'react';
|
||||||
@@ -24,9 +25,18 @@ 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 => {
|
||||||
|
// // console.log('Message handled in the background!', remoteMessage);
|
||||||
|
// // pushToPage(String(remoteMessage?.data?.category), String(remoteMessage?.data?.content))
|
||||||
|
// });
|
||||||
|
|
||||||
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 }));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
return mess
|
return mess
|
||||||
|
|||||||
Reference in New Issue
Block a user