【发布时间】:2020-06-18 10:03:30
【问题描述】:
我尝试了很多时间,但没有调用didRegisterForRemoteNotificationsWithDeviceToken 方法。我花了很多时间。如果有人有解决方案,请告诉我。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
}
以下函数用于didFinishLaunchingWithOptions 中的AppDelegate 中的注册通知。
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)
}
}
【问题讨论】:
-
并非一切都以成功告终。 Handle fail too 并检查那里发生了什么。
-
@user28434 我已经检查了这个
didFailToRegisterForRemoteNotificationsWithError没有被调用并且没有在那里抛出任何错误。 -
尝试重启你的设备,我也遇到这种情况。这在 Xcode 部署和 Adhoc 中也很常见,有时您需要等待长达 1 小时才能再次正常工作。
标签: ios swift xcode push-notification devicetoken