Using FCM in Cordova - android

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.

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.

Related

Add play services dependency in Android AAR

I am trying to create android aar which uses play services. I have added playservices dependency in library gradle file. But when aar is exported in release mode runtime it is not working.
How to add google play services in aar?
Please help
You may want to check these steps in adding your library as a dependency:
Add the library to your project in either of the following ways (if you created the library module within the same project, then it's already there and you can skip this step):
Add the compiled AAR (or JAR) file (the library must be already built):
Click File > New > New Module.
Click Import .JAR/.AAR Package then click Next.
Enter the location of the compiled AAR or JAR file then click Finish.
Import the library module to your project (the library source becomes part of your project):
Click File > New > Import Module.
Enter the location of the library module directory then click Finish.
The library module is copied to your project, so you can actually edit the library code. If you want to maintain a single version of the library code, then this is probably not what you want and you should instead add the compiled AAR file as described above.
Make sure the library is listed at the top of your settings.gradle file, as shown here for a library named "my-library-module":
include ':app', ':my-library-module'
Open the app module's build.gradle file and add a new line to the dependencies block as shown in the following snippet:
dependencies {
compile project(":my-library-module")
}
Click Sync Project with Gradle Files.
But, if it's an app module that you want to reuse, you can turn it into a library module by following these steps to convert an app module to a library module:
Open the module-level build.gradle file.
Delete the line for the applicationId. Only an Android app module can define this.
At the top of the file, you should see the following:
apply plugin: 'com.android.application'
Change it to the following:
apply plugin: 'com.android.library'
Save the file and click Tools > Android > Sync Project with Gradle
Files.
For a more detailed information, see this documentation. Also. this related SO post can give additional insights.
As play-services now a days contains aar library instead of jar, we can not merge aar file with library i.e. another aar file. And, hence it is not possible to add play-services inside aar file. Best solution for this problem is who ever is using aar made by you, tell them to add play-services dependency in project.
Add a new module to your project and select import .JAR/.AAR Package.
After you added the module proceed like this : Project Structure → Select the app module → Dependencies tab → clieck the + button → select the module you added → Ok

How to manually import a gradle dependency in android studio

I'm trying to contribute to an open source project from Mapzen called "On the Road". (Github link)
I would like to edit and add some code to the project. At the moment I'm including the project using gradle with the following dependency:
compile 'com.mapzen:on-the-road:1.1.1'
Someone gave me a tutorial on how to do this here, but I'm getting a gradle error;
Error:Configuration with name 'default' not found.
What I did was clone the project from github, copied the entire master folder into my apps main and renamed it to "customLibrary".
I then added include':customLibrary' to my settings.gradle and compile project(':customLibrary') to my build.gradle(Module:app) file.
How do I import this project without the errors?
As you cloned the entire master folder, inside the customLibrary there is the build.gradle file which is the top-level file of the library.
You can't do it.
Since you are including the customLibrary folder, gradle is looking for a module build.gradle.
You have to clone the library folder instead of the entire master folder.
Otherwise you can use include':customLibrary:library' only to add the module library of the entire project.

Integrating Notificare Push Notification service manually in android studio project

I am writing this post in order to help other users get through the issues i faced while integrating Notificare notification service in android project in android studio. as I have to integrate Notificare, Segment.io, Branch.io, Google places Autocomplete, In app purchase and main problem faced to me is the clashing of multiple libraries and getting of duplicate entry errors. As Notificare it self has the inappbilling library code, google analytics, play services and its own notification code. I tried the traditional way of integrating the library as provided in the notificare android studio integration the compile command in gradle file way, but that didnt work as i used to get duplicate entry for the inappbilling code which was already present in the library.
So here is the procedure.
Download the sdk folder from the notificare sdk source with sample
and then rename folder from sdk to notificare and then create a gradle file in the notificare folder as it doesnt have the gradle file by following steps:
create build.gradle file manually in notificare project. and copy the following code in it:
apply plugin: 'com.android.library'
dependencies {
compile 'com.android.support:support-v4:21.0.3'
}
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 14
targetSdkVersion 21
}
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
res.srcDirs = ['res']
}
}
}
above code is taken from this link.
then in the libs folder the notificare-push-lib.jar file rename to extn .zip extract the zip so we can remove contents clashing with other libraries. then in the com folder delete the android folder which has vendor-billing folder which will clash with your in app purchase implementation.
once the folder is deleted select the contents of folder which will be like com, META-INF and re select all 3 folders and compress them to zip.
This step is very important for mac users:
as you create zip file the mac automatically creates __MACOSX folder which needs to be removed which can be done using following command in terminal:
zip -d notificare.zip __MACOSX/\*
this will remove the __MACOSX folder within the zip.
Now rename the zip file to jar file with following command:
mv notificare.zip notificare.jar
copy this file and replace it in your project notificare libs folder
then in your main project app/build.gradle file add following command:
compile project(":notificare")
compile 'com.google.android.gms:play-services:8.4.0'
and also add following in main project settings.gradle file :
include ':app', ':notificare'
project(':notificare').projectDir = new File('notificare')
Clean project.
Sync gradle.
build the project it should run and the notificare should register your device.

Error:Cannot change configuration ':android:compile' after it has been resolved

I tried to add google-play-services.jar to my project but I faced with this err.Why? (Error:Cannot change configuration ':android:compile' after it has been resolved.)
Does anybody know a way to resolve this problem?
In android studio you don't need to add jar files, Remove the jar file and
Open the build.gradle file inside your application module directory.
Note: Android Studio projects contain a top-level build.gradle file and a build.gradle file for each module. Be sure to edit the file for your application module
Add a new build rule under dependencies for the latest version of play-services. For example:
dependencies {
compile 'com.google.android.gms:play-services:7.8.0'
}
Save the changes and click Sync Project with Gradle Files in the toolbar.
Here's official link

How to add google-services.json in Android?

The error is:
File google-services.json is missing from module root folder. The
Google Quickstart Plugin cannot function without it.
Above asked question has been solved as according to documentation at developer.google.com https://developers.google.com/cloud-messaging/android/client#get-config
2018 Edit : GCM Deprecated, use FCM
The file google-services.json should be pasted in the app/ directory.
After this is when I sync the project with gradle file the unexpected Top level exception error comes. This is occurring because:
Project-Level Gradle File having
dependencies {
classpath 'com.android.tools.build:gradle:1.0.0'
classpath 'com.google.gms:google-services:1.3.0-beta1'
}
and App-Level Gradle File having:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.1.1'
compile 'com.google.android.gms:play-services:7.5.0' // commenting this lineworks for me
}
The top line is creating a conflict between this and classpath 'com.google.gms:google-services:1.3.0-beta1' So I make comment it now it works Fine and no error of
File google-services.json is missing from module root folder. The Google Quickstart Plugin cannot function without it.
The document says:
Copy the file into the app/ folder of your Android Studio project, or
into the app/src/{build_type} folder if you are using multiple build
types.
It should be on Project -> app folder
Please find the screenshot from Firebase website
Instead of putting in root folder as given in docs of firebase, just copy the google-json file in the projectname/app 's root folder and it works fine then .
Its just simple !
WINDOWS
Open Terminal window in Android Studio (Alt+F12 or View->Tool Windows->Terminal).
Then type
"move file_path/google-services.json app/"
without double quotes.
eg
move C:\Users\siva\Downloads\google-services.json app/
LINUX
Open Android Studio Terminal and type this
scp file_path/google-services.json app/
eg:
scp '/home/developer/Desktop/google-services.json' 'app/'
Click right above the app i.e android(drop down list) in android studio.Select the Project from drop down and paste the json file by right click over the app package and then sync it....
This error indicates your package_name in your google-services.json might be wrong. I personally had this issue when I used
buildTypes {
...
debug {
applicationIdSuffix '.debug'
}
}
in my build.gradle. So, when I wanted to debug, the name of the application was ("all of a sudden") app.something.debug instead of app.something. I was able to run the debug when I changed the said package_name...
Download the "google-service.json" file from Firebase
Go to this address in windows explorer "C:\Users\Your-Username\AndroidStudioProjects" You will see a list of your Android Studio projects
Open a desired project, navigate to "app" folder and paste the .json file
Go to Android Studio and click on "Sync with file system", located in dropdown menu (File>Sync with file system)
Now sync with Gradle and everything should be fine
google-services.json file work like API keys means it store your project_id and api key with json format for all google services(Which enable by you at google console) so no need manage all at different places.
Important process when uses google-services.json
at application gradle you should add
apply plugin: 'com.google.gms.google-services'.
at top level gradle you should add below dependency
dependencies {
// Add this line
classpath 'com.google.gms:google-services:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}

Categories

Resources