【发布时间】: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