11import type RNFBMessaging from '@react-native-firebase/messaging' ;
2+ import type { FirebaseMessagingTypes } from '@react-native-firebase/messaging' ;
23import { Platform } from 'react-native' ;
34import type * as Permissions from 'react-native-permissions' ;
45
56import type { NotificationServiceInterface } from './types' ;
67
8+ // Type definitions for Firebase Messaging Module instance
9+ // This represents the instance returned by messaging() or getMessaging()
10+ // We use a type alias to handle both v14 and v22+ types
11+ type MessagingInstance = ReturnType < typeof RNFBMessaging > ;
12+
13+ // Type definitions for modular API support (Firebase v22+)
14+ // The modular API provides standalone functions that accept a messaging instance
15+ type ModularMessagingType = {
16+ getMessaging : ( ) => MessagingInstance ;
17+ getAPNSToken : ( messaging : MessagingInstance ) => Promise < string | null > ;
18+ getToken : ( messaging : MessagingInstance ) => Promise < string > ;
19+ hasPermission : ( messaging : MessagingInstance ) => Promise < FirebaseMessagingTypes . AuthorizationStatus > ;
20+ requestPermission : (
21+ messaging : MessagingInstance ,
22+ iosPermissions ?: FirebaseMessagingTypes . IOSPermissions ,
23+ ) => Promise < FirebaseMessagingTypes . AuthorizationStatus > ;
24+ onTokenRefresh : ( messaging : MessagingInstance , listener : ( token : string ) => void ) => ( ) => void ;
25+ AuthorizationStatus : typeof RNFBMessaging . AuthorizationStatus ;
26+ } ;
27+
28+ // Create a flexible type that can handle both v14 and v22+ Firebase modules
29+ type FirebaseMessagingModule = typeof RNFBMessaging | ModularMessagingType ;
30+
31+ const isModularAPI = ( module : FirebaseMessagingModule ) : module is ModularMessagingType => {
32+ return typeof ( module as ModularMessagingType ) . getMessaging === 'function' ;
33+ } ;
34+
735const createNativeNotificationService = ( {
836 messagingModule,
937 permissionModule,
1038} : {
11- messagingModule : typeof RNFBMessaging ;
39+ messagingModule : FirebaseMessagingModule ;
1240 permissionModule : typeof Permissions ;
1341} ) : NotificationServiceInterface => {
14- const module = messagingModule ( ) ;
42+ const isModular = isModularAPI ( messagingModule ) ;
43+ const modularMessaging = isModular ? ( messagingModule as ModularMessagingType ) : null ;
44+ const messaging = isModular ? modularMessaging ! . getMessaging ( ) : ( messagingModule as typeof RNFBMessaging ) ( ) ;
45+
1546 const authorizedStatus = [
1647 messagingModule . AuthorizationStatus . AUTHORIZED ,
1748 messagingModule . AuthorizationStatus . PROVISIONAL ,
1849 ] ;
1950 return {
2051 getAPNSToken ( ) : Promise < string | null > {
21- return module . getAPNSToken ( ) ;
52+ if ( modularMessaging ) {
53+ return modularMessaging . getAPNSToken ( messaging ) ;
54+ }
55+ return messaging . getAPNSToken ( ) ;
2256 } ,
2357 getFCMToken ( ) : Promise < string | null > {
24- return module . getToken ( ) ;
58+ if ( modularMessaging ) {
59+ return modularMessaging . getToken ( messaging ) ;
60+ }
61+ return messaging . getToken ( ) ;
2562 } ,
2663 async hasPushPermission ( ) : Promise < boolean > {
2764 if ( Platform . OS === 'android' ) {
@@ -30,7 +67,9 @@ const createNativeNotificationService = ({
3067 }
3168
3269 if ( Platform . OS === 'ios' ) {
33- const status = await module . hasPermission ( ) ;
70+ const status = modularMessaging
71+ ? await modularMessaging . hasPermission ( messaging )
72+ : await messaging . hasPermission ( ) ;
3473 return authorizedStatus . includes ( status ) ;
3574 }
3675
@@ -43,14 +82,21 @@ const createNativeNotificationService = ({
4382 }
4483
4584 if ( Platform . OS === 'ios' ) {
46- const status = await module . requestPermission ( ) ;
85+ const status = modularMessaging
86+ ? await modularMessaging . requestPermission ( messaging )
87+ : await messaging . requestPermission ( ) ;
4788 return authorizedStatus . includes ( status ) ;
4889 }
4990
5091 return false ;
5192 } ,
5293 onTokenRefresh ( handler : ( token : string ) => void ) : ( ) => void | undefined {
53- return module . onTokenRefresh ( ( token ) => {
94+ if ( modularMessaging ) {
95+ return modularMessaging . onTokenRefresh ( messaging , ( token : string ) => {
96+ if ( Platform . OS === 'android' ) handler ( token ) ;
97+ } ) ;
98+ }
99+ return messaging . onTokenRefresh ( ( token : string ) => {
54100 if ( Platform . OS === 'android' ) handler ( token ) ;
55101 } ) ;
56102 } ,
0 commit comments