swift - 当点击 FCM 推送通知时,如何在选项卡栏中打开特定的视图控制器?
问题描述
每当我在应用程序关闭时点击推送通知时,应用程序就会不断崩溃。如果它在后台或当我点击推送通知时打开,那么它加载就好了。
我已经尝试过这里的建议:Swift - How to open specific view controller when push notification received?
以下是应用程序委托中的相关功能。没有注册任何用于确定导致崩溃的确切位置的打印功能,因此我无法正确调试。我看到的唯一消息是:
来自调试器的消息:由于信号 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()
}
推荐阅读
- c++ - 如何允许 C++ 程序用户请求返回程序的前一个步骤?
- bash - 生成的 SHA256 值与预期的 SHA256 值不同
- html - 从数据库中提取字符串时如何在 JSX 中呈现 HTML 标签
- twitter-bootstrap - 您如何在 Bootstrap 4.5 中使用 $spacer 变量?
- vue.js - vue.js 函数仅在值大于零时工作
- numpy - 带有 3d 数组的 Numpy 点积
- javascript - 滚动时菜单变成纯色?JS?
- c# - 错误 CS0246 使用枚举时找不到类型或命名空间名称
- python - 有没有办法将 Python 程序编译为二进制文件并将其与 Scratch Dockerfile 一起使用?
- php - 在 php 中创建表前缀,就像使用 Wordpress 原始查询一样