AhmadWabbi
AhmadWabbi

Reputation: 2197

Using FCM in Cordova

I am using FCM in a Cordova project. I created a project in FCM web console and generated the google-services.json file with my project's package name com.mycompany.myapp. I had to put this file in two places to succeed compilation: the android project root folder and the 'CordovaLib' sub-folder. The problem is that each of these two copies should specify a different package name: the copy of the project root folder should specify com.mycompany.myapp and the copy in CordovaLib should specify com.apache.cordova. Otherwise, the project will not compile.

When I modify the package name in the file google-services.json manually, the compilation succeeds, but no notification are delivered.

My question is: how can I generate a google-services.json file that works with Cordova? What package name should I specify in FCM console (com.mycompany.myapp or com.mycompany.myapp)? Can I generate a file that works for many packages?

[EDIT]

If you need to know, the Cordova project is generated from a Sencha ExtJS6 application, and I use "cordova-plugin-fcm". In platforms\android folder, there is a folder called CordovaLib that contains build.gradle and src. The project will not compile if I don't copy google-services.json to this location. the classes in this src folder have the package name com.apache.cordova.

Upvotes: 0

Views: 1749

Answers (1)

AhmadWabbi
AhmadWabbi

Reputation: 2197

As the comments on the question suggested, the solution was to put google-services.json in the root of the project only. I will explain why I had this problem, so that others may not commit the same error.

When you create your project and application in Firebase Console, you get a window with the following instructions:

Add Firebase to your Android app

The Google services plugin for Gradle loads the google-services.json file you just downloaded. Modify your build.gradle files to use the plugin.

1 - Project-level build.gradle (<project\>/build.gradle):

buildscript {

dependencies {

// Add this line

classpath 'com.google.gms:google-services:3.0.0'

}

}

2 - App-level build.gradle (<project>/<app-module>/build.gradle):

...

// Add to the bottom of the file

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

So, I added the apply plugin ... line in the build.gradle file in the sub-folder CordovaLib (which is wrong, it should go in the same build.gradle file in the project root folder), and it would not compile unles I add google-services.json in that folder. So, the source of the error was following the above instructions without thinking.

Conclusion: In Android applications generated by Cordova, put both modifications in the build.gradle file at the root of the project, and put google-services.json in the root only.

Upvotes: 3

Related Questions