swiftappdelegateremote-notifications

Perform segue after notification access has been granted


I would like to know how to perform a modal segue after the remote notification access has been granted from the dialog box. I have set up my remote notification in the app delegate.

func registerANSForApplication(_ application: UIApplication,withBlock block: @escaping (_ granted:Bool) -> (Void)){
    InstanceID.instanceID().instanceID { (result, error) in
        if let error = error {
            print("Error fetching remote instange ID: \(error)")
        } else if let result = result {
            print("Remote instance ID token: \(result.token)")
            AppDelegate.isToken = result.token
        }
    }
    let current  = UNUserNotificationCenter.current()
    let options : UNAuthorizationOptions = [.sound, .badge, .alert]

    current.requestAuthorization(options: options) { (granted, error) in
        guard granted else{
            return
        }
        if error != nil{
            print(error?.localizedDescription ?? "")
        }else{
            Messaging.messaging().delegate = self
            current.delegate = self
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
        }
    }

}

Then, in my view controller, I have this code:

let appDelegate = UIApplication.shared.delegate as! 
appDelegate.registerANSForApplication(UIApplication.shared) { (granted) -> (Void) in
    self.performSegue(withIdentifier: "MembershipVC", sender: nil)
}

The problem is whether the user allows or denies the access to notification, the segue is not executed.

Thank you for your help.


Solution

  • You have to call the block parameter

    Replace

    current.requestAuthorization(options: options) { (granted, error) in
        guard granted else{
            return
        }
        if error != nil{
            print(error?.localizedDescription ?? "")
        }else{
            Messaging.messaging().delegate = self
            current.delegate = self
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
            }
        }
    }
    

    with

    current.requestAuthorization(options: options) { (granted, error) in
        if error != nil {
            print(error?.localizedDescription ?? "")
            block(false)
        } else {
            Messaging.messaging().delegate = self
            current.delegate = self
            DispatchQueue.main.async {
                application.registerForRemoteNotifications()
                block(granted)
            }
        }
    
    }