如果通知已打开,如何直接转到特定的视图控制器 [英] How to go directly to a specific View Controller if Notification has been opened
问题描述
我有一个 UITabBarController
,它在一个选项卡中显示一个聊天表.如果用户单击一行,将显示一个视图控制器,显示最新消息.
I have an UITabBarController
which presents in one tab a table of chats. 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.
这是我目前的代码:
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.
推荐答案
didFinishLaunchingWithOptions
不一定会被调用,当您点击通知时,只有当您的应用不再在内存中时才调用
didFinishLaunchingWithOptions
is not necessarily called, when you click on a notification, only if your app is not in the memory anymore
在 appDelegate 的 willFinishLaunchingWithOptions
函数中,为 currentNotificationCenter 设置一个委托
In your willFinishLaunchingWithOptions
function of your appDelegate, set up a delegate for the currentNotificationCenter
func application(_ application: UIApplication, willFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
UNUserNotificationCenter.current().delegate = self
}
确保您的 AppDelegate
实现了与您相关的 UNUserNotificationCenterDelegate
Make sure your AppDelegate
implements UNUserNotificationCenterDelegate
relevant for you could be this
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()
}
<小时>
更新以回答您的后续问题:如何在您的 UITabbarController
中将 UIViewController
添加到 UINavigationController
>
为了实例化您的 ViewController 并将其添加到您的 UITabbarController
:
In order to instantiate your ViewController and add it to your 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)
这篇关于如果通知已打开,如何直接转到特定的视图控制器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!