ios - APNS 推送通知 iOS 13 - didRegisterForRemoteNotificationsWithDeviceToken 未被调用
问题描述
我发现一些帖子已经在堆栈溢出,但它们已经过时并且不再实际了。
我在实现推送通知时遇到问题。
Xcode 11、iOS 13、App 刚刚创建,因此证书适用于 Xcode 11 或更高版本,启用了推送通知和背景模式(处理、远程通知、获取)。
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
//Firebase Messaging
registerForPushNotifications()
UNUserNotificationCenter.current().delegate = self as UNUserNotificationCenterDelegate
Messaging.messaging().delegate = self as MessagingDelegate
// //Locationtracking
//
// guard let isLocationEnabled = UserDefaults.standard.value(forKey: "userEnabledLocationTracking") as? Bool else {locationManager.stopLocationTracking(); return true}
//
// isLocationEnabled ? locationManager.startLocationTracking() : locationManager.stopLocationTracking()
return true
}
之后我调用这两个函数:
func registerForPushNotifications() {
UNUserNotificationCenter.current()
.requestAuthorization(options: [.alert, .sound, .badge]) { [weak self] granted, error in
print("Permission granted: \(granted)")
guard granted else {return}
self?.getNotificationSettings()
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else {return}
DispatchQueue.main.async {
print("register called.......")
UIApplication.shared.registerForRemoteNotifications()
}
}
}
但是我的“didRegisterForRemoteNotificationsWithDeviceToken” - 委托方法没有被调用,我不明白为什么会发生这种情况。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
Auth.auth().setAPNSToken(deviceToken, type: .unknown)
Messaging.messaging().apnsToken = deviceToken
let tokenParts = deviceToken.map { data in String(format: "%02.2hhx", data) }
let token = tokenParts.joined()
print("Device Token: \(token)")
}
如果有人可以帮助我并告诉我我的错误在哪里,那就太好了。
祝你今天过得愉快!
解决方案
推荐阅读
- json - 如何显示嵌套的 JSON 数据?
- javascript - 我想在 forge 查看器中从 3 个不同方面创建 3 个屏幕截图
- reactjs - reactjs中的三元运算符多个动作
- pandas-groupby - 按另一列的唯一对计算一列的平均值
- node.js - 是否可以更改新任务的代理
- reactjs - tsx 中的可变参数函数
- javascript - Angular variable assign value if/else inside subscribe getting undefined
- android - ConstrainLayout 中的自定义微调器未正确对齐
- c# - Umbraco 7 - 通过代码更新字典项的值
- javascript - 无法读取未定义的属性如何在 DOM 中解决此问题?