Navigating to specific tab when clicking on FCM push notification in AppDelegate iOS - Swift

Viewed 464

Been scratching my head for a day trying to get this to work. I was receiving the notification but never navigated to the specific tab when clicked.


    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                      didReceive response: UNNotificationResponse,
                                      withCompletionHandler completionHandler: @escaping () -> Void) {
            let userInfo = response.notification.request.content.userInfo
        
            // Print message ID.
            if let messageID = userInfo[gcmMessageIDKey] {
              print(tag + "Message ID: \(messageID)")
            }
            
            // With swizzling disabled you must let Messaging know about the message, for Analytics
            // Messaging.messaging().appDidReceiveMessage(userInfo)
            // Print full message.
            print(userInfo)
            
        //    let myTabBar = self.window?.rootViewController as? UITabBarController
        //    myTabBar?.selectedIndex = 2
            
            //TabBarController - StoryBoard Id
            let storyboard = UIStoryboard(name: "Main", bundle: nil)
            if let tabController = storyboard.instantiateViewController(withIdentifier: "TabBarController") as? UITabBarController {
                tabController.selectedIndex = 2
            }
        
            completionHandler()
          }

2 Answers

For iOS 14.4 using Swift. With the help from other questions, this is what worked for me in the end.


    func userNotificationCenter(_ center: UNUserNotificationCenter,
                                  didReceive response: UNNotificationResponse,
                                  withCompletionHandler completionHandler: @escaping () -> Void) {
        let userInfo = response.notification.request.content.userInfo
    
        // Print message ID.
        if let messageID = userInfo[gcmMessageIDKey] {
          print(tag + "Message ID: \(messageID)")
        }
        
        // With swizzling disabled you must let Messaging know about the message, for Analytics
        // Messaging.messaging().appDidReceiveMessage(userInfo)
        // Print full message.
        print(userInfo)
        
        let scene = UIApplication.shared.connectedScenes.first
            if let sceneDelegate = scene?.delegate as? SceneDelegate {
                if let tabController = sceneDelegate.window?.rootViewController as? UITabBarController {
                      tabController.selectedIndex = 2
                }
    
            }
        
        print("Clicked Notification")
    
        completionHandler()
      }

Let me know if you need more help regarding FCM push notifications. Happy coding everyone!

For Navigate to Specific ViewController I use this extention

public extension UIApplication {
    class func getTopViewController(base: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        
        if let nav = base as? UINavigationController {
            return getTopViewController(base: nav.visibleViewController)
            
        } else if let tab = base as? UITabBarController, let selected = tab.selectedViewController {
            return getTopViewController(base: selected)
            
        } else if let presented = base?.presentedViewController {
            return getTopViewController(base: presented)
        }
        return base
    }
}

then call it As below:

  func userNotificationCenter(_ center: UNUserNotificationCenter,
                              didReceive response: UNNotificationResponse,
                              withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo

    // Print message ID.
    if let messageID = userInfo[gcmMessageIDKey] {
      print(tag + "Message ID: \(messageID)")
    }
    
    // With swizzling disabled you must let Messaging know about the message, for Analytics
    // Messaging.messaging().appDidReceiveMessage(userInfo)
    // Print full message.
    print(userInfo)
    
     let storyboard = UIStoryboard(name: "yourStoryboardName", bundle: nil)
        let vc = storyboard.instantiateViewController(withIdentifier: "yourStoryboardId") as! YourViewcontroller
        //                   vc.nNotificationID = notificationID
        
        if let topVC = UIApplication.getTopViewController() {
            topVC.navigationController?.pushViewController(vc, animated: false)
        }
    
    print("Clicked Notification")

    completionHandler()
  }
Related