upd: clear warning

Deskripsi:
- update firebase > clear warning

No Issues
This commit is contained in:
2025-08-08 11:26:09 +08:00
parent 7555ece0fa
commit 602860d9c3
4 changed files with 27 additions and 31 deletions

View File

@@ -14,7 +14,8 @@ 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 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 { 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";
@@ -51,16 +52,18 @@ export default function RootLayout() {
}, []); }, []);
useEffect(() => { 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 id = remoteMessage?.data?.id;
const category = remoteMessage?.data?.category; const category = remoteMessage?.data?.category;
const content = remoteMessage?.data?.content; const content = remoteMessage?.data?.content;
const title = remoteMessage?.notification?.title; const title = remoteMessage?.notification?.title;
if (remoteMessage.notification != undefined && remoteMessage.notification.title != undefined && remoteMessage.notification.body != undefined) { if (remoteMessage.notification?.title && remoteMessage.notification?.body) {
if (category == 'discussion-general' && pathname == '/discussion/' + content) { if (category === 'discussion-general' && pathname === '/discussion/' + content) {
return null return null;
} else if (pathname != `/${category}/${content}`) { } else if (pathname !== `/${category}/${content}`) {
Notifier.showNotification({ Notifier.showNotification({
title: title, title: title,
description: remoteMessage.notification?.body, description: remoteMessage.notification?.body,

View File

@@ -1,12 +1,15 @@
// index.js // index.js
import 'expo-router/entry'; // ⬅️ wajib ada agar expo-router tetap bekerja 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'; import AsyncStorage from '@react-native-async-storage/async-storage';
// ✅ Firebase background handler — wajib diletakkan DI SINI // ✅ Ambil instance messaging modular
messaging().setBackgroundMessageHandler(async remoteMessage => { const mess = getMessaging(getApp());
// ✅ Firebase background handler — wajib diletakkan DI SINI
setBackgroundMessageHandler(mess, async remoteMessage => {
const screen = remoteMessage?.data?.category; const screen = remoteMessage?.data?.category;
const content = remoteMessage?.data?.content; const content = remoteMessage?.data?.content;

View File

@@ -1,6 +1,6 @@
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 { getMessaging, registerDeviceForRemoteMessages, setAutoInitEnabled } from '@react-native-firebase/messaging';
import * as Notifications from 'expo-notifications';
import { useEffect } from 'react'; import { useEffect } from 'react';
import { PermissionsAndroid, Platform } from 'react-native'; import { PermissionsAndroid, Platform } from 'react-native';
@@ -20,20 +20,8 @@ const initializeFirebase = async () => {
try { try {
const app = getApps().length ? getApp() : initializeApp(RNfirebaseConfig); const app = getApps().length ? getApp() : initializeApp(RNfirebaseConfig);
const mess = getMessaging(app); const mess = getMessaging(app);
await registerDeviceForRemoteMessages(mess);
await messaging().registerDeviceForRemoteMessages(); setAutoInitEnabled(mess, true);
// 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 }));
// }
// });
return mess return mess
} catch (error) { } catch (error) {
console.error('Failed to initialize Firebase:', error); console.error('Failed to initialize Firebase:', error);
@@ -53,8 +41,10 @@ export const requestPermission = async () => {
} }
return false return false
} }
} else if (Platform.OS === 'ios') {
const { status } = await Notifications.requestPermissionsAsync();
return status === 'granted';
} }
return true
} catch (err) { } catch (err) {
console.warn('Error requesting notification permissions:', err); console.warn('Error requesting notification permissions:', err);
} }
@@ -62,8 +52,8 @@ export const requestPermission = async () => {
export const getToken = async () => { export const getToken = async () => {
try { try {
await initializeFirebase(); const mess = await initializeFirebase();
const token = await messaging().getToken(); const token = await mess?.getToken();
return token; return token;
} catch (error) { } catch (error) {
console.error("Error getting token:", error); console.error("Error getting token:", error);
@@ -74,8 +64,6 @@ export const useNotification = () => {
useEffect(() => { useEffect(() => {
const initializeAndSetup = async () => { const initializeAndSetup = async () => {
try { try {
// await initializeFirebase();
// await requestPermission();
await getToken(); await getToken();
} catch (error) { } catch (error) {
console.error('Failed to setup notifications:', error); console.error('Failed to setup notifications:', error);

View File

@@ -56,6 +56,7 @@ export default function AuthProvider({ children }: { children: ReactNode }): Rea
const permission = await requestPermission() const permission = await requestPermission()
if (permission) { if (permission) {
try { try {
// COMING SOON
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
const tokenDevice = await getToken() const tokenDevice = await getToken()
const register = await apiRegisteredToken({ user: hasil, token: String(tokenDevice) }) 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 () => { const signOut = useCallback(async () => {
try { try {
const hasil = await decryptToken(String(tokenRef.current)) const hasil = await decryptToken(String(tokenRef.current))
const token = await getToken() // COMING SOON
if (Platform.OS === 'android') { if (Platform.OS === 'android') {
const token = await getToken()
const response = await apiUnregisteredToken({ user: hasil, token: String(token) }) const response = await apiUnregisteredToken({ user: hasil, token: String(token) })
} }
} catch (error) { } catch (error) {