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