Ios 如何侦听通知授权状态的更改

Ios 如何侦听通知授权状态的更改,ios,swift,Ios,Swift,在我的应用程序中,无论是否接收通知,都有一个控制器设置。当用户尝试打开接收通知,但在系统设置中关闭了通知时,会弹出一个对话框,将用户重定向到系统设置以首先打开该通知。我想知道用户是否在重定向后打开/关闭通知设置,然后我可以执行一些附加任务。您可以使用以下代码检查应用程序的通知设置是否打开/关闭 func setPushPermission(){ if #available(iOS 10.0, *) { let center = UNUserNotifica

在我的应用程序中,无论是否接收通知,都有一个控制器设置。当用户尝试打开接收通知,但在系统设置中关闭了通知时,会弹出一个对话框,将用户重定向到系统设置以首先打开该通知。我想知道用户是否在重定向后打开/关闭通知设置,然后我可以执行一些附加任务。

您可以使用以下代码检查应用程序的通知设置是否打开/关闭

func setPushPermission(){
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.getNotificationSettings { (settings) in
                if(settings.authorizationStatus == .authorized) {
                    self.pushPermission = .Allowed
                } else if(settings.authorizationStatus == .denied) {
                    self.pushPermission = .Disallowed
                } else {
                    self.pushPermission = .UnDefined
                }
            }
        }else{
            let notificationSettings = UIApplication.shared.currentUserNotificationSettings
            let status = notificationSettings?.types.contains(.alert)
            if status == true {
                self.pushPermission = .Allowed
            }
        }
    }
    func registerForUserNotification(){
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in

                if willAllow == true
                {
                    self.pushPermission = .Allowed
                    //[[UIApplication sharedApplication] registerForRemoteNotifications];
                    //                  UIApplication.shared.registerForRemoteNotifications()

                }else{
                    self.pushPermission = .Disallowed
                }
                NotificationCenter.default.post(name: NSNotification.Name("PushPermissionChaged"), object: nil)

            }
            UNUserNotificationCenter.current().delegate = self

        }else{
            let userNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)

            // Register User Notification Settings
            UIApplication.shared.registerUserNotificationSettings(userNotificationSettings)
            self.pushPermission = .Allowed
        }
    }

您可以使用以下代码检查应用程序的通知设置是否打开/关闭

func setPushPermission(){
        if #available(iOS 10.0, *) {
            let center = UNUserNotificationCenter.current()
            center.getNotificationSettings { (settings) in
                if(settings.authorizationStatus == .authorized) {
                    self.pushPermission = .Allowed
                } else if(settings.authorizationStatus == .denied) {
                    self.pushPermission = .Disallowed
                } else {
                    self.pushPermission = .UnDefined
                }
            }
        }else{
            let notificationSettings = UIApplication.shared.currentUserNotificationSettings
            let status = notificationSettings?.types.contains(.alert)
            if status == true {
                self.pushPermission = .Allowed
            }
        }
    }
    func registerForUserNotification(){
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [UNAuthorizationOptions.alert, UNAuthorizationOptions.sound, UNAuthorizationOptions.badge]) { (willAllow: Bool, error: Error?) in

                if willAllow == true
                {
                    self.pushPermission = .Allowed
                    //[[UIApplication sharedApplication] registerForRemoteNotifications];
                    //                  UIApplication.shared.registerForRemoteNotifications()

                }else{
                    self.pushPermission = .Disallowed
                }
                NotificationCenter.default.post(name: NSNotification.Name("PushPermissionChaged"), object: nil)

            }
            UNUserNotificationCenter.current().delegate = self

        }else{
            let userNotificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil)

            // Register User Notification Settings
            UIApplication.shared.registerUserNotificationSettings(userNotificationSettings)
            self.pushPermission = .Allowed
        }
    }

viewDidLoad
中,您可以添加观察者以收听通知设置状态。然后用最新的状态做一些事情。请参阅下面的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(checkNotificationStatus), name: UIApplication.didBecomeActiveNotification, object: nil)
}

@objc private func checkNotificationStatus() {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        switch settings.authorizationStatus {
        case .authorized, .denied, .provisional, .notDetermined:
            print("Do something according to status")
        }
    }
}

viewDidLoad
中,您可以添加观察者以收听通知设置状态。然后用最新的状态做一些事情。请参阅下面的代码:

override func viewDidLoad() {
    super.viewDidLoad()

    NotificationCenter.default.addObserver(self, selector: #selector(checkNotificationStatus), name: UIApplication.didBecomeActiveNotification, object: nil)
}

@objc private func checkNotificationStatus() {
    UNUserNotificationCenter.current().getNotificationSettings { (settings) in
        switch settings.authorizationStatus {
        case .authorized, .denied, .provisional, .notDetermined:
            print("Do something according to status")
        }
    }
}

用于侦听身份验证状态的iOS10+RxSwift解决方案

它适用于设置时的手动更改和请求访问时的接受/拒绝

extension Reactive where Base: UNUserNotificationCenter {
    var isAuthorized: Observable<Bool> {
        UIApplication.shared.rx.applicationDidBecomeActive
            .flatMap { [base] _ -> Observable<Bool> in
                Observable.create { observer in
                    base.getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in
                        guard settings.authorizationStatus == .authorized else {
                            observer.onNext(false)
                            return
                        }
                        observer.onNext(true)
                    })
                    return Disposables.create()
                }
           }
    }
}
extension-Reactive-where-Base:UNUserNotificationCenter{
var未授权:可观察{
UIApplication.shared.rx.ApplicationIDBecMeactive
.flatMap{[base]->在中可见
创建{observate in
中的base.getNotificationSettings(completionHandler:{(设置:UNNotificationSettings))设置
guard settings.authorizationStatus==.authorized else{
observer.onNext(错误)
回来
}
observer.onNext(真)
})
返回一次性物品。创建()
}
}
}
}

iOS10+RxSwift解决方案,用于侦听身份验证状态

它适用于设置时的手动更改和请求访问时的接受/拒绝

extension Reactive where Base: UNUserNotificationCenter {
    var isAuthorized: Observable<Bool> {
        UIApplication.shared.rx.applicationDidBecomeActive
            .flatMap { [base] _ -> Observable<Bool> in
                Observable.create { observer in
                    base.getNotificationSettings(completionHandler: { (settings: UNNotificationSettings) in
                        guard settings.authorizationStatus == .authorized else {
                            observer.onNext(false)
                            return
                        }
                        observer.onNext(true)
                    })
                    return Disposables.create()
                }
           }
    }
}
extension-Reactive-where-Base:UNUserNotificationCenter{
var未授权:可观察{
UIApplication.shared.rx.ApplicationIDBecMeactive
.flatMap{[base]->在中可见
创建{observate in
中的base.getNotificationSettings(completionHandler:{(设置:UNNotificationSettings))设置
guard settings.authorizationStatus==.authorized else{
observer.onNext(错误)
回来
}
observer.onNext(真)
})
返回一次性物品。创建()
}
}
}
}

你在找这个吗?@PrashantTukadiya这是我目前实施的。但是我想知道是否有人听我说系统设置改变了,这就是我告诉你的。如果有人更改了设备的设置,则他/她需要将您的应用程序置于后台,以便您可以检查NSNotification.Name.uiapplicationIDBECOMEACTIVE通知。正确地检查答案是否有提到这件事如果用户在系统设置中打开,但没有返回到应用程序如何?你为什么需要检查?你在寻找这个吗?@PrashantTukadiya这是我当前实现的。但是我想知道是否有人听我说系统设置改变了,这就是我告诉你的。如果有人更改了设备的设置,则他/她需要将您的应用程序置于后台,以便您可以检查NSNotification.Name.uiapplicationIDBECOMEACTIVE通知。正确地检查答案有提到的这件事如果用户在系统设置中打开但没有返回到应用程序如何?为什么需要检查?我喜欢这个答案,因为它还可以处理用户从设置中手动更改应用程序通知设置的情况。我喜欢这个答案,因为它还可以处理用户从设置appAm手动更改应用程序的通知设置的情况。我知道这是您的代码涉及RxAppState吗?我知道这是您的代码涉及RxAppState吗?