I can check if user granted notification (alert) permission or not before iOS8 like that:
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
{
//user granted
}
it is not working on iOS8, it says:
iOS (3.0 and later) Deprecated:Register for user notification settings using the registerUserNotificationSettings: method instead.
console says:
enabledRemoteNotificationTypes is not supported in iOS 8.0 and later.
so how can I check it on iOS 8?
Ok I solved my problem like this:
BOOL isgranted = false;
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= 80000
if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
isgranted = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
}
#else
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
{
isgranted = true;
}
#endif
I expanded on woheras answer a bit to be more dynamic
- (BOOL)pushNotificationsEnabled
{
BOOL isgranted = false;
if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
if ([[UIApplication sharedApplication] respondsToSelector:@selector(isRegisteredForRemoteNotifications)])
{
isgranted = [[UIApplication sharedApplication] isRegisteredForRemoteNotifications];
}else{
}
}else{
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert)
{
isgranted = true;
}else{
}
}
return isgranted;
}
I didn't have much luck using isRegisteredForNotifications
. I ended up using currentUserNotificationSettings
instead.
+ (BOOL)notificationServicesEnabled {
BOOL isEnabled = NO;
if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){
UIUserNotificationSettings *notificationSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];
if (!notificationSettings || (notificationSettings.types == UIUserNotificationTypeNone)) {
isEnabled = NO;
} else {
isEnabled = YES;
}
} else {
UIRemoteNotificationType types = [[UIApplication sharedApplication] enabledRemoteNotificationTypes];
if (types & UIRemoteNotificationTypeAlert) {
isEnabled = YES;
} else{
isEnabled = NO;
}
}
return isEnabled;
}
Trying to shoot two birds with one stone here. First, instead of checking for isRegisteredForRemoteNotifications(), you should probably check for currentUserNotificationSettings(). With that, you can see if Alerts, Sound, etc are allowed. Secondly, it seems like the iOS version check is redundant here. Simply checking if object responds to selector is good enough. Lastly, the code example is in Swift, for that one user who asked for it :P
func hasPushNotificationsEnabled() -> Bool {
if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") {
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
return (settings.types & UIUserNotificationType.Alert) != UIUserNotificationType.None
}
let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
return (types & UIRemoteNotificationType.Alert) != UIRemoteNotificationType.None
}
Here is how to do Daniels answer in Swift 2.0
func hasPushEnabled() -> Bool {
//ios 8+
if UIApplication.sharedApplication().respondsToSelector("currentUserNotificationSettings") == true {
let settings = UIApplication.sharedApplication().currentUserNotificationSettings()
if (settings?.types.contains(.Alert) == true){
return true
} else {
return false
}
}
else {
let types = UIApplication.sharedApplication().enabledRemoteNotificationTypes()
if types.contains(.Alert) == true {
return true
} else {
return false
}
}
}