iOS Swift4集成Firebase推送崩溃:GCM_SENDER_ID不可为空
Hey there, let's work through this frustrating crash you're hitting. Your app was working fine with Firebase push notifications, then suddenly started throwing this error—super annoying, right? The root issue is clearly that Firebase InstanceID can't find a valid GCM_SENDER_ID, so let's go through the most common fixes step by step:
Double-check your GoogleService-Info.plist
First, confirm this file is still in your project and correctly linked. Sometimes after Pod updates or project cleanups, it can get accidentally removed or unlinked.- Make sure the file has a
GCM_SENDER_IDfield with the correct value (you can grab this from your Firebase project's settings page). - Verify the Target Membership for the file is checked for your main app target (select the file, look in the right-hand inspector pane).
- Make sure the file has a
Verify Firebase initialization order
In yourAppDelegate'sdidFinishLaunchingWithOptionsmethod, ensure you're initializing Firebase before setting up push notifications. The order matters here—InstanceID needs Firebase to be configured first to pull the Sender ID. Your code should look something like this:func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool { // Initialize Firebase first FirebaseApp.configure() // Then set up user notifications and Firebase Messaging let center = UNUserNotificationCenter.current() center.delegate = self center.requestAuthorization(options: [.alert, .badge, .sound]) { granted, error in // Handle authorization result } application.registerForRemoteNotifications() Messaging.messaging().delegate = self return true }Clean and reinstall your Pods
Since you mentioned deleting some content already, let's do a full Pod reset to rule out cached issues:pod deintegrate pod clean rm Podfile.lock pod installThis removes old Pod artifacts and pulls fresh copies of Firebase dependencies, which often fixes configuration mismatches.
Check Other Linker Flags in Build Settings
Firebase relies on the-ObjClinker flag to properly load its components. Head to your target's Build Settings > Other Linker Flags and make sure-ObjCis present. If it was removed or modified during updates, this could prevent InstanceID from accessing the Sender ID.Manually add GCM_SENDER_ID to Info.plist (as a last resort)
If the GoogleService-Info.plist still isn't passing the value correctly, you can manually inject it into your Info.plist:- Right-click your Info.plist file and select Open As > Source Code.
- Add the following snippet, replacing
your-sender-id-herewith your actual Sender ID from Firebase:<key>GCM_SENDER_ID</key> <string>your-sender-id-here</string>
If none of these steps work, try re-downloading the latest GoogleService-Info.plist from your Firebase project—sometimes the file gets outdated if you made changes to your project settings.
内容的提问来源于stack exchange,提问作者Dragan Todorovic

