我在使用APNS的项目中使用推送通知。
推送通知在添加和配置Firebase SDK之前运行良好。
我注意到在添加Firebase后,函数:didRegisterForRemoteNotificationsWithDeviceToken
未被调用。
我不知道在Firebase中是否有必须禁用的内容,如果我删除了Firebase设置(FirebaseApp.configure()),则会再次调用函数didRegisterForRemoteNotificationsWithDeviceToken
。
我的代码:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ... existing code ...
registerForPushNotifications()
return true
}
func registerForPushNotifications() {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
}
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
print("Device Token: \(token)")
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
print("Failed to register: \(error)")
}
答案 0 :(得分:1)