You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

iOS获取FCM令牌失败:Xcode 9.2中Messaging类等无法识别

Fixing FCM Token Recognition Issues in Xcode 9.2 (Swift 4, iOS 11.2)

Hey there, let's work through why Xcode isn't recognizing those FCM-related classes and methods in your AppDelegate. Given your older environment (Xcode 9.2, Swift 4, deployment target 11.2), the issue almost certainly comes from a version mismatch between the Firebase docs you're following and the Firebase SDK version compatible with your setup. Here's how to fix it:

1. Verify Your Podfile & Firebase SDK Version

First, make sure your Podfile pulls in a Firebase version that works with Swift 4 and Xcode 9.2. Newer Firebase versions dropped the FIR prefix and updated method signatures, but those won't play nice with your setup.

  • Update your Podfile to specify a compatible Firebase version (Firebase 5.x is a safe bet for your environment):
    platform :ios, '11.2'
    use_frameworks!
    
    target 'YourAppName' do
      pod 'Firebase/Core'
      pod 'Firebase/Messaging', '~> 5.0'
    end
    
  • Run pod install --repo-update to install the correct SDK version.

2. Fix AppDelegate Imports & Delegate Conformance

In older Firebase versions, messaging classes still use the FIR prefix. Adjust your AppDelegate code to match:

  • Use import Firebase (Firebase 5.x includes Messaging under the main import; import FirebaseMessaging also works if preferred).
  • Replace the unprefixed class/delegate names with their prefixed counterparts:
    import UIKit
    import Firebase
    
    @UIApplicationMain
    class AppDelegate: UIResponder, UIApplicationDelegate, FIRMessagingDelegate {
        // ... your existing code ...
    }
    

3. Correct the Delegate Method Signature

The FCM token receive method has a different signature in older Firebase versions. Replace the method from the new docs with this compatible version:

func messaging(_ messaging: FIRMessaging, didReceiveRegistrationToken fcmToken: String) {
    print("FCM Registration Token: \(fcmToken)")
    // Store or send this token to your server as needed
}

4. Set Up the Messaging Delegate Properly

In your application(_:didFinishLaunchingWithOptions:) method, ensure you configure Firebase first and set the delegate correctly:

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
    FirebaseApp.configure()
    FIRMessaging.messaging().delegate = self
    
    // ... other setup code ...
    return true
}

Bonus: Don’t Miss Critical Setup Steps

Enable push notifications in your Xcode project (Capabilities > Push Notifications) and upload your APNs key to the Firebase Console—these are easy to overlook but essential for FCM to function.

After making these changes, clean your project (Cmd+Shift+K) and rebuild. Xcode should now recognize all the FCM classes and methods, and you’ll start receiving the registration token as expected.

内容的提问来源于stack exchange,提问作者calcMan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:54:49