iOS获取FCM令牌失败:Xcode 9.2中Messaging类等无法识别
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-updateto 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 FirebaseMessagingalso 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

