我尝试了很多时间,但没有调用DidRegisterForRemoteNotificationsWithDeviceToken
方法。 我花了很多时间。 如果有人有解决办法,请让我知道我。
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data){
let deviceTokenString = deviceToken.reduce("", {$0 + String(format: "%02X", $1)})
}
以下函数用于AppDelege
中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)
}
}
将以下代码应用于通知权限
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: "")
}