I have a question about remote push notifications and how other developers handle a specific scenario. (I use Fireabase Messaging for remote push notificaiton just FYI.)
I need to handle a situation where a user initially denies push notification authorization using the requestAuthorization(options:completionHandler:) method. The user then goes to the Settings app, turns on notifications for my app using the “Allow Notifications” toggle, and returns to my app.
If the user kills and relaunches the app, they can receive remote push notifications since all code in the app delegate runs again when relaunched. However, if the user doesn’t relaunch my app after changing the Settings, they won’t receive remote notifications in my app.
Specifically, here is the flow:
- User installs my app
- User goes to the notifications tab in my app and is asked for push
notification permission - User taps “Don’t Allow”
- User goes to the Settings app and turns on the “Allow Notifications”
toggle for my app - User returns to my app (if they have relaunched my app, they can receive notificaiton)
Ideally, I would like the user to be able to receive remote notifications after changing the Settings, without having to kill and relaunch the app.
Is it possible to make push notifications work in this scenario without relaunching the app? For example, could calling UIApplication.shared.registerForRemoteNotifications() when the app enters the foreground handle this case?
Any insights on how to best handle this user flow would be appreciated. Thank you!
final class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
FirebaseApp.configure()
Messaging.messaging().delegate = self
UNUserNotificationCenter.current().getNotificationSettings { settings in
guard settings.authorizationStatus == .authorized else { return }
DispatchQueue.main.async {
application.registerForRemoteNotifications()
}
}
return true
}
func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
// save device token
}
func application(_ application: UIApplication, didFailToContinueUserActivityWithType userActivityType: String, error: Error) {
print("didFailToContinueUserActivityWithType: \(error)")
}
}
// MARK: - MessagingDelegate
extension AppDelegate: MessagingDelegate {
func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
// save fcm token
}
}




