在iOS(快速)应用程序中,第二个Firebase应用程序的注册无法接收远程通知 [英] In iOS (swift) app, registration of a second Firebase app can't receive remote notifications

查看:159
本文介绍了在iOS(快速)应用程序中,第二个Firebase应用程序的注册无法接收远程通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个Firebase项目(使用相同的帐户,但可以是两个不同的帐户),并且每个项目都有一个注册了相同捆绑软件ID的iOS应用(因此,每个.plist文件具有不同的发件人ID和项目ID,但Bind ID相同).

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).

当我分别使用FirebaseApp.configure()和该应用程序的firebase配置.plist文件为每个应用程序配置Firebase消息传递时,它可以工作,我可以发送FCM消息,应用程序即可获取它. (这意味着Firebase控制台上的应用程序配置和Apple APNs密钥,plist文件均有效.)

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.)

但是,如果我要注册两个应用程序以使多个发件人可以向我的应用程序发送消息,则只有注册为__FIRAPP_DEFAULT的应用程序可以接收消息.

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.

我的配置代码是:

// 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!)

打印:

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

然后,当我使用以下函数从AppDelegate获取令牌时:

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

如果我同时将两个应用程序都配置为自定义应用程序名称,而在"__FIRAPP_DEFAULT"上均未配置任何应用程序,则即使第一个应用程序也不会收到任何消息.

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.

我的猜测是,只有"__FIRAPP_DEFAULT"应用可以接收FCM消息.你能确认或告诉我我哪里错了吗? 如果只有默认应用程序可以处理通知,那么能够为同一iOS应用程序中的多个应用程序配置Firebase有什么用?

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 ?

推荐答案

如果两个Firebase应用程序具有相同的Bunder ID和推送认证,则可以通过推送通知获取FCM令牌. 由于retrieveFCMToken不起作用,我认为您在调用retrieveFCMToken之前忘记为FIRMessaging设置APNSToken

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);
}];

此外,如果默认应用未配置,FIRMessaging将为nil,则可以使用 tokenWithAuthorizedEntity 函数使第二个应用正常工作.

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);
}];

这篇关于在iOS(快速)应用程序中,第二个Firebase应用程序的注册无法接收远程通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆