Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Handling a Notification

How do I get the notification to take me to the desire viewController?

Hey, I'm working on a project but struggling to get the notification to take me to my "NotiViewController". Sometimes it works as desired but other times it just opens my app to whatever viewController I was last on.

I hacked this code together from from Amit's video and some online sources:

extension QuestionViewController: UNUserNotificationCenterDelegate{
   func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: 
   UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> 
   Void) {
      let vc = storyboard?.instantiateViewController(withIdentifier: "NotiViewControllerID") 
      as! NotiViewController
      show(vc, sender: Any?.self)        
      completionHandler()
}

I want to be able to send a notification that asks the user a question, then once it is clicked have the user taken to a answer...

eg notificationQuestion: "How many states are in the contiguous United States?" =>tap notification => loads the appropriate NotiViewController: "48"

1 Answer

Ok, After moving a bit of code around I was able to solve this particular issue...

I noticed that the the desired viewcontroller would load only if the last viewcontroller loaded had the code: ` let notifictationCenter = UNUserNotificationCenter.current() notification.delegate = self

so the fix was to add that bit of code above and the extension to the other stubborn viewController and that did it... but I have another issue now...