11import * as Notifications from "expo-notifications" ;
2- import messaging , {
2+ import {
33 FirebaseMessagingTypes ,
4+ getToken ,
5+ onMessage ,
6+ setBackgroundMessageHandler ,
7+ onNotificationOpenedApp ,
8+ getInitialNotification ,
9+ requestPermission ,
10+ getMessaging ,
411} from "@react-native-firebase/messaging" ;
512import { Alert , PermissionsAndroid , Platform } from "react-native" ;
13+ import { getApp as rnGetApp } from "@react-native-firebase/app" ;
614import * as Device from "expo-device" ;
715import { NOTIFICATION_STRINGS } from "../constants/Notifications" ;
816import { getFirebaseApp } from "./firebase.utils" ;
@@ -15,17 +23,18 @@ export const testFirebaseMessaging = async () => {
1523 // Test Firebase initialization
1624 const app = getFirebaseApp ( ) ;
1725 console . log ( "Firebase app:" , app . name , app . options . projectId ) ;
18-
19- // Test messaging instance
20- const messagingInstance = messaging ( ) ;
21- console . log ( "Messaging instance created successfully" ) ;
22-
26+
27+ // Test messaging modular API
28+ console . log ( "Using React Native Firebase modular messaging API" ) ;
29+
30+ const messagingInstance = getMessaging ( rnGetApp ( ) ) ;
31+
2332 // Check if we can get an FCM token
24- const token = await messagingInstance . getToken ( ) ;
33+ const token = await getToken ( messagingInstance ) ;
2534 console . log ( "FCM token:" , token ? token . substring ( 0 , 20 ) + "..." : "null" ) ;
26-
35+
2736 // Check authorization status
28- const authStatus = await messagingInstance . requestPermission ( ) ;
37+ const authStatus = await requestPermission ( messagingInstance ) ;
2938 console . log ( "FCM authorization status:" , authStatus ) ;
3039
3140 return {
@@ -138,7 +147,9 @@ export const handleNotification = async (
138147export const handleForegroundNotification = ( ) => {
139148 console . log ( "Setting up foreground notification handler..." ) ;
140149
141- const unsubscribe = messaging ( ) . onMessage ( async ( remoteMessage ) => {
150+ const messagingInstance = getMessaging ( rnGetApp ( ) ) ;
151+
152+ const unsubscribe = onMessage ( messagingInstance , async ( remoteMessage : FirebaseMessagingTypes . RemoteMessage ) => {
142153 console . log ( "Foreground FCM message received:" , JSON . stringify ( remoteMessage , null , 2 ) ) ;
143154
144155 try {
@@ -173,21 +184,21 @@ export const handleForegroundNotification = () => {
173184
174185// Handle background & quit-state notifications
175186export const handleBackgroundNotifications = ( ) => {
176- messaging ( ) . setBackgroundMessageHandler ( async ( remoteMessage ) => {
187+ const messagingInstance = getMessaging ( rnGetApp ( ) ) ;
188+
189+ setBackgroundMessageHandler ( messagingInstance , async ( remoteMessage : FirebaseMessagingTypes . RemoteMessage ) => {
177190 console . log ( "Background message received:" , remoteMessage ) ;
178191 } ) ;
179192
180- messaging ( ) . onNotificationOpenedApp ( ( remoteMessage ) => {
193+ onNotificationOpenedApp ( messagingInstance , ( remoteMessage : FirebaseMessagingTypes . RemoteMessage | null ) => {
181194 console . log ( "Notification opened from background:" , remoteMessage ) ;
182195 } ) ;
183196
184- messaging ( )
185- . getInitialNotification ( )
186- . then ( ( remoteMessage ) => {
187- if ( remoteMessage ) {
188- console . log ( "Notification opened from quit state:" , remoteMessage ) ;
189- }
190- } ) ;
197+ getInitialNotification ( messagingInstance ) . then ( ( remoteMessage ) => {
198+ if ( remoteMessage ) {
199+ console . log ( "Notification opened from quit state:" , remoteMessage ) ;
200+ }
201+ } ) ;
191202} ;
192203
193204export const savePushToken = async ( token : string ) : Promise < void > => {
0 commit comments