[英]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.