简体   繁体   English

如何处理 iOS Swift 中的推送通知点击?

[英]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.首先,应用程序中有 SplashScreen,它通过检查用户之前是否从 API 调用登录,将 TabBar 显示为“Present Modally”。 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.我想向用户显示的是 TabBar 第一个选项卡中导航 controller 中表格视图中提要的内容。 But I can't reach it and show it as clicked on tableview.但我无法到达它并将其显示为单击 tableview。

-SplashScreen -------> TabBar -> Navigation Controller -> Table View Controller -> ContentView(I want to show) -SplashScreen -------> TabBar -> Navigation Controller -> Table View Controller -> ContentView(我想显示)

I tried writing the UIApplication extension but it didn't work.我尝试编写 UIApplication 扩展,但没有成功。 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我在 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)
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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