iOS8 check permission of remotenotificationtype

走远了吗. 提交于 2019-12-05 01:09:35

问题


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?


回答1:


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



回答2:


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:


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;
}



回答4:


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
}



回答5:


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
        }
    }
}


来源:https://stackoverflow.com/questions/25570015/ios8-check-permission-of-remotenotificationtype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!