I’m attempting to setup android push notifications within expo for my react native app and running into trouble. I have notifications successfully working for ios but when I run my build on my android phone I get the following error in the adb logs as well as in my react native side
W FirebaseApp: Default FirebaseApp failed to initialize because no default options were found.
This usually means that com.google.gms:google-services was not applied to your gradle project.
I FirebaseInitProvider: FirebaseApp initialization unsuccessful
I’m also getting this error returned from my react native code
ERROR Error getting Expo push token: [Error: Call to function 'ExpoPushTokenManager.getDevicePushTokenAsync' has been rejected.
→ Caused by: java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process com.reet. Make sure to call FirebaseApp.initializeApp(Context) first.]
React Native code
async function registerForPushNotificationsAsync() {
console.log('registerForPushNotificationsAsync called');
let token;
if (Platform.OS === 'android') {
console.log('Setting up Android notification channel');
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
if (Device.isDevice) {
console.log('Device is a physical device'); // Debugging log
const { status: existingStatus } = await Notifications.getPermissionsAsync();
console.log(`Existing notification permission status: ${existingStatus}`);
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
console.log(`Requested notification permission status: ${status}`);
finalStatus = status;
}
if (finalStatus !== 'granted') {
console.log('Push notification permissions not granted');
alert('Failed to get push token for push notification!');
return;
}
console.log(`Final Status: ${finalStatus}`)
try {
console.log(`Project ID: ${Constants.expoConfig.extra.eas.projectId}`)
token = await Notifications.getExpoPushTokenAsync({
projectId: Constants.expoConfig.extra.eas.projectId,
});
console.log(`Expo push token: ${token.data}`); // Debugging log
return token.data;
} catch (error) {
console.error('Error getting Expo push token:', error);
}
} else {
console.log('Running on an emulator, push notifications won’t work');
alert('Must use physical device for Push Notifications - will not work on emulators');
return;
}
}
Root-Level build.gradle (Project-Level):
plugins {
id 'com.android.application' version '8.1.0' apply false
id 'com.android.library' version '8.1.0' apply false
id 'org.jetbrains.kotlin.android' version '1.9.0' apply false
id 'com.google.gms.google-services' version '4.3.8' apply false
id 'com.facebook.react' version '0.73.6' apply false
}
ext {
buildToolsVersion = "34.0.0"
minSdkVersion = 21
compileSdkVersion = 34
targetSdkVersion = 34
ndkVersion = "23.1.7779620"
}
allprojects {
repositories {
google()
mavenCentral()
}
}
App-Level build.gradle:
plugins {
id 'com.android.application'
id 'com.facebook.react'
}
android {
ndkVersion rootProject.ext.ndkVersion
compileSdkVersion rootProject.ext.compileSdkVersion
namespace "com.reet"
defaultConfig {
applicationId "com.reet"
minSdkVersion rootProject.ext.minSdkVersion
targetSdkVersion rootProject.ext.targetSdkVersion
versionCode 23
versionName "1.2"
}
splits {
abi {
reset()
enable enableSeparateBuildPerCPUArchitecture
universalApk false
include(*reactNativeArchitectures())
}
}
signingConfigs {
release {
if (project.hasProperty('MYAPP_UPLOAD_STORE_FILE')) {
storeFile file(MYAPP_UPLOAD_STORE_FILE)
storePassword MYAPP_UPLOAD_STORE_PASSWORD
keyAlias MYAPP_UPLOAD_KEY_ALIAS
keyPassword MYAPP_UPLOAD_KEY_PASSWORD
}
}
debug {
storeFile file('debug.keystore')
storePassword 'android'
keyAlias 'androiddebugkey'
keyPassword 'android'
}
}
buildTypes {
debug {
signingConfig signingConfigs.debug
}
release {
signingConfig signingConfigs.release
minifyEnabled enableProguardInReleaseBuilds
proguardFiles getDefaultProguardFile("proguard-android-optimize.txt"), "proguard-rules.pro"
}
}
applicationVariants.all { variant ->
variant.outputs.each { output ->
def versionCodes = ["armeabi-v7a": 1, "x86": 2, "arm64-v8a": 3, "x86_64": 4]
def abi = output.getFilter(OutputFile.ABI)
if (abi != null) {
output.versionCodeOverride = defaultConfig.versionCode * 1000 + versionCodes.get(abi)
}
}
}
}
dependencies {
implementation("com.facebook.react:react-android")
implementation("androidx.swiperefreshlayout:swiperefreshlayout:1.0.0")
implementation('org.xerial:sqlite-jdbc:3.46.1.0')
implementation platform('com.google.firebase:firebase-bom:31.1.0')
implementation 'com.google.firebase:firebase-messaging'
debugImplementation("com.facebook.flipper:flipper:${FLIPPER_VERSION}")
debugImplementation("com.facebook.flipper:flipper-network-plugin:${FLIPPER_VERSION}") {
exclude group: 'com.squareup.okhttp3', module: 'okhttp'
}
debugImplementation("com.facebook.flipper:flipper-fresco-plugin:${FLIPPER_VERSION}")
hermesEnabled = (findProperty('expo.jsEngine') ?: "hermes") == "hermes"
if (hermesEnabled.toBoolean()) {
implementation("com.facebook.react:hermes-android")
} else {
implementation jscFlavor
}
}
apply from: file("../../node_modules/@react-native-community/cli-platform-android/native_modules.gradle")
apply plugin: 'com.google.gms.google-services'
I’ve been doing circles on this for a few days now and would greatly appreciate any help!
Things ive tried so far
- Changing com.google.gms.google-services version (most popular fix)
- Changing bom version (was originally 32.0.0)
- adding FirebaseApp.initializeApp(this) to MainApplication.kt (doesnt work if I have it or not)
- verifiying my google-services.json file was correct and in correct place
Drew Adorno is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.