[英]iOS 11.2.5 - didRegisterForRemoteNotificationWithDeviceToken - no response
Starting with the OS 11.2.5 my devices weren't able to register a remote notification (eg for silent push purposes. I implemented the registration process within these code lines: 从OS 11.2.5开始,我的设备无法注册远程通知(例如,出于静默推送目的。我在以下代码行中实现了注册过程:
// Ask for notification permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(accepted, error) in
if !accepted {
print("Notification access denied.")
}
}
application.registerForRemoteNotifications()
Additionally, as you already know, you need to implement the following two methods, in order to register a remote notification at Apple: 此外,您已经知道,您需要实现以下两种方法,才能在Apple上注册远程通知:
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
let token = tokenParts.joined()
// Get my token here and do additionally stuff
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
// Handling error for registering here
}
So my question would be the following: This implementation has been working until Apple OS Update 11.2.4: The didRegisterForRemoteNotificationsWithDeviceToken
was successfully called after registering a device and in case of an error the other method didFailToRegisterForRemoteNotificationsWithError
was called -> everything perfect! 所以我的问题是:该实现一直有效,直到Apple OS Update 11.2.4:注册设备后成功调用了
didRegisterForRemoteNotificationsWithDeviceToken
,并且在发生错误的情况下,另一个方法didFailToRegisterForRemoteNotificationsWithError
被调用了->一切完美!
But starting with OS 11.2.5 I got no response from Apple anymore. 但是从OS 11.2.5开始,我再也没有收到苹果的回应。 I spent a lot of time investigating this issue.
我花了很多时间调查这个问题。 After Apple released OS 11.2.6 it worked like charm again -> I'm totally confused.
苹果发布OS 11.2.6之后,它再次像魅力一样工作->我完全感到困惑。
Does anybody know, if this is a known issue in OS 11.2.5? 有人知道,如果这是OS 11.2.5中的已知问题? - Thanks Alex
-谢谢亚历克斯
I guess the problem caused at registering for remote notification, try below code please: 我猜是在注册远程通知时引起的问题,请尝试以下代码:
// Ask for notification permission
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {(accepted, error) in
if accepted {
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}else{
print("Notification access denied.")
}
}
use updated methods.
// Push Notifications
func registerForPushnotifications(application: UIApplication)
{
if #available(iOS 10.0, *) {
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { (granted, error) in
guard granted else{ return }
self.getNotificationSetting()
}
}
else
{
// Fallback on earlier versions
let notificationSettings = UIUserNotificationSettings(
types: [.badge, .sound, .alert], categories: nil)
application.registerUserNotificationSettings(notificationSettings)
}
}
// Push Notification settings
func getNotificationSetting()
{
if #available(iOS 10.0, *)
{
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
guard settings.authorizationStatus == .authorized else {return}
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
}
}
}
// Push Notifications Delegates
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data)
{
let tokenParts = deviceToken.map { data -> String in
return String(format: "%02.2hhx", data)
}
}
func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error)
{
print("Failed to register for remote Notifications due to: \(error.localizedDescription)")
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.