如何在App Delegate中同时正确配置Pusher Beams SDK与Firebase?
解决Firebase与Pusher Beams推送SDK共存配置方案
冲突原因
Firebase SDK默认开启了AppDelegate代理自动劫持能力,会优先接管APNS相关的系统回调,导致Pusher Beams SDK无法正常接收APNS注册、通知到达的回调事件,因此只有注释Firebase初始化代码时Beams才能正常工作。
具体配置步骤
- 第一步:禁用Firebase的自动代理劫持
在项目的Info.plist中添加如下配置项:
<key>FirebaseAppDelegateProxyEnabled</key> <false/>
如果是可视化编辑Info.plist,对应添加类型为Boolean的FirebaseAppDelegateProxyEnabled字段,值设为NO。
- 第二步:调整
didFinishLaunchingWithOptions内的初始化顺序
优先初始化Firebase,再执行Beams的相关配置,修改后代码如下:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool { // 先初始化Firebase FirebaseApp.configure() // 再初始化Beams相关配置 self.pushNotifications.start(instanceId: "MyInstanceID") self.pushNotifications.registerForRemoteNotifications() try? self.pushNotifications.addDeviceInterest(interest: "debug-hello") return true }
- 第三步:补全远程通知回调的处理逻辑
现有回调代码缺少非Firebase通知场景下的completionHandler调用,调整后代码如下:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) { print("RECEIVED NOTIFICATION") self.pushNotifications.handleNotification(userInfo: userInfo) if Auth.auth().canHandleNotification(userInfo){ completionHandler(.noData) return } // 非Firebase通知必须调用完成回调 completionHandler(.newData) } func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) { print("Did register for remote notifications") // 两个SDK都要注册DeviceToken,顺序不影响 self.pushNotifications.registerDeviceToken(deviceToken) // 测试环境下请将type改为.sandbox,生产环境用.prod Auth.auth().setAPNSToken(deviceToken, type: .prod) }
验证注意事项
- 确保APNS推送证书同时正确上传到Firebase控制台和Pusher Beams控制台,环境(开发/生产)要和当前打包类型匹配
- 测试推送时不要用Xcode的模拟器,需要用真机测试
内容的提问来源于stack exchange,提问作者Micheal S. Bingham
相关产品推荐
相关产品推荐

