In my application I have in app settings for enabling/disabling push notifications. I ran into a case in which if user Don't allow notifications when the application launches and then enables it with Settings in the Application then authorization status is always Denied.
Here is my code for push notification in app delegate.
I have called registerForPushNotifications method in didFinishLaunching.
// MARK: Register app for push notificaitons
func registerForPushNotifications() {
UNUserNotificationCenter.current().delegate = self
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) {
[weak self] granted, error in
printLogs("Permission granted: \(granted)")
// guard granted else { return }
if error == nil {
self?.getNotificationSettings()
}
}
}
func getNotificationSettings() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
printLogs("Notification settings: \(settings)")
switch settings.authorizationStatus {
case .authorized:
DispatchQueue.main.async {
UIApplication.shared.registerForRemoteNotifications()
}
case .denied:
printLogs("Notifications Denied")
case .notDetermined:
printLogs("Notifications not determined")
default:
break
}
}
}
and for enabling notifications from settings I am using this code
AppDelegate.shared.registerForRemoteNotifications()