Consider the following scenario:
App Installed
App Starts
Push Notification ➝ Request
Push Notification ➝ Don't Allow
App Closed or in Background
Settings ➝ User enabled Push Notification
How to get notified of this change without moving the app to foreground, so that the device token could be send to our backend server for receiving notifications even if the user do not open the app ?
I know that you can check whether notifications are allowed with UNUserNotifications
framework like this:
static func checkNotificationStatus() {
UNUserNotificationCenter.current().getNotificationSettings { settings in
switch settings.authorizationStatus {
case .authorized: generatePushToken()
case .provisional: generatePushToken()
case .denied: break
case .notDetermined:
UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .sound, .badge]) { granted, error in
checkNotificationStatus()
}
@unknown default: break
}
}
}
But that happens only after your app moves to foreground or it is launched again.