【问题标题】:iOS cannot receive push notifications through FCM (can through APNs)iOS 无法通过 FCM 接收推送通知(可以通过 APNs)
【发布时间】:2020-08-14 17:27:51
【问题描述】:

我正要在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

【问题讨论】:

    标签: ios swift firebase firebase-cloud-messaging swift5


    【解决方案1】:

    你应该做什么:

    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 或者你比我更了解你在做什么。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-22
      • 2017-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-07
      相关资源
      最近更新 更多