Check if Local Notifications are enabled in IOS 8

心不动则不痛 提交于 2020-01-09 04:18:08

问题


I've looked all over the internet for how to create local notifications with IOS 8. I found many articles, but none explained how to determine if the user has set "alerts" on or off. Could someone please help me!!! I would prefer to use Objective C over Swift.


回答1:


You can check it by using UIApplication 's currentUserNotificationSettings

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]){ // Check it's iOS 8 and above
    UIUserNotificationSettings *grantedSettings = [[UIApplication sharedApplication] currentUserNotificationSettings];

    if (grantedSettings.types == UIUserNotificationTypeNone) {
        NSLog(@"No permiossion granted");
    }
    else if (grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert ){
        NSLog(@"Sound and alert permissions ");
    }
    else if (grantedSettings.types  & UIUserNotificationTypeAlert){
        NSLog(@"Alert Permission Granted");
    }
}

Hope this helps , Let me know if you need more info




回答2:


To expand on Albert's answer, you are not required to use rawValue in Swift. Because UIUserNotificationType conforms to OptionSetType it is possible to do the following:

if let settings = UIApplication.shared.currentUserNotificationSettings {
    if settings.types.contains([.alert, .sound]) {
        //Have alert and sound permissions
    } else if settings.types.contains(.alert) {
        //Have alert permission
    }
}

You use the bracket [] syntax to combine option types (similar to the bitwise-or | operator for combining option flags in other languages).




回答3:


Swift with guard:

guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() where settings.types != .None else {
    return
}



回答4:


Here is a simple function in Swift 3 that checks whether at least one type of notification is enabled.

Enjoy!

static func areNotificationsEnabled() -> Bool {
    guard let settings = UIApplication.shared.currentUserNotificationSettings else {
        return false
    }

    return settings.types.intersection([.alert, .badge, .sound]).isEmpty != true
}

Thanks Michał Kałużny for the inspiration.




回答5:


Edit: Take a look at @simeon's answer.

In Swift, you need to use rawValue:

let grantedSettings = UIApplication.sharedApplication().currentUserNotificationSettings()
if grantedSettings.types.rawValue & UIUserNotificationType.Alert.rawValue != 0 {
    // Alert permission granted
} 



回答6:


I think this code is more precise :

if ([[UIApplication sharedApplication] respondsToSelector:@selector(currentUserNotificationSettings)]) {

    UIUserNotificationType types = [[[UIApplication sharedApplication] currentUserNotificationSettings] types];

    if (types & UIUserNotificationTypeBadge) {
        NSLog(@"Badge permission");
    }
    if (types & UIUserNotificationTypeSound){
        NSLog(@"Sound permission");
    }
    if (types & UIUserNotificationTypeAlert){
        NSLog(@"Alert permission");
    }
}



回答7:


Using the @simeon answer Xcode tells me that

'currentUserNotificationSettings' was deprecated in iOS 10.0: Use UserNotifications Framework's -[UNUserNotificationCenter getNotificationSettingsWithCompletionHandler:] and -[UNUserNotificationCenter getNotificationCategoriesWithCompletionHandler:]

so here is the solution using the UNUserNotificationCenter for Swift 4:

UNUserNotificationCenter.current().getNotificationSettings(){ (settings) in

        switch settings.alertSetting{
        case .enabled:

            //Permissions are granted

        case .disabled:

            //Permissions are not granted

        case .notSupported:

            //The application does not support this notification type
        }
    }



回答8:


Objective C + iOS 10

  UNUserNotificationCenter *center = [UNUserNotificationCenter currentNotificationCenter];
  [center getNotificationSettingsWithCompletionHandler:^(UNNotificationSettings * _Nonnull settings) {

        switch (settings.authorizationStatus) {
            case UNAuthorizationStatusNotDetermined:

                break;

            case UNAuthorizationStatusDenied:

                break;

            case UNAuthorizationStatusAuthorized:

                break;

            default:
                break;
        }
    }];


来源:https://stackoverflow.com/questions/26051950/check-if-local-notifications-are-enabled-in-ios-8

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