in iOS11, Swift 4 ...
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Already authorized
}
else {
// Either denied or notDetermined
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
(granted, error) in
// add your own
UNUserNotificationCenter.current().delegate = self
let alertController = UIAlertController(title: "Notification Alert", message: "please enable notifications", preferredStyle: .alert)
let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
return
}
if UIApplication.shared.canOpenURL(settingsUrl) {
UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
})
}
}
let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
alertController.addAction(cancelAction)
alertController.addAction(settingsAction)
DispatchQueue.main.async {
self.window?.rootViewController?.present(alertController, animated: true, completion: nil)
}
}
}
}
Dies scheint nicht für iOS 10 zu funktionieren. Im Simulator habe ich auf "Do not Allow" geklickt und dieser Code sagte immer noch, dass Benutzer für Remote-Benachrichtigungen registriert ist. – tylerSF
Funktioniert für iOS 10. Versuchen Sie es mit einem tatsächlichen Gerät anstelle des Simulators. –
Es zeigt Ihnen nur, wenn das Token jemals generiert wurde (das Gerät wurde registriert), nicht, wenn die Benachrichtigungen blockiert wurden. – KlimczakM