'In iOS (swift) app, registration of a second Firebase app can't receive remote notifications

I'm having two Firebase projects (on se same account, but could be two different accounts) and each project have an iOS app registered with the same bundle ID (so each .plist files have different sender IDs and project ID, but Bindle ID are the same).

When I configure Firebase messaging for each app separately using FirebaseApp.configure() and the firebase configuration .plist file for the app, it works, I can send FCM message and the app gets it. (That means that both apps configurations on the Firebase console and Apple APNs keys, plist files are valid.)

But If I want to register both apps to enable multiple senders to send messages to my app, only the one registered as __FIRAPP_DEFAULT receives messages.

My configuration code is :

// this is configuration for the default "__FIRAPP_DEFAULT" using the base GoogleService-Info.plist file
FirebaseApp.configure()

// This is the second app configuration
let bundle = Bundle(for: type(of: self))
let path = bundle.path(forResource: "GoogleService-Info-second-app", ofType: "plist")
let options = FirebaseOptions.init(contentsOfFile: path!)
if options != nil {
  FirebaseApp.configure(name: "app2", options: options!)
}

I can verify that both apps are well configured :

let apps = FirebaseApp.allApps
debugPrint(apps!)

Prints :

["app2": <FIRApp: 0x1704585a0>, "__FIRAPP_DEFAULT": <FIRApp: 0x17044a9b0>]

Then when I get the tokens from my AppDelegate using the following function:

func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    let d = NSData(data: deviceToken)
    print(d)
    Messaging.messaging().retrieveFCMToken(forSenderID: firstAppSenderIdString) { (message, error) in
        print("FCM token for app1: \(message!)")
    }
    Messaging.messaging().retrieveFCMToken(forSenderID: secondAppSenderIdString) { (message, error) in
        print("FCM token for app2: \(message!)")
    }
}

I get both valid tokens :

FCM token for app1: fwjJVG2-T9Q:APA91bG8xVN9S-F4aERzh0GtcLWAqOy3dBPed0vPUE4AS_Jt4rau1bmmhvGPjfQgwBt9krdI9e91GaA04x4PXm4eW9PsG52P-Vt8yeo2woWGl3CP6zE09cT8ouRmOoWBhFfZkLbzbGul
FCM token for app2: fwjJVG2-T9Q:APA91bGggGD0YBZO5tpDqwdYKEbGX4vTSXnhwFoc_lrHbLIIWg1WE4RjTS8NYZ--TX-GkoypuEM4Plb4h41mVcP0uYjvo2dGDO3SNyuo3GrsBRb4ISzoieC_bcJZs5MibLKrET97f49j

Also if I configure both apps on a custom app name and none on "__FIRAPP_DEFAULT" I don't get any messages even for the first one.

My guess is that only "__FIRAPP_DEFAULT" app can receive FCM messages. Could you confirm or tell me where I'm wrong ? If only the default app can handle notifications, what's the use of being able to configure Firebase for more than one app in the same iOS application ?



Solution 1:[1]

If both firebase app are same bunder id and push certification, you can get FCM token work with push notification. Because retrieveFCMToken don't work, I think you forget set APNSToken for FIRMessaging before call retrieveFCMToken

[FIRMessaging messaging].APNSToken = deviceToken;
[[FIRMessaging messaging] retrieveFCMTokenForSenderID:self.firstAppSenderIdString completion:^(NSString * _Nullable FCMToken, NSError * _Nullable error) {
    NSLog(@"FCM token for app1: %@ - error: %@", FCMToken, error);
}];

In addition, if the default app not config, FIRMessaging will be nil, let second app work you can using tokenWithAuthorizedEntity function.

[[FIRInstanceID instanceID] tokenWithAuthorizedEntity:self.secondAppSenderIdString scope:kFIRInstanceIDScopeFirebaseMessaging options:@{@"apns_token": deviceToken} handler:^(NSString * _Nullable token, NSError * _Nullable error) {
    NSLog(@"FCM token for app2: %@ - error: %@", token, error);
}];

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1