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.
Asked
Active
Viewed 9,297 times
5
-
Are you looking for this https://stackoverflow.com/a/50645392/4601900 ? – Prashant Tukadiya Jul 18 '18 at 05:36
-
@PrashantTukadiya This is current what I implemented. But I want to know if there is listen to know the system setting is changed. – lypw2009 Jul 18 '18 at 08:41
-
That what I told you. If someone change the setting of device then he/she need to put your app in background so you can check NSNotification.Name.UIApplicationDidBecomeActive notification for that . Check the answer properly there is this thing mentioned – Prashant Tukadiya Jul 18 '18 at 09:09
-
How about if user turn on in the system setting but not back to the app? – lypw2009 Jul 19 '18 at 04:06
-
Why would you need to check that ? – Prashant Tukadiya Jul 19 '18 at 04:50
3 Answers
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
-
1Am I right that this your code involves RxAppState https://github.com/pixeldock/RxAppState? – granan May 19 '21 at 09:06