This error occurs when your Android app's package name doesn't match the one registered in your Firebase project configuration. The mismatch prevents Firebase Cloud Messaging from identifying your app correctly.
The "invalid-package-name" error in Firebase Cloud Messaging indicates that there's a discrepancy between your Android application's actual package name and the package name registered in your Firebase project configuration. Firebase uses the package name as a unique identifier to associate your app with the correct Firebase project and configuration. When you initialize Firebase services, it validates that the package name in your app matches one of the package names registered in the google-services.json configuration file. If no match is found, or if another Firebase/Google Cloud project already has an OAuth 2.0 client ID with your package name and SHA-1 combination, this error is thrown. This validation is critical for security and ensures that Firebase Cloud Messaging tokens and messages are delivered to the correct application. The package name must match exactly - even differences in capitalization or additional characters will cause this error.
First, identify the package name your app is actually using. Open your app's build.gradle file and check the applicationId:
// android/app/build.gradle
android {
defaultConfig {
applicationId "com.example.myapp" // This is your package name
// ...
}
}Also verify the package attribute in your AndroidManifest.xml:
<!-- android/app/src/main/AndroidManifest.xml -->
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
<!-- ... -->
</manifest>Note the exact package name including capitalization - it must match exactly.
Open your google-services.json file (located at android/app/google-services.json) and find the package_name field:
{
"project_info": { ... },
"client": [
{
"client_info": {
"mobilesdk_app_id": "1:123456789:android:abc123...",
"android_client_info": {
"package_name": "com.example.myapp"
}
}
}
]
}Compare this package_name with the one from step 1. They must match exactly.
If the package names don't match, you have two options:
Option A: Update your app's package name (if Firebase is correct):
1. Update the applicationId in build.gradle to match Firebase
2. Update the package in AndroidManifest.xml
3. Refactor your code directory structure if needed
4. Clean and rebuild: ./gradlew clean
Option B: Update Firebase Console (if your app is correct):
1. Go to Firebase Console (https://console.firebase.google.com)
2. Select your project
3. Go to Project Settings > General
4. Find your Android app in the "Your apps" section
5. If the package name is wrong, delete the app and re-add it with the correct package name
6. Download the new google-services.json file
7. Replace the old file in your project at android/app/google-services.json
After updating, rebuild your app completely.
If you use build variants (flavors or build types) that add an applicationIdSuffix, you need separate Firebase apps and config files:
// android/app/build.gradle
android {
buildTypes {
debug {
applicationIdSuffix ".debug"
// This creates package: com.example.myapp.debug
}
release {
// This uses: com.example.myapp
}
}
}For this scenario:
1. Register each variant as a separate app in Firebase Console:
- com.example.myapp (release)
- com.example.myapp.debug (debug)
2. You'll get one google-services.json with multiple client entries, or
3. Use different config files per variant using gradle:
android {
buildTypes {
debug {
applicationIdSuffix ".debug"
}
}
}
// Copy the appropriate google-services.json based on build type
task switchToDebug {
doLast {
copy {
from 'google-services-debug.json'
into 'app'
rename { 'google-services.json' }
}
}
}After making changes, ensure all build caches are cleared:
# Clean Gradle cache
cd android
./gradlew clean
# Clear build folders
rm -rf app/build
rm -rf build
# For React Native projects
cd ..
rm -rf node_modules
npm install
# For Flutter projects
flutter clean
flutter pub getThen rebuild your app completely:
# Android
./gradlew assembleDebug
# React Native
npx react-native run-android
# Flutter
flutter runVerify the fix by checking Firebase initialization in your app:
// Android (Kotlin)
import com.google.firebase.Firebase
import com.google.firebase.messaging.FirebaseMessaging
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// Test FCM token retrieval
FirebaseMessaging.getInstance().token.addOnCompleteListener { task ->
if (task.isSuccessful) {
val token = task.result
Log.d("FCM", "Token retrieved successfully: $token")
} else {
Log.e("FCM", "Failed to get token", task.exception)
}
}
}
}If the token is retrieved successfully, your package name configuration is correct.
SHA-1 Fingerprint Conflicts: In rare cases, the error "invalid-package-name" can occur if another Firebase or Google Cloud project contains an OAuth 2.0 client ID with both the same package name AND SHA-1 fingerprint that you're trying to use. This typically happens when migrating between projects or working across multiple development teams. To resolve this, you'll need to either use a different package name or remove the conflicting OAuth client from the other project.
Multiple Firebase Projects: If you're working with multiple Firebase projects (e.g., development, staging, production), ensure you're using the correct google-services.json for each environment. Consider using separate build flavors for each environment with their own configuration files.
Cordova/Ionic/Capacitor Projects: For hybrid mobile frameworks, the package name is typically defined in config.xml (Cordova/Ionic) or capacitor.config.json (Capacitor). Ensure these match your Firebase configuration. The google-services.json should be placed in the platform-specific directory (e.g., platforms/android/app/ for Cordova).
React Native Firebase: When using @react-native-firebase, the package name is defined in android/app/build.gradle. The library automatically reads from google-services.json, so ensure both files are in sync. If using Flipper or other dev tools, they may add applicationIdSuffix which requires separate Firebase app registration.
Monorepo Considerations: In monorepo setups where multiple apps share configuration, be careful not to accidentally use the wrong google-services.json file. Consider using environment-specific directories or build scripts to ensure the correct file is copied during build time.
Callable Functions: INTERNAL - Unhandled exception
How to fix "Callable Functions: INTERNAL - Unhandled exception" in Firebase
auth/invalid-hash-algorithm: Hash algorithm doesn't match supported options
How to fix "auth/invalid-hash-algorithm: Hash algorithm doesn't match supported options" in Firebase
Hosting: CORS configuration not set up properly
How to fix CORS configuration in Firebase Hosting
auth/reserved-claims: Custom claims use reserved OIDC claim names
How to fix "reserved claims" error when setting custom claims in Firebase
Callable Functions: UNAUTHENTICATED - Invalid credentials
How to fix "UNAUTHENTICATED - Invalid credentials" in Firebase Callable Functions