首页 > 解决方案 > iOS 无法通过 FCM 接收推送通知(可以通过 APNs)

问题描述

我正要在stackoverflow上问这个问题,但我只是想通了。为了后代,我还是发布了这个,因为这个错误花了我几天的时间,我在这个网站上找不到任何提及它的地方。

在制作了两种不同的方案并根据 AppDelegate 中的方案选择不同的 plist 后,我​​无法通过 FCM 发送推送通知。选择我的 plist 的代码如下所示:

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {

    let defaults = UserDefaults.standard
    // NOTE: DONT call registrationDataManager here or any other VC that uses Firebase before it's configured
    let gcmMessageIDKey = "gcm.message_id"
    let window: UIWindow? = nil

//XXX OVERRIDE INIT BREAKS SWIZZLING -- DO NOT USE
    override init() {
        super.init()
        let buildFor = ProcessInfo.processInfo.environment["BUILD_FOR"]! as String
        var firebasePlistFileName = "GoogleService-Info"

        if buildFor == "PROD" {
            //firebasePlistFileName = "GoogleService-Info-Production"
            firebasePlistFileName = "RELEASE-NEW-GoogleService-Info"
            print("--- USING PRODUCTION / RELEASE PLIST")
        }
        else {
            firebasePlistFileName = "DEBUG-NEW-GoogleService-Info"
            print("--- USING DEBUG PLIST")
        }

        let filePath = Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist")
        guard let fileopts = FirebaseOptions(contentsOfFile: filePath!) else {
                assert(false, "Couldn't load config file")
                return
        }

        FirebaseApp.configure(options: fileopts)
    }
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//       FirebaseApp.configure()
}

如果您尝试执行上述操作,您可能会注意到一个类似于

[AppDelegateSwizzler] App Delegate does not conform to UIApplicationDelegate protocol firebase

标签: iosswiftfirebasefirebase-cloud-messagingswift5

解决方案


你应该做什么:

class AppDelegate: UIResponder, UIApplicationDelegate, UNUserNotificationCenterDelegate, MessagingDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        
        let buildFor = ProcessInfo.processInfo.environment["BUILD_FOR"]! as String
        var firebasePlistFileName = "GoogleService-Info"

        if buildFor == "PROD" {
            //firebasePlistFileName = "GoogleService-Info-Production"
            firebasePlistFileName = "RELEASE-NEW-GoogleService-Info"
            print("--- USING PRODUCTION / RELEASE PLIST")
        }
        else {
            firebasePlistFileName = "DEBUG-NEW-GoogleService-Info"
            print("--- USING DEBUG PLIST")
        }

        let filePath = Bundle.main.path(forResource: firebasePlistFileName, ofType: "plist")
        guard let fileopts = FirebaseOptions(contentsOfFile: filePath!) else {
                assert(false, "Couldn't load config file")
        }

        FirebaseApp.configure(options: fileopts)

        }
//....

这是假设您已进入方案,已编辑方案以具有环境变量 BUILD_FOR 并将其设置为 PROD 用于您的生产/发布方案,然后将基于此指向正确的 GoogleServices-Info plist 文件名。这是使用 Firebase 实现多个 plist 而不搞砸一切的一种方法。

要点 - 不要使用 override init 除非你想自己修复 swizzling 或者你比我更了解你在做什么。


推荐阅读