【问题标题】:The default Firebase app has not yet been configured but FirebaseApp.configure() added to appdelegate's didFinishLaunchingWithOptions method默认 Firebase 应用尚未配置,但 FirebaseApp.configure() 添加到 appdelegate 的 didFinishLaunchingWithOptions 方法
【发布时间】:2021-09-22 13:19:49
【问题描述】:

我正在尝试在我的 ios 应用上配置 firebase 云消息传递。 根据 Firebase 文档的说明,我添加了这样的 firebase 配置(appdelegate.swift)

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { IQKeyboardManager.shared.enable = true

if #available(iOS 10.0, *) {
    UNUserNotificationCenter.current().delegate = self
    
    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()

Messaging.messaging().isAutoInitEnabled = true
Messaging.messaging().delegate = self

Messaging.messaging().token { token, error in
    if let error = error {
        print("Error fetching FCM registration token: \(error)")
    } else if let token = token {
        self.fcmId = token
        print("\n My FCM registration token: \(token) \n")
    }
}

FirebaseApp.configure()

//For check update
Siren.shared.wail()

if UserDefaults.standard.value(forKey: "login") as? String == "yes" {
    
    let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
    let nextViewController = storyBoard.instantiateViewController(withIdentifier: "MainViewController") as! MainViewController
    let navigationController = UINavigationController(rootViewController: nextViewController)
    let appdelegate = UIApplication.shared.delegate as! AppDelegate
    appdelegate.window!.rootViewController = navigationController
    self.window?.makeKeyAndVisible()
    
}else{
    
    let mainStoryboardIpad : UIStoryboard = UIStoryboard(name: "Main", bundle: nil)
    let initialViewControlleripad : UIViewController = mainStoryboardIpad.instantiateViewController(withIdentifier: "goToLogin") as! LoginViewController
    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = initialViewControlleripad
    self.window?.makeKeyAndVisible()
}
return true

}

当我在 iPhone 6 上运行该应用程序时,我得到了。

[Firebase/Core][I-COR000003] 默认 Firebase 应用尚未配置。将FirebaseApp.configure() 添加到您的应用程序初始化中。这可以在 App Delegate 的应用程序中完成(_:didFinishLaunchingWithOptions:)(or the@main` 结构在 SwiftUI 中的初始化程序)。

【问题讨论】:

    标签: swift firebase-cloud-messaging appdelegate


    【解决方案1】:

    FirebaseApp.configure() 移到顶部,因为在调用任何 Firebase 方法之前应该首先调用它。

    【讨论】:

    • 现在我得到 8.3.0 - [Firebase/Messaging][I-FCM002022] 在检索发件人 ID '454886066833' 的 FCM 令牌之前未设置 APNS 设备令牌。此 FCM 令牌的通知将不会通过 APNS 传递。请确保在设置 APNS 设备令牌后重新检索 FCM 令牌。
    • 您是否在 Xcode 项目目标的 Signing & Capabilities 选项卡中添加了推送通知功能?
    • 是的,在后台模式下,远程通知被选中
    • 好的,那么您应该在检索 FCM 令牌之前设置 APNS 设备令牌,如错误本身所述。
    • 是的,我已经添加了`func messages(_messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) { print("Firebase registration token: (String(describing: fcmToken))") let dataDict: [字符串:字符串] = [“令牌”:fcmToken ?? ""] NotificationCenter.default.post(name: Notification.Name("FCMToken"), object: nil, userInfo: dataDict) } `
    猜你喜欢
    • 2018-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-04-02
    • 2018-12-01
    • 2016-09-16
    • 1970-01-01
    • 2019-11-09
    • 1970-01-01
    相关资源
    最近更新 更多