Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS8 check permission of remotenotificationtype

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?

like image 786
woheras Avatar asked Dec 02 '22 19:12

woheras


2 Answers

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
like image 100
woheras Avatar answered Feb 13 '23 01:02

woheras


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;

}
like image 42
Daniel Baughman Avatar answered Feb 13 '23 01:02

Daniel Baughman