How to integrate firebase in an Ionic project (android/iOS)? - firebase

I am reading articles and testing ways to implement analytics and push notifications in my Ionic app using firebase.
My ionic app targets both android and ios platforms, so where do I place the google-services.json firebase file? Do I have to create an 2 apps within firebase if I need to use firebase for android and iOS?
I am reading many articles some of which say root folder, whereas some mention to put it in the apps folder. Some say we have to then update the config.xml to include the foll:
<resource-file src="google-services.json" target="app\google-services.json" />
I have also read that we need to update the gradle file with application and project level dependencies.
I have put the file in platform/android/app folder and things are working correctly now.
I just want to know what is the best practice when using Ionic and Firebase, where do I place the file and what config changes do I have to make?:
For an Ionic android app
For Ionic iOS app
For Ionic app for both android and iOS platforms

Have a look at the documentation of the firebase plugin :
https://github.com/arnesson/cordova-plugin-firebase
For Android, you have the google-services.json file, and for iOS you have the GoogleService-Info.plist file. These files are required by firebase to work on both iOS and Android. You have to place theses files at the same level as config.xml.
This plugin uses a hook (after prepare) that copies the configuration
files to the right place, namely platforms/ios/\/Resources for ios and platforms/android for android.
So you don't need the resource-file directive in your config.xml, since it is copied automatically.
You don't need to edit the build.gradle file, but If you have other google play plugins installed (like maps or admob), the compilation may not work.
Your build may fail if you are installing multiple plugins that use
Google Play Services. This is caused by the plugins installing
different versions of the Google Play Services library. This can be
resolved by installing cordova-android-play-services-gradle-release.
If your build is still failing, you can try installing
cordova-android-firebase-gradle-release. For more info, read the
following comment about locking down the specific versions for play
services and firebase. It is suggested to use + instead of 15.+ to
ensure the correct versions are used.
EDIT : Since you're using cordova-plugin-firebase-analytics and not cordova-plugin-firebase, it seems that they didn't implement any hook to copy the files. You'll have to place these directives in your config.xml :
<platform name="android">
<resource-file src="google-services.json" target="app\google-services.json" />
...
<platform name="ios">
<resource-file src="GoogleService-Info.plist" />
...
Be sure to install cordova-support-google-services

Related

Crashlytics could not find the resource file generated by Google Services

Crashlytics could not find the resource file generated by Google Services.You may need to execute the :processGoogleServices Task
i read the solution from this qusetion Crashlytics could not find the resource file generated by Google Services. You may need to execute the :process<Variant>GoogleServices Task
but i didn't succeded to run this command in my unity project
./gradlew :app:processProdReleaseGoogleServices or ./gradlew :app:processDevReleaseGoogleServices
how can i run it?
A tricky bit with the Unity SDK is that because Firebase still supports versions of Unity that predate a total move to gradle (I believe 2019.4 is when Unity switched over entirely, the earliest supported version right now is 2017.4), many Android specific solutions (such as the one you linked) won't function. If you did want to try it, you can select "Export Project" in your build settings -- but I don't think this will help:
It sounds instead like you're missing Plugins/Android/FirebaseApp.androidlib or Plugins/Android/FirebaseCrashlytics.androidlib. These are generated by the Firebase plugins (obviously FirebaseCrashlytics.androidlib is specific to Crashlytics -- you won't see that one otherwise) as part of a processing step to simulate what the Play Services gradle plugin would do in a typical Android application or game.
Some things to try:
Make sure you're on a supported version of Unity (2017.4 or newer -- Firebase does not officially support alphas or betas).
Ensure that you have a valid google-services.json file added and that Crashlytics has been added to your backend (full instructions here to redownload).
Re-add FirebaseCrashlytics and ensure that you leave everything checked.
Ensure that you have the latest Crashlytics plugin (currently 7.1.0). You can download just Crashlytics now from this site, but make sure you don't mix and match versions with other Firebase plugins.
And if none of these work, sometimes small changes in build environments expose unexpected issues. Your best bet will be to file an issue here with everything you've tried.

Firebase Crashlytics NDK unstripped files

I have an SDK that uses C++ code. The SDK exports 4 different modules in a maven repo that I publish on either a local maven repo or on GitHub packages.
The apps that consume the SDK should be able to use Firebase Crashlytics and get reports of crashes in the native code as well.
The first problem I have is that I need to provide to the Firebase plugin the folders unstrippedNativeLibsDir and strippedNativeLibsDir:
firebaseCrashlytics {
nativeSymbolUploadEnabled true
unstrippedNativeLibsDir "???"
strippedNativeLibsDir "???"
}
If I go to the project where I build the SDK I can see: build/intermediates/merged_native_libs/release and build/intermediates/stripped_native_libs.
Are these the unstripped and stripped folder respectively?
Is there any documentation about what exactly those folders are supposed to contain?
Anyway if I configure the app firebaseCrashlytics to point to these folders I still need to fix another issue.
I get:
Crashlytics could not find NDK build tasks on which to depend. You many need to manually enforce task dependencies
It looks like since I'm building the libs that use the NDK in another project, then the Firebase plugin does not work.
What am I supposed to do?
Is it possible to get the Crashlytics NDK working even if I build the libraries independently with a separate project?
The only way I made it work was to build the libraries along with the app. The libraries were in separate repos anyway but I included them as sub-repositories in the app.
In this way, you solve the issue of not finding NDK build tasks on which to depend.
I know this is not the answer though

Configuring cordova-plugin-firebase

I have installed cordova-plugin-firebase.
In the root folder of my app I have zipped www folder, config.xml, google-services.json and GoogleService-Info.plist.
I use this file in phonegap build and it builds fine for ios.
I have installed the app in a iPhone 7, but when I run the app it crashes.
In the plugin page I can read (https://www.npmjs.com/package/cordova-plugin-firebase#important-notes):
"IMPORTANT NOTES
This plugin uses a hook (after prepare) that copies the configuration
files to the right place, namely platforms/ios/\/Resources for ios and platforms/android for android.
Firebase SDK requires the configuration files to be present and valid,
otherwise your app will crash on boot or Firebase features won't work.
PhoneGap Build
Hooks does not work with PhoneGap Build. This means you will have to
manually make sure the configuration files are included. One way to do
that is to make a private fork of this plugin and replace the
placeholder config files (see src/ios and src/android) with your
actual ones, as well as hard coding your app id and api key in
plugin.xml."
So I think what is happening is that GoogleService-Info.plist and google-services.json are not placed in the right place.
But, how can I fix this? I don't understand the solution about making a private fork?!?
Any help?
Thanks
I answer myself.
Because of hooks doesn't work in phonegap build I have installed Xcode and Android Studio.
Now the plugin is working in android, but not ios.
I will post a question for this.

Firebase Push notifications with PhoneGap Build using cordova-plugin-fcm

I need to implement push notifications in a PhoneGap Build app that I'm developing.
I didn't find much documentation on using the new Google service, Firebase Cloud Messaging (FCM), but since it is supposed to be what we have to use now I searched for an API that could make my life easier.
I found: https://www.npmjs.com/package/cordova-plugin-firebase
So I added to my config.xml:
<plugin name="cordova-plugin-fcm" source="npm" />
And put the google-services.json I've created from https://console.firebase.google.com/ in the root of the folder I zip to upload to the Phonegap Build.
But I get the following error:
Execution failed for task ':processReleaseGoogleServices'.
> File google-services.json is missing. The Google Services Plugin cannot function without it.
Searched Location:
/project/src/release/google-services.json
/project/google-services.json
I created the "project" folder in the root and put the file there but that doesn't work neither.
Currently my app folder looks like:
\css
\img
\js
\res (icons and splashscreens)
index.html
config.xml
google-services.json
If anyone knows where I've to put that file or have any other way to implement this even using other API, I would really appreciate it.
I've never used Phonegap CLI, I've always been using Phonegap Build.
Cordova introduced the resource-file tag some time ago and it also works in Phonegap Build.
You can use that instead of a forked plugin to copy the google-services.json and GoogleService-Info.plist files
Put the in the root of your project and the use the resource-file tag in the config.xml like this:
If using cordova-android 7 or newer:
<platform name="android">
<resource-file src="google-services.json" target="app/google-services.json" />
</platform>
older versions
<platform name="android">
<resource-file src="google-services.json" target="google-services.json" />
</platform>
iOS
<platform name="ios">
<resource-file src="GoogleService-Info.plist" />
</platform>
You can also put it inside www folder, in that case in my examples add www/ before the file name in the src field.
https://cordova.apache.org/docs/en/7.x/config_ref/index.html#resource-file
The files need to be alongside the www folder, problem is that Phonegap Build only lets you upload the contents of said folder, so you can't upload the files.
Fortunately there's something you can do; Build installs the plugins directly from npm, and plugins can work thought the whole project, so what you need is a plugin that copies the files [google-services.json, GoogleService-Info.plist] for you.
Most of the work has already been done: cordova-plugin-fcm-config.
This plugin copies the required FCM configurations in the project root folders and Xcode project. It is used in combination with the great cordova-plugin-fcm plugin.
It wasn't built for this purpose but works wonderfully.
There's an added complexity though; since you can't upload plugins (you can only add a reference to it in your config.xml), and this plugin needs your own app configuration files, you'll have to:
Clone/Fork the plugin into your own repository.
Replace the configuration files.
Upload it somewhere Build can find.
Where do you upload it? That might be tricky. The infrastructure is built around the idea that plugins are for general purposes and can be configured in each project xml, so npm makes sense. But in this case your plugin will contain very specific data to your project, so uploading to npm would pollute the namespace in my opinion.
I don't know about you, but I have a paid account, so I publish the plugin in my own repository and submit as a private plugin. This is what I recommend.
For me I had to go into the .xcodeproj file and change Main Interface from MainViewController.xib to CDVLaunchScreen.storyboard
Rebuild and it worked. My app was built with Ionic and I started it in Ionic Creator.

How do I get Android Studio to include resources in a .jar file?

I'm using Unity 5. It still expects java plugins to be a .jar file built with JDK 1.6.
I had this working under Unity 4, but they changed something. Now I get this error while building the apk from Unity:
OBSOLETE - Providing Android resources in Assets/Plugins/Android/res
is deprecated, please move your resources to an Android Library. See
"Building Plugins for Android" section of the Manual.
How do I get AS to put the resources into classes.jar? The "Building Plugins for Android" doesn't mention resources.
Additional information about my project:
The project references Google Play Services, so I need version.xml in there
<integer name="google_play_services_version">6587000</integer>
Which contains the version number for the reference in the Manifest.
<meta-data android:name="com.google.android.gms.version" android:value="#integer/google_play_services_version" />
But if I include this file in res/values in my AS library project, it's still output as an xml file in res/values next to the classes.jar.
Incidentally, I'm digging the .jar file out of
/ApplicationName/build/intermediates/exploded-aar/ProjectName/LibararyName/unspecified/classes.jar
after building the project.
First of all, this is a warning and not an error.
Providing resources in Unity in Assets/Plugins/Android/res/ folder is not recommended, because it can cause resource conflicts.
When you provide resources in an Android library, they are correctly merged during build process.
Basically, you have three options:
convert your plugin into an Android library. Or you can init an empty library, add your .jar file to bin/, your resources to res/ (there should be a manifest and android.library=true in project.properties). You can do this by calling android create lib-project (more details)
if you compile your plugin directly using gradle or in Android Studio, you can use .aar file produced, given that you have set your project up as a library (apply plugin: 'com.android.library' in your build.gradle file). Just put it in any folder of your project, it should automatically be detected as Android-only plugin.
continue providing resources in res/ and wait for it to be completely unsupported by Unity.
Hope this helps.

Resources