当点击 FCM 推送通知时,如何在选项卡栏中打开特定的视图控制器? [英] How do I open a particular view controller in a tab bar when a FCM push notification is tapped?
问题描述
每当我在关闭应用程序时点击推送通知时,应用程序都会崩溃.如果它在后台或在我点击推送通知时打开,那么它加载得很好.
The app keeps crashing whenever I tap the push notification when the app is closed. If it's in the background or open when I tap the push notification then it loads just fine.
我已经尝试过这里的建议:Swift - 如何在收到推送通知时打开特定的视图控制器?
I've tried the suggestions over here: Swift - How to open specific view controller when push notification received?
以下是应用委托中的相关功能.没有注册任何用于确定导致崩溃的确切位置的打印功能,因此我无法正确调试.我看到的唯一消息是:
Below are the relevant functions in the app delegate. Any print functions to determine where exactly the crash is caused is not being registered so I cannot debug properly. The only message I see is:
来自调试器的消息:由于信号 9 终止
Message from debugger: Terminated due to signal 9
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
// Navigation customization
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(hex: Constants.Colors.primary)!, NSAttributedString.Key.font: UIFont(name: "NexaLight", size: 18)!], for: UIControl.State.normal)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(hex: Constants.Colors.primary)!, NSAttributedString.Key.font : UIFont(name: "NexaBold", size: 18)! ], for: .highlighted)
UIBarButtonItem.appearance().setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor(hex: Constants.Colors.secondary)!, NSAttributedString.Key.font : UIFont(name: "NexaBold", size: 18)! ], for: .focused)
ApplicationDelegate.shared.application( application, didFinishLaunchingWithOptions: launchOptions )
if #available(iOS 10.0, *) {
// For iOS 10 display notification (sent via APNS)
UNUserNotificationCenter.current().delegate = self
}
// Firebase
FirebaseApp.configure()
// APN FCM
Messaging.messaging().delegate = self
// Stripe
StripeAPI.defaultPublishableKey = "test"
// Google sign-in
GIDSignIn.sharedInstance()?.clientID = "test"
return true
}
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window {
guard let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController else { return }
if let tabBarController = rootViewController as? MainTabBarController {
tabBarController.selectedIndex = 1
window.rootViewController = tabBarController
window.makeKeyAndVisible()
}
}
completionHandler()
}
推荐答案
这是我解释的代码
func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping () -> Void) {
if let window = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window {
if let rootViewController = (UIApplication.shared.connectedScenes.first?.delegate as? SceneDelegate)?.window?.rootViewController {
if let tabBarController = rootViewController as? MainTabBarController {
tabBarController.selectedIndex = 1
window.rootViewController = tabBarController
window.makeKeyAndVisible()
}
} else {
let tabBarController = UIStoryboard(name: "main", bundle: .main).instantiateViewController(identifier: "MainTabBarController") as? MainTabBarController
tabBarController.selectedIndex = 1
window.rootViewController = tabBarController
window.makeKeyAndVisible()
}
}
completionHandler()
}
这篇关于当点击 FCM 推送通知时,如何在选项卡栏中打开特定的视图控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!