From 602860d9c3e7c4f1cfd47b851c2df2f6c7c9f9fa Mon Sep 17 00:00:00 2001 From: amaliadwiy Date: Fri, 8 Aug 2025 11:26:09 +0800 Subject: [PATCH] upd: clear warning Deskripsi: - update firebase > clear warning No Issues --- app/(application)/_layout.tsx | 15 +++++++++------ index.js | 9 ++++++--- lib/useNotification.ts | 30 +++++++++--------------------- providers/AuthProvider.tsx | 4 +++- 4 files changed, 27 insertions(+), 31 deletions(-) diff --git a/app/(application)/_layout.tsx b/app/(application)/_layout.tsx index 2c80f19..ba8a7fd 100644 --- a/app/(application)/_layout.tsx +++ b/app/(application)/_layout.tsx @@ -14,7 +14,8 @@ 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 { getApp } from "@react-native-firebase/app"; +import { getMessaging, onMessage } from "@react-native-firebase/messaging"; import { Redirect, router, Stack, usePathname } from "expo-router"; import { StatusBar } from 'expo-status-bar'; import { useEffect } from "react"; @@ -51,16 +52,18 @@ export default function RootLayout() { }, []); useEffect(() => { - const unsubscribe = firebase.app().messaging().onMessage(async remoteMessage => { + const mess = getMessaging(getApp()); + + const unsubscribe = onMessage(mess, async remoteMessage => { const id = remoteMessage?.data?.id; const category = remoteMessage?.data?.category; const content = remoteMessage?.data?.content; 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}`) { + if (remoteMessage.notification?.title && remoteMessage.notification?.body) { + if (category === 'discussion-general' && pathname === '/discussion/' + content) { + return null; + } else if (pathname !== `/${category}/${content}`) { Notifier.showNotification({ title: title, description: remoteMessage.notification?.body, diff --git a/index.js b/index.js index b4ec209..f47919e 100644 --- a/index.js +++ b/index.js @@ -1,12 +1,15 @@ // index.js import 'expo-router/entry'; // ⬅️ wajib ada agar expo-router tetap bekerja -import messaging from '@react-native-firebase/messaging'; +import { getApp } from '@react-native-firebase/app'; +import { getMessaging, setBackgroundMessageHandler } 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 => { +// ✅ Ambil instance messaging modular +const mess = getMessaging(getApp()); +// ✅ Firebase background handler — wajib diletakkan DI SINI +setBackgroundMessageHandler(mess, async remoteMessage => { const screen = remoteMessage?.data?.category; const content = remoteMessage?.data?.content; diff --git a/lib/useNotification.ts b/lib/useNotification.ts index fcc22d3..f23120b 100644 --- a/lib/useNotification.ts +++ b/lib/useNotification.ts @@ -1,6 +1,6 @@ -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 { getMessaging, registerDeviceForRemoteMessages, setAutoInitEnabled } from '@react-native-firebase/messaging'; +import * as Notifications from 'expo-notifications'; import { useEffect } from 'react'; import { PermissionsAndroid, Platform } from 'react-native'; @@ -20,20 +20,8 @@ const initializeFirebase = async () => { try { const app = getApps().length ? getApp() : initializeApp(RNfirebaseConfig); const mess = getMessaging(app); - - await messaging().registerDeviceForRemoteMessages(); - - // Set auto initialization and background message handler - mess.setAutoInitEnabled(true); - // 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 })); - // } - // }); - + await registerDeviceForRemoteMessages(mess); + setAutoInitEnabled(mess, true); return mess } catch (error) { console.error('Failed to initialize Firebase:', error); @@ -53,8 +41,10 @@ export const requestPermission = async () => { } return false } + } else if (Platform.OS === 'ios') { + const { status } = await Notifications.requestPermissionsAsync(); + return status === 'granted'; } - return true } catch (err) { console.warn('Error requesting notification permissions:', err); } @@ -62,8 +52,8 @@ export const requestPermission = async () => { export const getToken = async () => { try { - await initializeFirebase(); - const token = await messaging().getToken(); + const mess = await initializeFirebase(); + const token = await mess?.getToken(); return token; } catch (error) { console.error("Error getting token:", error); @@ -74,8 +64,6 @@ export const useNotification = () => { useEffect(() => { const initializeAndSetup = async () => { try { - // await initializeFirebase(); - // await requestPermission(); await getToken(); } catch (error) { console.error('Failed to setup notifications:', error); diff --git a/providers/AuthProvider.tsx b/providers/AuthProvider.tsx index 981b8ee..95ab880 100644 --- a/providers/AuthProvider.tsx +++ b/providers/AuthProvider.tsx @@ -56,6 +56,7 @@ export default function AuthProvider({ children }: { children: ReactNode }): Rea const permission = await requestPermission() if (permission) { try { + // COMING SOON if (Platform.OS === 'android') { const tokenDevice = await getToken() const register = await apiRegisteredToken({ user: hasil, token: String(tokenDevice) }) @@ -78,8 +79,9 @@ export default function AuthProvider({ children }: { children: ReactNode }): Rea const signOut = useCallback(async () => { try { const hasil = await decryptToken(String(tokenRef.current)) - const token = await getToken() + // COMING SOON if (Platform.OS === 'android') { + const token = await getToken() const response = await apiUnregisteredToken({ user: hasil, token: String(token) }) } } catch (error) {