remedy. remedy. - 26 days ago 12
JSON Question

Firebase Messaging Upstream for Android is not firing

I have a project with multiple

flavours
and multiple
buildType
.

I'll illustrate my project structure first before continuing:

app
- src
- main
- flavour1
- flavour2


Above is my current project structure. I'll now paste below my
build.gradle


productFlavors {
flavour1 {
applicationId "com.myproject.flavour1"
signingConfig signingConfigs.release

}
flavour2 {
applicationId "com.myproject.flavour2"
signingConfig signingConfigs.release
}
}

buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
applicationIdSuffix '.debug'
debuggable true
}
qa {
applicationIdSuffix '.qa'
debuggable true
}
}

dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.google.firebase:firebase-messaging:9.0.0'
compile 'com.google.android.gms:play-services-analytics:9.0.0'
}

apply plugin: 'com.google.gms.google-services'


Now what I want to accomplish is to have a specific
google-services.json
for each build variant so that I can use different Firebase Cloud Messaging projects per build variant.

Something like this:

app
- src
- main
- flavour1
- debug
- google-services.json <-- connected to Debug FCM Project
- qa
- google-services.json <-- connected to QA FCM Project
- flavour2
- debug
- google-services.json <-- connected to Debug FCM Project
- qa
- google-services.json <-- connected to QA FCM Project


I tried this method, and it's not working at all. None of the upstream messages are being sent to the server. I tried moving the
google-services.json
to the root and deleted all the other
json
files and then I found that messages were now being sent. I thought this was supported with
classpath 'com.google.gms:google-services:3.0.0'
, which was something I included in my
build.gradle
file.

If I'm missing a step or if I'm doing something incorrectly, please enlighten me.

Answer

After 72 hours of recreating multiple projects to mimic my file structure, I was able to narrow it down to this problem and I hope this may be of use to someone who experiences this:

FirebaseApp.initializeApp(this, FirebaseOptions.fromResource(this));

I was placing the above line of code into my Application class because my project was always requiring me to do so. Most of the sample apps didn't require me to have to manually intialize FirebaseApp since it's supposed to be done automatically but for some weird reason, my project always required me to do this.

After doing some digging, I noticed a Log in my console stating:

D/FirebaseApp: com.google.firebase.auth.FirebaseAuth is not linked. Skipping initialization.

This was my 'AHA!' moment. Then after a little bit more digging, I found that the problem was chillinlike a villain inside my AndroidManifest.xml.

I had the following tag:

<application
    android:name=".Application"
    android:allowBackup="false"
    tools:node="replace"
    android:icon="@mipmap/ic_launcher_my_app"
    android:label="@string/app_name"
    android:theme="@style/AppTheme"> ...

If you notice, I have a tag tools:node="replace". Turns out that with this line, it will NOT intialize my FirebaseApp. Using this actually replaces the declaration of lower priority.

I ended up replacing this line and voila! FirebaseApp was linking successfully and upstream messages began to work!

I also didn't create a google-services.json file for each flavour build type. I instead created a folder for each buildType and stored my google-services.json in there.

Looks like this:

app
- src
   - debug
      - google-services.json <-- connected to Debug FCM Project
   - qa
      - google-services.json <-- connected to QA FCM Project
   - release
      - google-services.json <-- connected to Release FCM Project
   - flavour1
   - flavour2

Each of the google-services.json configuration files within each buildType package has all the productFlavours inside so whenever I run a specific build variant, it loads up the proper configuration file.