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]
quantumpotato's issue:
Where types
is given by
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
one can use
if (types & UIRemoteNotificationTypeAlert)
instead of
if (types == UIRemoteNotificationTypeNone)
will allow you to check only whether notifications are enabled (and don't worry about sounds, badges, notification center, etc.). The first line of code (types & UIRemoteNotificationTypeAlert
) will return YES
if "Alert Style" is set to "Banners" or "Alerts", and NO
if "Alert Style" is set to "None", irrespective of other settings.
In the latest version of iOS this method is now deprecated. To support both iOS 7 and iOS 8 use:
UIApplication *application = [UIApplication sharedApplication];
BOOL enabled;
// Try to use the newer isRegisteredForRemoteNotifications otherwise use the enabledRemoteNotificationTypes.
if ([application respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
enabled = [application isRegisteredForRemoteNotifications];
}
else
{
UIRemoteNotificationType types = [application enabledRemoteNotificationTypes];
enabled = types & UIRemoteNotificationTypeAlert;
}
Updated code for swift4.0 , iOS11
import UserNotifications
UNUserNotificationCenter.current().getNotificationSettings { (settings) in
print("Notification settings: \(settings)")
guard settings.authorizationStatus == .authorized else { return }
//Not authorised
UIApplication.shared.registerForRemoteNotifications()
}
Code for swift3.0 , iOS10
let isRegisteredForRemoteNotifications = UIApplication.shared.isRegisteredForRemoteNotifications
if isRegisteredForRemoteNotifications {
// User is registered for notification
} else {
// Show alert user is not registered for notification
}
From iOS9 , swift 2.0 UIRemoteNotificationType is deprecated, use following code
let notificationType = UIApplication.shared.currentUserNotificationSettings!.types
if notificationType == UIUserNotificationType.none {
// Push notifications are disabled in setting by user.
}else{
// Push notifications are enabled in setting by user.
}
simply check whether Push notifications are enabled
if notificationType == UIUserNotificationType.badge {
// the application may badge its icon upon a notification being received
}
if notificationType == UIUserNotificationType.sound {
// the application may play a sound upon a notification being received
}
if notificationType == UIUserNotificationType.alert {
// the application may display an alert upon a notification being received
}