iOS 9下前台点击FCM推送无法跳转指定ViewController求助
Hey there! Let's tackle this issue you're facing with FCM push notifications on your iPhone 4s (iOS 9) — where tapping your custom banner in the foreground fails to jump to the specified ViewController. iOS 9 has some push handling quirks compared to newer versions, and combining that with a third-party custom banner can lead to missed click bindings or incorrect navigation logic. Here's your step-by-step fix:
1. Verify Your Custom Banner's Tap Event is Bound Correctly
First things first: if your custom banner isn't triggering any action on tap, the navigation code won't run at all. Double-check how you're setting up the banner's tap callback:
- Make sure you're explicitly binding a tap handler that includes your navigation logic, not just displaying the banner. For example:
// Example with a closure-based tap handler (adjust to match your banner library's API) let customBanner = YourThirdPartyBanner() customBanner.title = "Your Push Title" customBanner.subtitle = "Your Push Body" // Bind the tap action customBanner.onTap = { [weak self] in guard let self = self else { return } // Navigate to your target VC here let targetVC = YourTargetViewController() // Adjust based on your root controller type if let navController = self.window?.rootViewController as? UINavigationController { navController.pushViewController(targetVC, animated: true) } else { self.window?.rootViewController?.present(targetVC, animated: true) } } customBanner.show() - Use
weak selfto avoid memory leaks, and confirm you're accessing the correct root view controller (nav, tab bar, etc.) for your app's structure.
2. Fix Your didReceiveRemoteNotification Logic (iOS 9-Specific)
In iOS 9, the system doesn't show a native notification banner when your app is in the foreground — you have to handle that manually. Make sure you're parsing the push payload and passing navigation data to your banner:
func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable: Any]) { // Parse navigation parameters from the push payload (adjust keys to match your FCM setup) let targetIdentifier = userInfo["target_vc"] as? String ?? "" // Show your custom banner with the parsed data self.displayCustomPushBanner(userInfo: userInfo, targetID: targetIdentifier) // Critical for iOS 9: Notify the system you've handled the notification let settings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(settings) } private func displayCustomPushBanner(userInfo: [AnyHashable: Any], targetID: String) { let banner = YourThirdPartyBanner() // Extract push content from the aps dictionary let aps = userInfo["aps"] as? [String: Any] ?? [:] let alert = aps["alert"] as? [String: String] ?? [:] banner.title = alert["title"] ?? "" banner.subtitle = alert["body"] ?? "" // Pass the target VC identifier to the banner banner.targetID = targetID banner.onTap = { [weak self] in guard let self = self, !targetID.isEmpty else { return } // Initialize the target VC using the identifier (adjust if you use storyboards) if let vcClass = NSClassFromString(targetID) as? UIViewController.Type { let targetVC = vcClass.init() // Navigate based on your app's root controller if let tabBarVC = self.window?.rootViewController as? UITabBarController, let selectedNav = tabBarVC.selectedViewController as? UINavigationController { selectedNav.pushViewController(targetVC, animated: true) } else if let navVC = self.window?.rootViewController as? UINavigationController { navVC.pushViewController(targetVC, animated: true) } else { self.window?.rootViewController?.present(targetVC, animated: true) } } } banner.show() }
3. Confirm You're Accessing the Correct Window & Root Controller
iOS 9 can be finicky with window references. Ensure you're using the active key window to access your root controller:
- Use
UIApplication.shared.keyWindow(for iOS 9, this is reliable) instead of assuming a hardcoded window reference. - If your app uses a
UITabBarControlleras the root, make sure you're pushing to the selected tab's navigation controller (like in the code above).
4. Test if the Tap Event is Actually Triggering
Add a simple print statement to your banner's tap handler to confirm it's firing:
banner.onTap = { print("Custom banner tapped!") // Your navigation code here }
If you don't see this print in the console, the issue is with the banner library's tap binding — check its documentation for iOS 9 compatibility, or try a different library that explicitly supports iOS 9.
5. Double-Check Push Permissions for iOS 9
Make sure your app has the correct push permissions enabled in didFinishLaunchingWithOptions:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Request push permissions for iOS 9 let notificationSettings = UIUserNotificationSettings(types: [.alert, .badge, .sound], categories: nil) application.registerUserNotificationSettings(notificationSettings) application.registerForRemoteNotifications() // Rest of your setup code return true }
A final note: iPhone 4s is a 32-bit device running an older iOS version, so some third-party libraries might have dropped iOS 9 support. If all else fails, consider building a simple custom banner component yourself — it's easier to control tap events and navigation logic that way.
内容的提问来源于stack exchange,提问作者kunal kushwaha

