授予通知访问权限后执行segue

时间:2019-12-19 11:49:49

标签: swift appdelegate remote-notifications

我想知道在从对话框中授予远程通知访问权限后如何执行模态搜索。我已经在应用程序委托中设置了远程通知。

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()
            }
        }
    }

}

然后,在我的视图控制器中,我有以下代码:

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

问题是用户是允许还是拒绝对通知的访问,segue不会执行。

谢谢您的帮助。

1 个答案:

答案 0 :(得分:0)

您必须调用block参数

替换

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()
        }
    }
}

使用

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)
        }
    }

}
相关问题