简体   繁体   English

如果已打开通知,如何直接转到特定的View Controller

[英]How to go directly to a specific View Controller if Notification has been opened

I have an UITabBarController which presents in one tab a table of chats. 我有一个UITabBarController ,它在一个选项卡中显示一个聊天表。 If the user clicks on one row, a view controller will be displayed that presents the latest messages. 如果用户单击一行,将显示一个视图控制器,其中显示了最新消息。

Now I am introducing Push notifications. 现在,我要介绍推送通知。 The desired behavior would be to automatically go to the chat room if the user opens up the Push notification. 如果用户打开“推送”通知,则期望的行为是自动进入聊天室。 Although there's plenty information available on how to handle these use cases, I do not succeed in implementing it. 尽管有很多有关如何处理这些用例的信息,但是我并没有成功实现它。

Here's my code so far: 到目前为止,这是我的代码:

   func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.

        if (!isUserLoggedIn()) {
            // show login vc
        } else {
            let protectedController = mainStoryboard.instantiateViewController(withIdentifier: "TabBarController") as! UITabBarController

            if let notificationPayload = launchOptions?[UIApplicationLaunchOptionsKey.remoteNotification] as? NSDictionary {
                if let chatId = notificationPayload["chatId"] as? String {
                    print(chatId)
                    // show chat VC
                }
            }

            window!.rootViewController = protectedController
            window!.makeKeyAndVisible()
        }


        let center = UNUserNotificationCenter.current()
        center.requestAuthorization(options: [.alert, .sound]) { (granted, error) in
            // Enable or disable features based on authorization.
        }
        application.registerForRemoteNotifications()

        return true
    }

The print statement will never be called but that may be due to the fact that the application is completely closed before opening the notification. 不会调用print语句,但这可能是由于在打开通知之前应用程序已完全关闭。

didFinishLaunchingWithOptions is not necessarily called, when you click on a notification, only if your app is not in the memory anymore 当您单击通知时,仅当您的应用程序不再在内存中时, didFinishLaunchingWithOptions不一定会调用

In your willFinishLaunchingWithOptions function of your appDelegate, set up a delegate for the currentNotificationCenter 在您的willFinishLaunchingWithOptions函数中,为currentNotificationCenter设置一个委托

func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
    UNUserNotificationCenter.current().delegate = self
}

Make sure your AppDelegate implements UNUserNotificationCenterDelegate relevant for you could be this 确保您的AppDelegate实现与您相关的UNUserNotificationCenterDelegate可能是这样

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
    let userInfo = response.notification.request.content.userInfo
    if let chatID = userInfo["chatID"] as? String {
        // here you can instantiate / select the viewController and present it
    }
    completionHandler()
}

Update to answer your follow-up question: How to add a UIViewController to a UINavigationController within your UITabbarController 更新以回答您的后续问题:如何在UITabbarController UIViewController添加到UINavigationController

In order to instantiate your ViewController and add it to your UITabbarController : 为了实例化ViewController并将其添加到UITabbarController

let myViewController = MyViewController() 
guard let tabbarController = self.window.rootViewController as? UITabbarController else {
    // your rootViewController is no UITabbarController
    return
}
guard let selectedNavigationController = tabbarController.selectedViewController as? UINavigationController else {
    // the selected viewController in your tabbarController is no navigationController!
    return
}
selectedNavigationController.pushViewController(myViewController, animated: true)

When the user taps on a notification, callback method of the app delegate is: 当用户点击通知时,应用程序委托的回调方法为:

application:didReceiveRemoteNotification:fetchCompletionHandler:

More information about notifications from Apple . 有关来自Apple的通知的更多信息。

You should put your code inside this method. 您应该将代码放入此方法中。

Also you can create Router class (subclass of NSObject), which will show chat view controller and will be responsible for navigation between view controllers in application. 您还可以创建Router类(NSObject的子类),该类将显示聊天视图控制器,并负责在应用程序中的视图控制器之间进行导航。

It's a good manner incapsulate this logic into separate class, not keeping it in the AppDelegate class. 这是将这种逻辑封装到单独的类中的一种好方法,而不是将其保留在AppDelegate类中。

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

相关问题 我如何知道该应用在后台被杀死后是否已从本地通知中打开? - How can I know if the app has been opened from a local notification after it has been killed in the background? 检查是否已在此运行中打开视图 - Check if view has been opened in this run 单击iPhone通知中心中的消息时如何进入特定视图? - How to go to a specific view when clicking a message in iPhone notification center? 从推送通知打开应用程序时,如何进入某个视图? - How can I go to a certain view when the app is opened from push notification? 如何测试视图控制器是否已呈现? - How can I test that a view controller has been presented? 如何检查视图 controller 是否已在 Swift 中被取消 - How to check if a view controller has been dismissed in Swift 视图控制器被解雇后如何显示警报 - How to present an alert after view controller has been dismissed 模态视图控制器被关闭后如何调用函数 - How to call a function when a Modal View Controller has been dismissed 如何直接从通知警报的点击打开深埋在视图层次结构中的视图控制器 - How do I open a view controller that is buried deep into the view hierarchy, directly from tap of a Notification alert 如何在swift 3中收到推送通知时打开特定的视图控制器 - How to open specific view controller when push notification received in swift 3
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM