Coder1224
Coder1224

Reputation: 1835

Display UILocalNotification request after user clicked "don't allow"

I want to allow my users to disable/enable local notifications from within my app. I know that when I call register for UILocalNotification, the pop-up is only shown once, no matter how many times register is called. Is there a way to reset their answer and ask it again if the user later decides to enable notifications from within my app?

Enable/Disable Notifications Toggle

 application.registerUserNotificationSettings(
    UIUserNotificationSettings( forTypes: [.Alert, .Badge, .Sound], 
                                categories: nil ) )

Can I reset their answer to registerUserNotificationSettings()? Something like

// if user clicked enable notifications
let grantedSettings = application.currentUserNotificationSettings()
// reset grantedSettings
// call registerUserNotificationSettings() again

PS: I know that UNNotificationRequest is recommended now, but I want to support iOS 9.0 and I read UNNotificationRequest is for iOS 10+.

Upvotes: 0

Views: 790

Answers (2)

Rhm Akbari
Rhm Akbari

Reputation: 360

You can send users to your app's settings page of user's device setting app, letting them to opt-in into LocalNotification

  • iOS earlier than version 10 dos not provide information if user has denied permission before or not.
  • In your app you need to save in NSUserDefaults or somewhere that you have already asked the permission.
  • Every time you require permission, first check if permission is available or not.
  • If permission is not available and your app has not asked user before ( based on the saved status in prior step ) you go for requesting permission.
  • If you have asked the permission from user before ( based on the saved status in prior step ) you prompt users if they want to allow permission, if they say 'Yes' forward them to the device setting app.

Objective-C for iOS <= 10 (Deprecated in iOS 10)

This is some code snip for inquiring UIUserNotificationSettings

UIUserNotificationSettings *currentSettings = [[UIApplication sharedApplication] currentUserNotificationSettings]
if (currentSettings.types == UIUserNotificationTypeNone)   // Permission does not exist either user denied or never been asked before

if (!(currentSettings.types & (UIUserNotificationTypeAlert | UIUserNotificationTypeSound)))  // This means your app does not have permission alert and to play sounds with notification.

This code snip shows how to send users to device setting page.

NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                if ([[UIApplication sharedApplication] canOpenURL:url]) {
                    [[UIApplication sharedApplication] openURL:url];
                }

Swift 3 for iOS <= 10 (Deprecated in iOS 10)

This is some code snip for inquiring UIUserNotificationSettings

let currentSettings: UIUserNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)
if currentSettings.types.isEmpty {
        // Here you decide whether to prompt user with new authorization request
       // or send user to setting app based on your stored variable
    }

This code snip shows how to send users to device setting page.

if let urlStr = URL(string: UIApplicationOpenSettingsURLString) {
        if UIApplication.shared.canOpenURL(urlStr) {
            UIApplication.shared.openURL(urlStr)
        }
    }

For iOS 10 and above

instead of saving a variable in your app the check whether your app ever asked for authorization or not, check the value of authorizationStatus of UNNotificationSettings class

The value of this property is notDetermined if your app has never requested authorization using the requestAuthorization(options:completionHandler:) method.

along with other UNNotificationSettings counterpart methods similar to older version of this class ( UIUserNotificationSettings ) request permission or check for the permissions available like badge, alert or sound.

Upvotes: 2

Darryl Johnson
Darryl Johnson

Reputation: 431

No, you can only ask for notification permission once and the permission is managed by the OS. If they press Don't Allow, the user will have to go into settings and change the permission manually.

The way many apps get around this is to show a custom 'soft' alert before showing the iOS dialog. That way if the user presses 'not now', you can show your custom alert at some time in the future and reserve showing the iOS dialog for when the user is ready to enable notifications (or any other permission).

TLDR; No, it's up to the user to manage their notification preferences after and including the initial dialog.

Upvotes: 1

Related Questions