Search code examples
iosnotificationsalertuilocalnotification

How to handle user choice of notification settings permission alert


At some view controller I pop up this Notification setting message:

My app would like to to send you Notifications which may include alerts, sounds and icon badges. These can be configured in Settings

How do I handle the message's Don't Allow and Allow buttons?

If the user taps on Allow, I need change the label text in the same view controller.


Solution

  • I assume that somewhere in your app you have code like that:

    UIUserNotificationSettings *settings = [UIUserNotificationSettings settingsForTypes:... categories:...];
    [application registerUserNotificationSettings:settings];
    

    After doing that, system will show alert about which you are talking. In that moment, app will receive UIApplicationWillResignActiveNotification (you can register for that in app delegate or by NSNotificationCenter). Then, after user will make some choice, system will send UIApplicationDidBecomeActiveNotification (also in app delegate or by notification). At that point, check for permissions using code like that:

    UIUserNotificationSettings *settings = application.currentUserNotificationSettings;
    if (settings.types & UIUserNotificationTypeSound & UIUserNotificationTypeBadge) {
        // sound and icon badge allowed
    }
    else {
        // either sound or icon badge or both disallowed
    }