我正在使用Firebase和云消息传递。
问题是我需要在FirebaseApp.configure()之前调用application.registerForRemoteNotifications()。 我读过它应该可以解决我的问题。
我使用从雇主处获得的项目,FirebaseApp.configure()以这种方式调用:
override init() {
super.init()
FirebaseApp.configure()
}
我不知道为什么会这样。
在application(didFinishLaunchingWithOptions)中调用application.registerForRemoteNotifications()。我在这里:
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:
中调用。
我无法理解原因。
答案 0 :(得分:0)
首先, 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
}