upd: clear warning
Deskripsi: - update firebase > clear warning No Issues
This commit is contained in:
@@ -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,
|
||||
|
||||
9
index.js
9
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;
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user