【问题标题】:FirebaseApp.configure() Thread 1: Signal SIGBARTFirebaseApp.configure() 线程 1:信号 SIGABRT
【发布时间】:2018-11-05 22:43:46
【问题描述】:

我正在使用 Firebase 和 Cloud Messaging 做一些事情。

问题是我需要在 FirebaseApp.configure() 之前调用 application.registerForRemoteNotifications()。 我读过它应该可以解决我的问题。

我使用从雇主那里得到的项目,FirebaseApp.configure() 是这样调用的:

override init() {
    super.init()
    FirebaseApp.configure()
}

我不知道为什么会这样。

并且 application.registerForRemoteNotifications() 在 application(didFinishLaunchingWithOptions) 中被调用。这是我所拥有的:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    //        FirebaseApp.configure()
    GMSPlacesClient.provideAPIKey("AIzaSyCWMU53OSR4zO28i9e2BsASnda3X1TAS2Y")
    GMSServices.provideAPIKey("AIzaSyCWMU53OSR4zO28i9e2BsASnda3X1TAS2Y")
    UIApplication.shared.setStatusBarStyle(UIStatusBarStyle.lightContent, animated: true)


    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.clear], for: .normal)
    UITabBarItem.appearance().setTitleTextAttributes([NSAttributedStringKey.foregroundColor:UIColor.clear], for: .selected)




    print("Token is ", Messaging.messaging().fcmToken)


    Thread.sleep(forTimeInterval: 1.0)

    if #available(iOS 10, *) {
        UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate

        Messaging.messaging().delegate = self

        UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, err) in
            //                application.registerForRemoteNotifications()
        })
    } else {
        let notificationSettings = UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil)
        UIApplication.shared.registerUserNotificationSettings(notificationSettings)
        UIApplication.shared.registerForRemoteNotifications()
    }

    application.registerForRemoteNotifications()

    return true
}

问题是,当我尝试将FirebaseApp.configure() 放在application(didFinishLaunchingWithOptions) 的开头时,我得到了

线程 1:信号 SIGABRT 错误。

从控制台我得到

由于未捕获的异常而终止应用程序 'NSInternalInconsistencyException',原因:'默认的 FIRApp 必须先配置实例,然后才能使用默认的 FIRAuthinstance 初始化。确保这一点的一种方法是致电[FIRApp configure];application:didFinishLaunchingWithOptions: 中调用。'

我无法理解原因。

【问题讨论】:

    标签: ios swift firebase firebase-cloud-messaging


    【解决方案1】:

    首先,FirebaseApp.configure() 必须位于 didFinishLaunchingWithOptions

    的第一行
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        FirebaseApp.configure()
        ...
    

    .

    在那之后,你做所有的注册事情。

        ...
        // Thread.sleep(forTimeInterval: 1.0) // why do you need a sleep here?
        if #available(iOS 10, *) {
            UNUserNotificationCenter.current().delegate = self as? UNUserNotificationCenterDelegate
            Messaging.messaging().delegate = self
            UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: { (granted, err) in
               application.registerForRemoteNotifications()
            })
        } else {
            let notificationSettings = UIUserNotificationSettings(types: [.badge, .alert, .sound], categories: nil)
            UIApplication.shared.registerUserNotificationSettings(notificationSettings)
            UIApplication.shared.registerForRemoteNotifications()
        }
        return true
    }
    

    【讨论】:

    • 我仍然得到 Thread 1: Signal SIGABRT 与控制台中的相同输出
    猜你喜欢
    • 1970-01-01
    • 2021-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多