UIApplication.shared.isRegisteredForRemoteNotifications 总是 return 真

UIApplication.shared.isRegisteredForRemoteNotifications always return true

我正在开发一个应用程序并试图弄清楚我的应用程序是否注册了推送通知。我的意思是用户是否允许推送通知。但是当我想签入代码时,它总是 return 正确。我不知道为什么会这样。

我的检查推送通知注册的代码。

let isRegistered = UIApplication.shared.isRegisteredForRemoteNotifications

if isRegistered == false {

    let alertController = UIAlertController(title: "Notification Alert", message: "Kindly enable push notifications", preferredStyle: .alert)
    let settingsAction = UIAlertAction(title: "Settings", style: .default) { (_) -> Void in
        guard let settingsUrl = URL(string: UIApplicationOpenSettingsURLString) else {
            return
        }
        if UIApplication.shared.canOpenURL(settingsUrl) {
            UIApplication.shared.open(settingsUrl, completionHandler: { (success) in
            })
        }
    }
    let cancelAction = UIAlertAction(title: "Cancel", style: .default, handler: nil)
    alertController.addAction(cancelAction)
    alertController.addAction(settingsAction)
    DispatchQueue.main.async {
        self.present(alertController, animated: true, completion: nil)

    }
}

我的理解是,如果 "isRegistered" 为假,您将要求用户允许通知,在这种情况下,您应该像这样请求授权:

let center = UNUserNotificationCenter.current()
center.requestAuthorization(options:[.badge, .alert, .sound]) { (granted, error) in
        if granted {
            DispatchQueue.main.async(execute: {
                UIApplication.shared.registerForRemoteNotifications()
            })
        }
    }