3

I am trying to implement a settings screen in an iOS 11 app, where I need a UISwitch that would be used to control user notifications. When set on off, I want to give up the permissions to notifications, when set to on, I want to request for permissions (standard dialog asking for user permission to send her notifications).

To ask for permission, I found following code:

UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound]) { (granted, error) in
    // Enable or disable features based on authorization.
}

However, if I turn off notifications for the app in system settings, this code does not pop up a dialog with a request, and it just simply returns false in granted.

I was not able to find anything on how to give up permissions.

Any hint on how to solve the problem? Is it even possible, or does Apple assume that this task should be left only to system settings?

Milan Nosáľ
  • 19,169
  • 4
  • 55
  • 90
  • 1
    system settings only. For the simple reason that the user will not get bombarded with "push notification requests" on each app launch. As soon as you get permission to send notification you have to decide for yourself wether or not you want to send the user an actual notification, maybe add topics the user can opt-in/-out. – luk2302 Jul 28 '17 at 15:15
  • so there is no way to programatically give up permissions? – Milan Nosáľ Jul 28 '17 at 15:25
  • why would you want to do that? I am not aware of a way, that is no use case that makes sense – luk2302 Jul 28 '17 at 15:26
  • if you are able to ask for permissions from the code, why not give them up as well? basically turn them all off – Milan Nosáľ Jul 28 '17 at 15:31
  • 1
    Because that decision has to be the user's and the user's only. You shouldn't decide for the user if they want to continue getting notifications or not. Moreover, it would make no sense to have such setting, since you as a programmer can stop sending notifications any time, which from the app's point of view is the same as turning them off. Whether the notifications switch is inside the app or in the system makes no difference for this particular question. – Dávid Pásztor Jul 28 '17 at 15:33
  • 1
    Because you are asking the user to give you the permission, asking him to revoke the permission does not make any sense. Your application can decide not to send him no more notifications but that is completely in your hand, the user should not be affected by this decision. – luk2302 Jul 28 '17 at 15:33
  • thanks for the comments guys – Milan Nosáľ Jul 28 '17 at 15:34
  • You can create one api which is post true or false according to switch and if there are false then not send notification from backend to particular token user. – Nikhlesh Bagdiya Jul 29 '17 at 08:46

2 Answers2

6

In iOS Turn on/off permission push notification appears only one time. So in order to achieve that you need to do some tweak like you can check first whether your notification is enabled or not.

func pushEnabledAtOSLevel() -> Bool {
 guard let currentSettings = UIApplication.shared.currentUserNotificationSettings?.types else { return false }
 return currentSettings.rawValue != 0
}

After that you can create your customize popup with TurnON/Off button and navigate to System setting page where user can enable that option accordingly

if let appSettings = NSURL(string: UIApplicationOpenSettingsURLString) {
    UIApplication.shared.openURL(appSettings as URL)
}
Hussain Shabbir
  • 14,801
  • 5
  • 40
  • 56
2

For iOS 10.0 and above

  UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        if settings.authorizationStatus == .authorized {
            // Notifications are allowed
        }
        else {
            // Either denied or notDetermined

            let alertController = UIAlertController(title: nil, message: "Do you want to change notifications settings?", preferredStyle: .alert)

            let action1 = UIAlertAction(title: "Settings", style: .default) { (action:UIAlertAction) in
                if let appSettings = NSURL(string: UIApplication.openSettingsURLString) {
                    UIApplication.shared.open(appSettings as URL, options: [:], completionHandler: nil)
                }
            }

            let action2 = UIAlertAction(title: "Cancel", style: .cancel) { (action:UIAlertAction) in
            }

            alertController.addAction(action1)
            alertController.addAction(action2)
            self.present(alertController, animated: true, completion: nil)
        }
    }
Kanika Sharma
  • 631
  • 5
  • 11