32

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.

Jacob Richman
  • 471
  • 1
  • 5
  • 8

8 Answers8

76

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

Ashvin
  • 8,227
  • 3
  • 36
  • 53
Bhumit Mehta
  • 16,278
  • 11
  • 50
  • 64
  • 6
    this works great. If the user previously denied permissions the type == none but asking it again wont bring up the request window. How can I know if they previously denied it? Should I keep a flag in user preferences if I asked already? – mihai Feb 27 '15 at 19:06
  • 1
    Note, that it is available on iOS 8+ only. – FreeNickname Mar 04 '15 at 18:05
  • 1
    Note, this only determines if it is enabled, not explicitly disabled. – Jason McCreary Jul 22 '15 at 14:38
  • @jasonmccreary How do you mean exactly? How can it be enabled if its explicitly disabled? I thinking Im having this issue. I used the above code in this thread, but I do not get the wanted effect at all. I have made a thread: http://stackoverflow.com/questions/33766846/uiusernotificationsettings-not-working-right – DevilInDisguise Nov 17 '15 at 21:09
  • 1
    Shouldn't the second `if` be something like if (`grantedSettings.types & (UIUserNotificationTypeSound | UIUserNotificationTypeAlert))`since the bitwise comparation between 7* (111) & 2 (010) & 4 (100) will result in 0. 7 is all permissions – Benjamin Jimenez Mar 08 '16 at 16:50
  • 1
    further to what Benjamin said - `(grantedSettings.types & UIUserNotificationTypeSound & UIUserNotificationTypeAlert)` will NEVER be true – yet another programmer Apr 05 '16 at 15:18
  • I know the question asks for Objective-C, but adding a Swift version of your answer could also be helpful :) – George Marmaridis Sep 07 '16 at 10:35
23

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).

Elijah
  • 8,381
  • 2
  • 55
  • 49
simeon
  • 4,466
  • 2
  • 38
  • 42
9

Swift with guard:

guard let settings = UIApplication.sharedApplication().currentUserNotificationSettings() where settings.types != .None else {
    return
}
zgjie
  • 2,099
  • 2
  • 21
  • 32
9

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.

dustinrwh
  • 888
  • 1
  • 14
  • 16
7

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
} 
Community
  • 1
  • 1
Albert Vila Calvo
  • 15,298
  • 6
  • 62
  • 73
  • 3
    Don't use the `rawValue`s, I don't think there's any guarantee they'd never change. As @simeon says, use `settings.types.contains(.Alert)` – Chris Wagner Mar 29 '16 at 19:06
  • No idea @ChrisWagner. I took this from a book from Matt Neuburg. I don't do iOS (I was porting an Android app 1 year ago). I've edited the answer to point to simeon's one. Thanks for the downvote anyway :) – Albert Vila Calvo Mar 30 '16 at 10:28
4

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
        }
    }
Andrea Gorrieri
  • 1,704
  • 2
  • 22
  • 36
3

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");
    }
}
AdrienBr
  • 31
  • 2
0

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;
        }
    }];
Ofir Malachi
  • 1,145
  • 14
  • 20