使用 Firebase 云消息传递启动应用时出错



我正在尝试将 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)
    }

发生了什么,如何修复此错误?

我试图按照这里的说明进行操作...

你不能

有这一行

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

函数外部 - 它将在初始化应用程序委托时调用,即在调用 didFinishLaunching 之前 - 这意味着Auth.auth()在调用FireBaseApp.configure()之前正在初始化,如异常所述。

我建议:

var currentUser: String?
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
    self.currentUser = Auth.auth().currentUser?.uid
    return true
}

相关内容

  • 没有找到相关文章

最新更新