简体   繁体   中英

How to handle push notification click in iOS Swift?

I'm trying to handle push notification click. When push notification comes in, I want to direct the user to a certain part of the application after clicking on push notification. Firstly, there is SplashScreen in the application, where it shows a TabBar as "Present Modally" by checking whether the user has previously logged in from API Call. What I want to show the user is the content of a feed in the table view in the navigation controller in the first tab of the TabBar. But I can't reach it and show it as clicked on tableview.

-SplashScreen -------> TabBar -> Navigation Controller -> Table View Controller -> ContentView(I want to show)

I tried writing the UIApplication extension but it didn't work. Here's the extension

extension UIApplication {
class func topViewController(base: UIViewController? = (UIApplication.shared.delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
        return topViewController(base: nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
        if let selected = tab.selectedViewController {
            return topViewController(base: selected)
        }
    }
    if let presented = base?.presentedViewController {
        return topViewController(base: presented)
    }
    return base
}
}

I wrote the following code in didFinishLaunchingWithOptions

      if let option = launchOptions {
        let info = option[UIApplication.LaunchOptionsKey.remoteNotification] as? [String:Any]
        if let info = info {
            if info["type"] as! String == "site-notification" {
                let contentVC = Destination().FeedCoontentVC

                contentVC.selectedSite = (info["link"] as! String)
                contentVC.title = "asd"
               UIApplication.topViewController()?.present(contentVC, animated: true, completion: nil)
            }
        }
    }
if info["type"] as! String == "site-notification" {

    let tabbarSB = UIStoryboard(name: StoryBoard.tabBar, bundle: nil)
    let tabbarVC = tabbarSB.instantiateViewController(withIdentifier: ViewController.TabBar) as! TabBarController
    self.window?.rootViewController = tabbarVC 

    tabbarVC.viewControllers = [] // add your tab view controllers

    for child in (tabbarVC.childViewControllers) {
        if child.restorationIdentifier == "tablevc" //add restoration id to storyboard {
            tabbarVC.selectedIndex = 1
            let tableVC = (child.childViewControllers[0]) as! TableViewController
            let contentSB = UIStoryboard(name: StoryBoard.contentSB , bundle: nil)
            let contentVC = contentSB.instantiateViewController(withIdentifier: ViewController.contentVC ) as! FeedCoontentVC    
            contentVC.selectedSite = (info["link"] as! String)
            contentVC.title = "asd"
            tableVC.navigationController?.pushViewController(contentVC, animated: false)
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM