[英]iOS 13.x didRegisterForRemoteNotificationsWithDeviceToken is never called
Haven't done iOS dev for long time (iOS 8) so had to read up and started on a new app.很久没有做过 iOS 开发(iOS 8)所以不得不阅读并开始一个新的应用程序。 Problem is that I don't get a device token callback.问题是我没有收到设备令牌回调。 Certificates and profiles are setup correctly证书和配置文件设置正确
import UIKit
import CoreLocation
import UserNotifications
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,CLLocationManagerDelegate, UNUserNotificationCenterDelegate {
let locationManager = CLLocationManager()
var window: UIWindow?
static var sharedDelegate: AppDelegate {
return UIApplication.shared.delegate as! AppDelegate
}
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
print("Permission granted: \(granted)")
guard granted else { return }
DispatchQueue.main.async{
application.registerForRemoteNotifications()
}
}
return true
}
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 for remote notifications with error: \(error)")
}
}
Problem is that didRegisterForRemoteNotificationsWithDeviceToken or didFailToRegisterForRemoteNotificationsWithError is not called whatever I try.问题是 didRegisterForRemoteNotificationsWithDeviceToken 或 didFailToRegisterForRemoteNotificationsWithError 无论我尝试什么都不会被调用。 note: entitlement is correct, Push Notification ability added, even background mode Remote notification is added.注意:权利是正确的,添加了推送通知功能,甚至添加了后台模式远程通知。
Can someone provide a working app example that prompts for permission and register and prints the deviceToken有人可以提供一个提示许可并注册并打印 deviceToken 的工作应用程序示例吗
I'm doing it this way.我正在这样做。 (Sorry, had to post it here as an answer instead of as a comment because of the length restriction and for better formatting) (对不起,由于长度限制和更好的格式,不得不在这里发布它作为答案而不是评论)
let center = UNUserNotificationCenter.current()
center.delegate = self
// check for user permission first
center.requestAuthorization(options: [.alert, .sound, .badge]) { (granted, _) in
NSLog("PUSH NOTIFICATION PERMISSION GRANTED: \(granted)")
guard granted else { return }
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
Some more notes:还有一些注意事项:
Maybe a deciding difference could be that I'm using a let
constant for the current()
object and storing it rather than assigning the delegate to the current()
?也许决定性的区别可能是我对current()
object 使用let
常量并将其存储而不是将委托分配给current()
? I think I remember struggling like you do because of that... By the way, I'm calling this function after enabling a Switch for receiving Notifications, so double-check your function for registering is even called!我想我记得因为那个而像你一样挣扎......顺便说一句,我在启用接收通知的开关后调用这个 function,所以仔细检查你的 function 甚至调用! Maybe another hind could be to call it on UIApplication.shared
instead of the application
parameter in AppDelegate
, but I think that shouldn't make a difference...也许另一个原因是在UIApplication.shared
而不是AppDelegate
中的application
参数上调用它,但我认为这不应该有所作为......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.