Sharlene
Sharlene

Reputation: 5

IOS Open local Notification to Specific View Controller or Tab

I have researched and came across a few articles about opening local notifications to a specific view and showing the information within the view controller. Unfortunately, I'm still unable to make the function work in my app.

I have a quote app and I send local and push notifications. When I have a scheduled notification, the user should be able to click the notfication and be directed to the "TopViewController" and the controller should display the notification information. Instead the app opens up to the last view controller the user was on.

Could someone tell me what I am doing wrong? Here is the code the I have in the "TopViewController" ViewDidLoad


NotificationCenter.default.addObserver(self, selector: ("Scheduled:"), name:NSNotification.Name(rawValue: "Scheduled"), object: nil)

Here is the code I have in the AppDelegate:

@Published var quote = Quote(id: "", text: "", author: "")

      func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
          
          let application = UIApplication.shared
          
          if(application.applicationState == .active){
              print("user tapped the notification bar when the app is in foreground")
          }
          
          if(application.applicationState == .inactive)
          {
              print("user tapped the notification bar when the app is in background")
          }
          
          guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else {
              return
          }
          print("got active scene")
          
          let storyboard = UIStoryboard(name: "Main", bundle: nil)
          if  let conversationVC = storyboard.instantiateViewController(withIdentifier: "TopViewController") as? TopViewController,
          let tabBarController = self.window?.rootViewController as? UITabBarController

          {
              conversationVC.shareQuote?.text = response.notification.request.content.body
              conversationVC.shareQuote?.author = response.notification.request.content.subtitle
            }
         
              completionHandler()
      }
    
    

The notification doesn't open up to the right view controller nor does it show the notification information.

Upvotes: 0

Views: 1770

Answers (1)

Shawn Frank
Shawn Frank

Reputation: 5153

Here are a few ideas I have, however, I cannot be certain if this is your fix but it was too much to put into the comments.

  1. Are you setting the app delegate as the UNNotificationCenter's delegate ?
UNUserNotificationCenter.current().delegate = self
  1. Does anything print to your console like print("got active scene") or print("user tapped the notification bar when the app is in foreground") ?

  2. If you have done both of the above, can you add breakpoints inside this block of code

{
    conversationVC.shareQuote?.text = response.notification.request.content.body
    conversationVC.shareQuote?.author = response.notification.request.content.subtitle
}

This will tell you if there is some issue in your logic if it does not enter this block of code

Update

  1. To manage notifications in the foreground, you need to also implement this UNUserNotificationCenterDelegate method:
// This will show the notification when the app is in
// the foreground. This is optional
func userNotificationCenter(_ center: UNUserNotificationCenter,
                            willPresent notification: UNNotification,
                            withCompletionHandler completionHandler: @escaping (UNNotificationPresentationOptions) -> Void) {
    completionHandler([.list, .banner, .badge, .sound])
}

Upvotes: 0

Related Questions