Swift ios check if remote push notifications are enabled in ios9 and ios10
Updated answer after iOS 10 is using UNUserNotificationCenter
.
First you need to import UserNotifications
then
let current = UNUserNotificationCenter.current()
current.getNotificationSettings(completionHandler: { permission in
switch permission.authorizationStatus {
case .authorized:
print("User granted permission for notification")
case .denied:
print("User denied notification permission")
case .notDetermined:
print("Notification permission haven't been asked yet")
case .provisional:
// @available(iOS 12.0, *)
print("The application is authorized to post non-interruptive user notifications.")
case .ephemeral:
// @available(iOS 14.0, *)
print("The application is temporarily authorized to post notifications. Only available to app clips.")
@unknown default:
print("Unknow Status")
}
})
this code will work till iOS 9, for iOS 10 use the above code snippet.
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
} else {
// Show alert user is not registered for notification
}
Determine on iPhone if user has enabled push notifications
Call enabledRemoteNotificationsTypes
and check the mask.
For example:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// blah blah blah
iOS8 and above:
[[UIApplication sharedApplication] isRegisteredForRemoteNotifications]
Check whether user notifications are enabled after UILocalNotification deprecation
Step 1 : import UserNotifications
Step 2 :
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
if settings.authorizationStatus == .authorized {
// Notifications are allowed
}
else {
// Either denied or notDetermined
}
}
Inspect the settings object for more informations.
How to check that notifications are enabled on iPhone
This should work:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types == UIRemoteNotificationTypeNone)
// Disabled
Related Topics
Native Facebook App Does Not Open with Facebook Login in iOS 9
How to Use Apple's New .P8 Certificate for Apns in Firebase Console
Why Push Notifications Is Not Working on Testflight
How to Convert an Int to Hex String in Swift
Swift - Download a Video from Distant Url and Save It in an Photo Album
How to Hide Status Bar in Uiimagepickercontroller
Does Swift Support Implicit Conversion
Error: Invalid Bitcode Version (Producer: '800.0.35.0_0' Reader: '703.0.31_0')
Wkwebview Content Loaded Function Never Get Called
Swift Get String Between 2 Strings in a String
How to Draw a "Speech Bubble" on an Iphone
Programmatically Add Centerx/Centery Constraints
How to Prompt the User to Turn on Location Services After User Has Denied Their Use
How to Set a Uitableview to Grouped Style
How to Disable Control Center in iOS 7 Programmatically and If Not, What Are Alternatives
Error: Ld: Library Not Found for -Lpods with Cocoapods
What Does the Text Inside Parentheses in @Interface and @Implementation Directives Mean