Prashant Tukadiya
Prashant Tukadiya

Reputation: 16446

Check Push if notification permission asked or not

I have already seen Determine on iPhone if user has enabled push notifications

But My Question is some different. I need to show a "navigate to settings" screen if push notification permission is denied by the user. How can we check if push permission has been asked before or not? if #available(iOS 10.0, *) { is working fine and it is able to get all the status, but for earlier versions I am not able to figure it out.

I have following code:

    NotificationCenter.default.addObserver(forName: NSNotification.Name.UIApplicationDidBecomeActive, object: nil, queue: .main) {[weak self] (notificaiont) in
        guard let strongSelf = self  else {return }

        if #available(iOS 10.0, *) {
            let current = UNUserNotificationCenter.current()
            current.getNotificationSettings(completionHandler: {settings in

                switch settings.authorizationStatus {
                case .notDetermined:
                    AppDelegate.sharedDelegate.registerForPush()
                case .denied:
                    AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")
                case .authorized:
                    strongSelf.dismiss(animated: true, completion: nil)
                    break
                }
            })
        } else {
            // Fallback on earlier versions
            if UIApplication.shared.isRegisteredForRemoteNotifications {
                strongSelf.dismiss(animated: true, completion: nil)
            } else {
                //what should i call here  registerForPush for navigate to setting ?
               // AppDelegate.sharedDelegate.registerForPush()
                AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")

            }
        }
    }

In // Fallback on earlier versions 's else part I am confused, do I need to call

AppDelegate.sharedDelegate.registerForPush()

or

AppDelegate.sharedDelegate.navigateUesrToSettings(withMessage: "Allow app to send notifications")

? Thanks!

Upvotes: 2

Views: 1147

Answers (1)

dr_barto
dr_barto

Reputation: 6075

You have to store this yourself, e.g. in user defaults. When you prompt the user for the permission, set the flag to true; something like this:

if UIApplication.shared.isRegisteredForRemoteNotifications {
  // equivalent to .authorized case
}
else if !UserDefaults.standard.bool(forKey: "didAskForPushPermission") 
{
  // equivalent to .notDetermined case
  UserDefaults.standard.set(true, forKey: "didAskForPushPermission")
}
else {
  // equivalent to .denied case
}

Upvotes: 2

Related Questions