简体   繁体   English

在没有 StoryBoard 的情况下点击通知时访问特定的 ViewController(以编程方式)

[英]Access specific ViewController when notification is Tapped WITHOUT StoryBoard (programatically)

I have an application that has 3 tabs: - Home - Inbox - Profile我有一个包含 3 个选项卡的应用程序: - 主页 - 收件箱 - 个人资料

When the user receives a notification, I want to open the correspond chat and when the user presses the back button, I want to be able to open the Inbox tab.当用户收到通知时,我想打开相应的聊天,当用户按下后退按钮时,我希望能够打开收件箱选项卡。

The following code that I have is this:我的以下代码是这样的:

     func userNotificationCenter(_ center: UNUserNotificationCenter,
                                    didReceive response: UNNotificationResponse,
                                    withCompletionHandler completionHandler: @escaping () -> Void) {

            let notificationContent = response.notification.request.content

            switch notificationContent.categoryIdentifier {

            case "ChatViewController":

                let tabBarVC = self.window?.rootViewController as! MainTabViewController

                tabBarVC.selectedIndex = 1 // This is the Inbox tab

                let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

                let inboxVC = InboxViewController()

                inboxVC.navigationController?.pushViewController(chatVC, animated: true)

            default:
                return
            }


            completionHandler()
        }

However after this line tabBarVC.selectedIndex = 1 , the code is not read, and it opens the InboxViewController , why is that and how can I open the chat?但是在这行tabBarVC.selectedIndex = 1 ,代码没有被读取,它打开InboxViewController ,为什么会这样,我该如何打开聊天?

its too simple check for selected controller using使用它对所选控制器的检查过于简单

let selectedController = tabBarVC.selectedViewController? 

and full function和全功能

          func userNotificationCenter(_ center: UNUserNotificationCenter,
                                        didReceive response: UNNotificationResponse,
                                        withCompletionHandler completionHandler: @escaping () -> Void) {

                let notificationContent = response.notification.request.content

                switch notificationContent.categoryIdentifier {

                case "ChatViewController":

                    let tabBarVC = self.window?.rootViewController as! MainTabViewController

                    tabBarVC.selectedIndex = 1 // This is the Inbox tab

                    let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

              if let selectedController = tabBarVC?.selectedViewController as? InboxViewController
        {
            selectedController?.navigationController?.pushViewController(chatVC, animated: true)
        }               

                default:
                    return
                }


                completionHandler()
            }

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) { func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {

        let notificationContent = response.notification.request.content

        switch notificationContent.categoryIdentifier {

        case "ChatViewController":

            let tabBarVC = self.window?.rootViewController as! MainTabViewController

            tabBarVC.selectedIndex = 1 // This is the Inbox tab

            let chatVC = ChatViewController(collectionViewLayout: UICollectionViewFlowLayout())

            if let selectedController = tabBarVC?.selectedViewController as? InboxViewController {
                selectedController?.navigationController?.pushViewController(chatVC, animated: true)
            } else if let nc =  tabBarVC?.selectedViewController as? UINavigationController {
                nc.pushViewController(chatVC, animated: true)
            }  

        default:
            return
        }


        completionHandler()
    }

// I have extended @Abu Ul Hassan's answer. // 我已经扩展了@Abu Ul Hassan 的回答。 As most probably the the selectedViewController will be a UINavigation controller.最有可能的 selectedViewController 将是一个 UINavigation 控制器。

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

相关问题 点击通知时,通过rootViewController显示viewController - Present viewController over rootViewController when a notification is tapped 轻按通知时启动特定的View Controller - Launching a specific View Controller when a notification is tapped 在通知中心中轻按或首次出现时,获取特定通知的标识符 - Get the identifier of a specific notification when it is tapped in the notification center or tapped when it first appears 情节提要中ViewController的访问属性 - Access property of ViewController in Storyboard 点击通知警报按钮时显示ViewController - Present ViewController on notification alertaction button tapped 在点击通知时无法正确显示ViewController - Can't present ViewController correctly on notification tapped 在通知中点击时转到特定视图 controller 并且用户未登录 - Going to a specific view controller when tapped in a notification and user is unlogged 如何在接收和点击推送通知时显示特定的UIViewController - How to display a specific UIViewController when a push notification is received and tapped 以编程方式返回到不同故事板中的上一个ViewController - Programatically return to previous ViewController in different storyboard 如何在 Swift 2.0 中没有 Storyboard 的情况下以编程方式从一个 ViewController 导航到另一个 - How to navigate from one ViewController to Other Programatically, without Storyboard in Swift 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM