14.4 C
London
Saturday, November 18, 2023

ios – Dealing with push notifications when consumer permits them in Settings after initially denying entry


I’ve a query about distant push notifications and the way different builders deal with a selected situation. (I exploit Fireabase Messaging for distant push notificaiton simply FYI.)

I must deal with a scenario the place a consumer initially denies push notification authorization utilizing the requestAuthorization(choices:completionHandler:) technique. The consumer then goes to the Settings app, activates notifications for my app utilizing the “Enable Notifications” toggle, and returns to my app.

If the consumer kills and relaunches the app, they’ll obtain distant push notifications since all code within the app delegate runs once more when relaunched. Nonetheless, if the consumer does not relaunch my app after altering the Settings, they will not obtain distant notifications in my app.

Particularly, right here is the circulate:

  1. Person installs my app
  2. Person goes to the notifications tab in my app and is requested for push
    notification permission
  3. Person faucets “Do not Enable”
  4. Person goes to the Settings app and activates the “Enable Notifications”
    toggle for my app
  5. Person returns to my app (if they’ve relaunched my app, they’ll obtain notificaiton)

Ideally, I would love the consumer to have the ability to obtain distant notifications after altering the Settings, with out having to kill and relaunch the app.

Is it attainable to make push notifications work on this situation with out relaunching the app? For instance, might calling UIApplication.shared.registerForRemoteNotifications() when the app enters the foreground deal with this case?

Any insights on learn how to finest deal with this consumer circulate could be appreciated. Thanks!

last class AppDelegate: UIResponder, UIApplicationDelegate {
    var window: UIWindow?
    
    func software(_ software: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil) -> Bool {
        FirebaseApp.configure()
        Messaging.messaging().delegate = self

        UNUserNotificationCenter.present().getNotificationSettings { settings in
            guard settings.authorizationStatus == .approved else { return }
            DispatchQueue.major.async {
                software.registerForRemoteNotifications()
            }
        }
        return true
    }
    
    func software(_ software: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Knowledge) {
    // save system token
    }
    
    func software(_ software: UIApplication, didFailToContinueUserActivityWithType userActivityType: String, error: Error) {
        print("didFailToContinueUserActivityWithType: (error)")
    }
}

// MARK: - MessagingDelegate

extension AppDelegate: MessagingDelegate {
    
    func messaging(_ messaging: Messaging, didReceiveRegistrationToken fcmToken: String?) {
    // save fcm token
    }
}

Latest news
Related news

LEAVE A REPLY

Please enter your comment!
Please enter your name here