diff --git a/__tests__/withAndroidPushNotifications.test.ts b/__tests__/withAndroidPushNotifications.test.ts index f57b808a..4f3cdd83 100644 --- a/__tests__/withAndroidPushNotifications.test.ts +++ b/__tests__/withAndroidPushNotifications.test.ts @@ -351,6 +351,70 @@ dependencies { warnSpy.mockRestore(); }); + + test('detects existing FCM service when action is a single object (not array)', () => { + const config = createMockConfig('com.example.myapp'); + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(); + + config.modResults.manifest.application[0].service.push({ + '$': { + 'android:name': '.ExistingFcmService', + 'android:exported': 'true', + }, + 'intent-filter': [ + { + action: { + $: { + 'android:name': 'com.google.firebase.MESSAGING_EVENT', + }, + }, + }, + ], + } as any); + + withAndroidPushNotifications(config as any, {} as any); + + const services = config.modResults.manifest.application[0].service; + expect(services).toHaveLength(1); + expect(services[0].$['android:name']).toBe('.ExistingFcmService'); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('existing FirebaseMessagingService') + ); + + warnSpy.mockRestore(); + }); + + test('detects existing FCM service when intent-filter is a single object (not array)', () => { + const config = createMockConfig('com.example.myapp'); + const warnSpy = jest.spyOn(console, 'warn').mockImplementation(); + + config.modResults.manifest.application[0].service.push({ + '$': { + 'android:name': '.ExistingFcmService', + 'android:exported': 'true', + }, + 'intent-filter': { + action: [ + { + $: { + 'android:name': 'com.google.firebase.MESSAGING_EVENT', + }, + }, + ], + }, + } as any); + + withAndroidPushNotifications(config as any, {} as any); + + const services = config.modResults.manifest.application[0].service; + expect(services).toHaveLength(1); + expect(services[0].$['android:name']).toBe('.ExistingFcmService'); + expect(warnSpy).toHaveBeenCalledWith( + expect.stringContaining('existing FirebaseMessagingService') + ); + + warnSpy.mockRestore(); + }); }); describe('error handling', () => { diff --git a/src/expo-plugins/withAndroidPushNotifications.ts b/src/expo-plugins/withAndroidPushNotifications.ts index 118a371c..8bf765bb 100644 --- a/src/expo-plugins/withAndroidPushNotifications.ts +++ b/src/expo-plugins/withAndroidPushNotifications.ts @@ -152,13 +152,17 @@ const registerServiceInManifest: ConfigPlugin = ( const hasExistingFcmService = mainApplication.service?.some( (s) => s.$?.['android:name'] !== serviceName && - s['intent-filter']?.some( - (f: any) => - f.action?.some( - (a: any) => - a.$?.['android:name'] === 'com.google.firebase.MESSAGING_EVENT' - ) - ) + ([] as any[]) + .concat(s['intent-filter'] ?? []) + .some((f: any) => + ([] as any[]) + .concat(f.action ?? []) + .some( + (a: any) => + a.$?.['android:name'] === + 'com.google.firebase.MESSAGING_EVENT' + ) + ) ); if (hasExistingFcmService) {