我尝试了很多时间,但没有调用didRegisterForRemoteNotificationsWithDeviceToken方法。我花了很多时间。如果有人有解决方案,请告诉我。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
}下面的函数用于在AppDelegate的didFinishLaunchingWithOptions中注册通知。
func registerNotification(application:UIApplication) -> Void {
if #available(iOS 10.0, *){
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.badge, .sound, .alert], completionHandler: {(granted, error) in
if (granted)
{
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
else{
print("Noti Not registered !!")
//Do stuff if unsuccessful...
}
})
}
else { //If user is not on iOS 10 use the old methods we've been using
let notificationSettings = UIUserNotificationSettings(
types: [.badge, .sound, .alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
}发布于 2020-06-18 18:32:06
应用以下代码获取通知权限
中启用后台模式

AppDelegate中的
self.registerForPushNotifications(application: application) func registerForPushNotifications(application: UIApplication) {
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
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()
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let token = deviceToken.description().trimmingCharacters(in: CharacterSet(charactersIn: "<>"))
strDeviceToken = token.replacingOccurrences(of: " ", with: "")
}https://stackoverflow.com/questions/62447463
复制相似问题