iOS8 check permission of remotenotificationtype
Asked Answered
O

5

4

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?

Overrule answered 29/8, 2014 at 14:8 Comment(0)
O
9

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
Overrule answered 3/9, 2014 at 11:5 Comment(3)
Hi, your solution for IOS8 doesn't completely tell if you the "allow notifications" is on or off. "isRegisteredForRemoteNotifications" will return false if the allow notification setting is off, but will also return false if sound notification is disabled and alert types is set to "none". Is there any way to concretely determine if the allow notifications is enabled or not, all by itself?Hypocrite
@MichalShatz I could not find a way, but this suggestion is bad because it doesn't operate on both ios7 and ios 8, see the extra answer i've added to this questionHypocrite
@DanielBaughman types & UIRemoteNotificationTypeAlert demonstrates the same behaviourChapen
H
4

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;

}
Hypocrite answered 2/10, 2014 at 15:56 Comment(1)
How do you convert this to swift, been trying to figure it out for the last half an hour :(Bimetallic
A
4

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;
}
Aton answered 5/3, 2015 at 10:53 Comment(0)
F
0

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
}
Firooc answered 18/2, 2015 at 3:3 Comment(0)
C
0

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
        }
    }
}
Corncob answered 15/2, 2016 at 19:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.