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 messaging = isModular ? messagingModule . getMessaging ( ) : ( messagingModule as typeof RNFBMessaging ) ( ) ;
44+
1545 const authorizedStatus = [
1646 messagingModule . AuthorizationStatus . AUTHORIZED ,
1747 messagingModule . AuthorizationStatus . PROVISIONAL ,
1848 ] ;
1949 return {
2050 getAPNSToken ( ) : Promise < string | null > {
21- return module . getAPNSToken ( ) ;
51+ if ( isModular ) {
52+ return ( messagingModule as ModularMessagingType ) . getAPNSToken ( messaging ) ;
53+ }
54+ return messaging . getAPNSToken ( ) ;
2255 } ,
2356 getFCMToken ( ) : Promise < string | null > {
24- return module . getToken ( ) ;
57+ if ( isModular ) {
58+ return ( messagingModule as ModularMessagingType ) . getToken ( messaging ) ;
59+ }
60+ return messaging . getToken ( ) ;
2561 } ,
2662 async hasPushPermission ( ) : Promise < boolean > {
2763 if ( Platform . OS === 'android' ) {
@@ -30,7 +66,9 @@ const createNativeNotificationService = ({
3066 }
3167
3268 if ( Platform . OS === 'ios' ) {
33- const status = await module . hasPermission ( ) ;
69+ const status = isModular
70+ ? await ( messagingModule as ModularMessagingType ) . hasPermission ( messaging )
71+ : await messaging . hasPermission ( ) ;
3472 return authorizedStatus . includes ( status ) ;
3573 }
3674
@@ -43,14 +81,21 @@ const createNativeNotificationService = ({
4381 }
4482
4583 if ( Platform . OS === 'ios' ) {
46- const status = await module . requestPermission ( ) ;
84+ const status = isModular
85+ ? await ( messagingModule as ModularMessagingType ) . requestPermission ( messaging )
86+ : await messaging . requestPermission ( ) ;
4787 return authorizedStatus . includes ( status ) ;
4888 }
4989
5090 return false ;
5191 } ,
5292 onTokenRefresh ( handler : ( token : string ) => void ) : ( ) => void | undefined {
53- return module . onTokenRefresh ( ( token ) => {
93+ if ( isModular ) {
94+ return ( messagingModule as ModularMessagingType ) . onTokenRefresh ( messaging , ( token : string ) => {
95+ if ( Platform . OS === 'android' ) handler ( token ) ;
96+ } ) ;
97+ }
98+ return messaging . onTokenRefresh ( ( token : string ) => {
5499 if ( Platform . OS === 'android' ) handler ( token ) ;
55100 } ) ;
56101 } ,
0 commit comments