簡體   English   中英

如何處理 iOS Swift 中的推送通知點擊?

[英]How to handle push notification click in iOS Swift?

我正在嘗試處理推送通知點擊。 當推送通知進來時,我想在點擊推送通知后將用戶引導到應用程序的某個部分。 首先,應用程序中有 SplashScreen,它通過檢查用戶之前是否從 API 調用登錄,將 TabBar 顯示為“Present Modally”。 我想向用戶顯示的是 TabBar 第一個選項卡中導航 controller 中表格視圖中提要的內容。 但我無法到達它並將其顯示為單擊 tableview。

-SplashScreen -------> TabBar -> Navigation Controller -> Table View Controller -> ContentView(我想顯示)

我嘗試編寫 UIApplication 擴展,但沒有成功。 這是擴展名

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
}
}

我在 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