5

In my application, there is a setting to controller if receive notification or not. When user try to turn on receive notification but notification is turn off in system setting, it pop up a dialog to redirect user to system setting to turn on it firstly. I want to know whether user is turn on/ off notification setting after redirect and then I can do some additional task.

lypw2009
  • 201
  • 3
  • 11

3 Answers3

10

At viewDidLoad you can add observer to listen to notification settings status. Then do something with the latest status. Refer code below:

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")
        }
    }
}
muazhud
  • 914
  • 7
  • 18
  • I like this answer, because it also handles situation when user manually changes app's notification settings from Settings app – zzmasoud Apr 21 '20 at 09:53
1

You can use following code for check whether app's notification setting is On/off.

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
        }
    }
girish_pro
  • 838
  • 9
  • 18
0

iOS10+ RxSwift solution to listen auth status.

It works for manual changes at settings and accept/decline when access was asked

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

upd: need to use RxAppState github.com/pixeldock/RxAppState library.

headstream
  • 206
  • 2
  • 6
  • 1
    Am I right that this your code involves RxAppState https://github.com/pixeldock/RxAppState? – granan May 19 '21 at 09:06