.NET Android中Firebase Messaging初始化失败、无法获取Token求助
1. Ensure google-services.json has the correct Build Action
- Right-click
google-services.jsonin Solution Explorer → Properties - Set Build Action to
GoogleServicesJson(this ensures the file is processed by the google-services plugin)
2. Add the google-services Gradle plugin reference
Edit your project's .csproj file and add this entry to directly resolve the plugin application error:
<ItemGroup> <GradlePluginReference Include="com.google.gms.google-services" Version="4.4.1" /> </ItemGroup>
3. Remove manual FirebaseApp.InitializeApp call
Newer Firebase versions automatically initialize via FirebaseInitProvider. Delete the FirebaseApp.InitializeApp(this); line from your OnCreate method—manual calls can conflict with automatic initialization.
4. Update outdated AndroidManifest receiver entries
The FirebaseInstanceIdReceiver and FirebaseInstanceIdInternalReceiver are deprecated. Replace your existing receiver entries in AndroidManifest.xml with:
<receiver android:name="com.google.firebase.messaging.FirebaseMessagingReceiver" android:exported="true" android:permission="com.google.android.c2dm.permission.SEND"> <intent-filter> <action android:name="com.google.android.c2dm.intent.RECEIVE" /> <category android:name="${applicationId}" /> </intent-filter> </receiver>
5. Verify package name consistency
- Confirm the
package_namefield ingoogle-services.jsonexactly matches your project's Application ID (found in Project Properties → Android Manifest → Package name) - Mismatched names will block Firebase from loading the correct configuration.
6. Clean and rebuild the project
- Delete the
binandobjfolders from your project directory - Clean the solution (Build → Clean Solution)
- Rebuild the solution (Build → Rebuild Solution)
This clears cached build artifacts that may cause conflicts.
7. Align all Firebase NuGet package versions
Ensure all Firebase-related NuGet packages (e.g., Xamarin.Firebase.Core, Xamarin.Firebase.Messaging) are on the same version. Mismatched versions often lead to initialization failures.
After applying these steps, run the app again. You should see the OnNewToken log output once the token is successfully generated.
内容的提问来源于stack exchange,提问作者Balint Fodor

