使用Firebase云消息传递启动应用程序时发生错误

时间:2019-05-28 00:19:08

标签: ios swift firebase firebase-cloud-messaging

我正在尝试将Firebase云消息传递集成到我的应用程序中,以便向用户发送通知。尝试启动应用程序时,出现以下错误:

    *** First throw call stack:
(0x1e024927c 0x1df4239f8 0x1e01534b0 0x102a31268 0x102995540 0x1029956f8 0x20cb84088 0x20cb83944 0x102995878 0x1dfc9a8e0)
libc++abi.dylib: terminating with uncaught exception of type NSException

这发生在这里:

    [NSException raise:NSInternalInconsistencyException

在FIRAuth.m。

  [NSException raise:NSInternalInconsistencyException
                format:@"The default FIRApp instance must be configured before the default FIRAuth"
                       @"instance can be initialized. One way to ensure that is to call "
                       @"`[FIRApp configure];` (`FirebaseApp.configure()` in Swift) in the App "
                       @"Delegate's `application:didFinishLaunchingWithOptions:` "
                       @"(`application(_:didFinishLaunchingWithOptions:)` in Swift)."];

这是我大多数应用程序代表的样子:

      let currentUser = Auth.auth().currentUser!.uid

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        Messaging.messaging().delegate = self as! MessagingDelegate

//        InstanceID.instanceID().instanceID { (result, error) in
//            if let error = error {
//                print("Error fetching remote instange ID: \(error)")
//            } else if let result = result {
//                print("Remote instance ID token: \(result.token)")
//                let ref = Database.database().reference()
//                ref.child("UsersFireTokens").child(self.currentUser).child("token").setValue(result.token)
//            }
//        }

        return true
    }

    func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey : Any]? = nil) -> Bool {
        if #available(iOS 10.0, *) {//for notif's
            // For iOS 10 display notification (sent via APNS)
            UNUserNotificationCenter.current().delegate = self as! UNUserNotificationCenterDelegate

            let authOptions: UNAuthorizationOptions = [.alert, .badge, .sound]
            UNUserNotificationCenter.current().requestAuthorization(
                options: authOptions,
                completionHandler: {_, _ in })
        } else {
            let settings: UIUserNotificationSettings =
                UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
            application.registerUserNotificationSettings(settings)
        }

        application.registerForRemoteNotifications()
        return true
    }

    func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
        Messaging.messaging().apnsToken = deviceToken
        InstanceID.instanceID().instanceID { (result, error) in
            if error == nil {
                print("InstanceID token: \(result)")
//                let ref = Database.database().reference()
//                ref.child("UsersFireTokens").child(self.currentUser).child("token").setValue(result!)
            } else {
                print(error, ": Error in getting token for Fire")
            }
        }
//        if let refreshedToken = InstanceID.instanceID().token() {
//            print("InstanceID token: \(refreshedToken)")
//        }
    }

    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String) {
        print("Firebase registration token: \(fcmToken)")

        let dataDict:[String: String] = ["token": fcmToken]
        NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict)
        // TODO: If necessary send token to application server.
        let ref = Database.database().reference()
        ref.child("UsersFireTokens").child(self.currentUser).child("token").setValue(dataDict["token"]!)
        // Note: This callback is fired at each app startup and whenever a new token is generated.
    }

    // MARK: - MessagingDelegate

    func messaging(_ messaging: Messaging, didRefreshRegistrationToken fcmToken: String) {
        print(fcmToken, ": fcmToken")
        let ref = Database.database().reference()
        ref.child("UsersFireTokens").child(self.currentUser).child("token").setValue(fcmToken)

    }

发生了什么事,我该如何解决该错误?

我正在尝试按照说明here...

1 个答案:

答案 0 :(得分:1)

您不能有此行

let currentUser = Auth.auth().currentUser!.uid

函数外-在初始化didFinishLaunching之前初始化App Delegate时将调用它-这意味着Auth.auth() 正在初始化后再调用{ {1}}是例外。

我建议:

FireBaseApp.configure()