I'm trying to use the iOS 10 UserNotifications framework to do something very basic: open a specific ViewController when the default action is selected. I've been through a number of tutorials but they all focus on custom categories and actions, and they also don't really show the code for launching a specific ViewController after having determined the action (this is simply left as a comment or a print statement).
I want to do one of two things, depending on what is possible:
- Instantiate and display a modal ViewController that is not the storyboard's initial view controller, or
- display the initial ViewController, but first inject some information into it, such that it can use this information to determine what ViewController to segue to next.
My AppDelegate sets itself as the UNUserNotificationCenterDelegate:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// Make this object responsible for handling notification events.
UNUserNotificationCenter.current().delegate = self
return true
}
And then I want to use the following to override what ViewController is selected:
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
switch response.actionIdentifier {
case UNNotificationDismissActionIdentifier:
// Notification was dismissed. Do nothing.
completionHandler()
case UNNotificationDefaultActionIdentifier:
// App opened from notification.
// ??? Select ViewController to show based on information found in the notification ???
completionHandler()
default:
completionHandler()
}
}