【问题标题】:How to listen to the changes in notification authorization Status如何监听通知授权状态的变化
【发布时间】:2018-07-18 05:35:08
【问题描述】:

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

【问题讨论】:

  • 您在寻找这个stackoverflow.com/a/50645392/4601900 吗?
  • @PrashantTukadiya 这是我目前实施的。但是我想知道是否有听知道系统设置改变了。
  • 我告诉过你的。如果有人更改了设备的设置,那么他/她需要将您的应用程序置于后台,以便您可以检查 NSNotification.Name.UIApplicationDidBecomeActive 通知。正确检查答案有提到这件事
  • 如果用户在系统设置中打开但不返回应用程序怎么办?
  • 为什么需要检查?

标签: ios swift


【解决方案1】:

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】:

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

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

【讨论】:

    【解决方案3】:

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

    更新:需要使用 RxAppState github.com/pixeldock/RxAppState 库。

    【讨论】:

    猜你喜欢
    • 2016-05-15
    • 2011-01-31
    • 2017-05-31
    • 2015-05-23
    • 2014-12-11
    • 2021-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多