Search code examples
iosswiftunusernotificationcenterusernotifications

UNUserNotificationCenterDelegate didReceive response for Local Notifications not called when app is not running


I am trying to use the new UserNotifications framework for local notifications. I redirect the user to a different viewcontroller when the notification is clicked/open. The notification is not handled properly by UNUserNotificationCenterDelegate didReceive method.

When the notification is in the foreground, and then it is clicked, it redirects to the right viewcontroller, however, when the app is closed and the notification is opened, the app is launched, however, the main viewcontroller is displayed instead.


Solution

  • Problem wasn't actually with the didReceive, but instead with the way the transitions were being made being viewcontrollers, due to methods returning before completion.

    I was simulating a normal user flow programmatically. The problem with that is that tableview:didselectRowAt was returning before the performSegue method inside it was executed.

    I ended up manually loading the VCs instead and explicitly calling UInavigationController:pushViewController(vc:animated:) to push them on the navigation stack.