NotificationCenter observer method not being executed in some cases

I'm working with a deeplink I have to implement to my app. The deeplink is working because in some cases i will explain later the method is being executed. So when I tap the deeplink with a deeplink-tester-web I detect if the url is which I want and, if it's the case, i post a notification to NotificationCenter with a string parameter I need.

Case 1. I tap the deeplink when the view controller with the addObserver method is at the background with the app itself. It works (the only case)

Case 2. The app is closed so the deeplink opens the app directly in that view controller (the method is no being called event if the notification is there) Why?

Case 3. The app is at the background but in another screen. When I tap the deeplink and, after returning to the app, travel to my viewController the method is not called when the ViewController becomes visible. Why?

I've not seen anything in the documentation that can help me and i cannot access to the array of observers because the API does not allow that

Thanks. Any help will be appreciate.

Code:

When the deeplink is tapped the app delegate method that manages universal links call to this code when the url is a specific one

@objc func handleRecommendedOffer(offerId: String) {
        NotificationCenter.default.post(name: .deeplinkRecommendedOfferNotification, object: nil, userInfo: ["offerId": offerId])
    }

In my viewcontroller viewDidLoad (i've tried also viewWillAppear too):

NotificationCenter.default.addObserver(self, selector: #selector(goToDetail(notification:)), name: .deeplinkRecommendedOfferNotification, object: nil)

@objc private func goToDetail(notification: Notification) {
        if let userInfo = notification.userInfo, let offerId = userInfo["offerId"] as? String {
            showLoading()
            getOfferViewModel.getOfferDetail(id: offerId)
        }
    }
Answered by DTS Engineer in 822772022

The exact details will vary depending on the exacts details of your apps implementation but here is what I suspect is going on:

Case 2. The app is closed so the deeplink opens the app directly in that view controller (the method is no being called event if the notification is there) Why?

The system tends deliver event notification (like the deep link delegate) relatively early in the app launch cycle. The exact timing depends on the API involved and your app structure, but it can easily create a sequence like this:

  1. System launches app.
  2. Basic app initialization finishes ("applicationDidFinishLaunching")
  3. System calls delegate to deliver data (in this case, the deep link data)
  4. Your app creates it's view controller.

In other words, the deep link "event" was delivered to your app before the view controller was created to observe it.

Case 3. The app is at the background but in another screen. When I tap the deeplink and, after returning to the app, travel to my viewController the method is not called when the ViewController becomes visible. Why?

One of two things happened here:

  1. Your deeplink view controller wasn't on screen or in the active view hierarchy, so it didn't exist to receive the notification.

  2. The presentation of the visible structure meant that your deeplink view controller couldn't (or didn't) present itself. In other words, it did receive the notification, it just couldn't become active.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

Accepted Answer

The exact details will vary depending on the exacts details of your apps implementation but here is what I suspect is going on:

Case 2. The app is closed so the deeplink opens the app directly in that view controller (the method is no being called event if the notification is there) Why?

The system tends deliver event notification (like the deep link delegate) relatively early in the app launch cycle. The exact timing depends on the API involved and your app structure, but it can easily create a sequence like this:

  1. System launches app.
  2. Basic app initialization finishes ("applicationDidFinishLaunching")
  3. System calls delegate to deliver data (in this case, the deep link data)
  4. Your app creates it's view controller.

In other words, the deep link "event" was delivered to your app before the view controller was created to observe it.

Case 3. The app is at the background but in another screen. When I tap the deeplink and, after returning to the app, travel to my viewController the method is not called when the ViewController becomes visible. Why?

One of two things happened here:

  1. Your deeplink view controller wasn't on screen or in the active view hierarchy, so it didn't exist to receive the notification.

  2. The presentation of the visible structure meant that your deeplink view controller couldn't (or didn't) present itself. In other words, it did receive the notification, it just couldn't become active.

__
Kevin Elliott
DTS Engineer, CoreOS/Hardware

NotificationCenter observer method not being executed in some cases
 
 
Q