如何收听通知授权状态中的更改

时间:2018-07-18 05:35:09

标签: ios swift

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

3 个答案:

答案 0 :(得分:1)

您可以使用以下代码检查应用的通知设置是否为开/关。

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

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