当点击 FCM 推送通知时,如何在选项卡栏中打开特定的视图控制器?

Posted

技术标签:

【中文标题】当点击 FCM 推送通知时,如何在选项卡栏中打开特定的视图控制器?【英文标题】:How do I open a particular view controller in a tab bar when a FCM push notification is tapped? 【发布时间】:2021-04-11 19:18:41 【问题描述】:

每当我在应用关闭时点击推送通知,应用就会一直崩溃。如果它在后台或当我点击推送通知时打开,那么它加载就好了。

我已经尝试过这里的建议: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()

【问题讨论】:

您正在做的是,您正在从窗口中提取 rootViewController。但是当应用程序关闭时,没有与窗口关联的 rootViewController。所以你没有设置任何 rootViewController,但是 window 需要一个 rootView 来显示内容,这就是你的应用程序崩溃的原因。但是当应用程序处于后台或活动状态时,它正在工作,因为您从窗口获取 rootViewController,您明白我的意思吗? 我明白你在说什么。看起来我正在做与您在另一个线程中的回答中指出的相同的事情,除了我没有使用故事板。 【参考方案1】:

这是我解释的代码

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 推送通知时,如何在选项卡栏中打开特定的视图控制器?的主要内容,如果未能解决你的问题,请参考以下文章

单击通知时更改选项卡栏

打开应用程序并锁定屏幕时未收到 Ionic FCM 推送通知,除非被点击

颤振 - 用户打开推送通知消息(FCM)后如何执行任务

当应用程序处于后台状态时,FCM 多个推送通知无法正常工作

当应用程序处于后台状态时,FCM多次推送通知无法正常工作

如何在接收 fcm 推送通知时处于退出状态时打开应用程序 - REACT NATIVE