iOS8 check permission of remotenotificationtype

2019-03-31 02:23发布

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?

5条回答
来,给爷笑一个
2楼-- · 2019-03-31 03:04

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;

}
查看更多
时光不老,我们不散
3楼-- · 2019-03-31 03:05

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
}
查看更多
冷血范
4楼-- · 2019-03-31 03:09

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
        }
    }
}
查看更多
你好瞎i
5楼-- · 2019-03-31 03:09

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
查看更多
Fickle 薄情
6楼-- · 2019-03-31 03:21

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;
}
查看更多
登录 后发表回答