iOS Swift 从推送通知以编程方式导航到某些 ViewController [英] iOS Swift Navigate to certain ViewController programmatically from push notification

查看:28
本文介绍了iOS Swift 从推送通知以编程方式导航到某些 ViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用推送通知来通知用户应用中发生的不同类型的事件.某种类型的推送通知应该打开一个特殊的视图控制器(例如,关于新聊天消息的通知会在点击时导航到聊天)

I use push notifications to inform the user about different types of events happening in the app. A certain type of push notification should open a special viewcontroller (f.e a notification about a new chat message does navigate to the chat on click)

为了实现这一点,我在我的应用程序委托中尝试了以下代码:

To achieve this, i tried the following code inside my app delegate:

func applicationDidBecomeActive(_ application: UIApplication) {
 if var topController = UIApplication.shared.keyWindow?.rootViewController {
        while let presentedViewController = topController.presentedViewController {
            topController = presentedViewController
        }
        topController.tabBarController?.selectedIndex = 3
    }
}

它不会移动到任何地方.我在这里错过了什么?

It does not move anywhere. What am I missing here?

推荐答案

这正是您所需要的.
使用导航控制器推送通知视图控制器

This is what you need.
Use your navigation controller to push your notification view controller

    let mainStoryBoard : UIStoryboard       = UIStoryboard(name: "Main", bundle: nil)
    let navigationController : UINavigationController = mainStoryBoard.instantiateViewController(withIdentifier: "MainNavigationController") as! UINavigationController

    let notifcontrol : UIViewController     = (mainStoryBoard.instantiateViewController(withIdentifier: "NotificationViewController") as? NotificationViewController)!

    navigationController.pushViewController(notifcontrol, animated: true)

    self.window = UIWindow(frame: UIScreen.main.bounds)
    self.window?.rootViewController = navigationController
    self.window?.makeKeyAndVisible()

这篇关于iOS Swift 从推送通知以编程方式导航到某些 ViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆