You are my only hope solving this problem. I have 1 firebase project, and two android applications, one is for the users, and one is for the coaches. I store data in firebase firestore, and my plan is the coaches can upload trainings to firestore and that list appears on the users app, the users then can sign up for training etc etc...but with the second application connected to the firebase project, this error pops up in both applications:
java.lang.IllegalStateException: Default FirebaseApp is not initialized in this process [package name]. Make sure to call FirebaseApp.initializeApp(Context) first.
I have found some infos on internet that says I must use FirebaseOptions.Builder() to initialise one-one separate FirebaseApp instance in each android app, but I tried it and didnt work. This was the code for it:
private fun firebaseInit() {
val options = FirebaseOptions.Builder()
.setProjectId("qrtrainertruck-adminapp")
.setApplicationId("1:221722591872:android:b3964eb554e10b6251dc65")
.setApiKey("AIzaSyCfjNCD5kHb-LnB5FZXKPxi1t0lXJQASB4")
.build()
FirebaseApp.initializeApp(this, options, "adminapp")
}
Maybe just the api key is wrong, or I dont know...If this is the solution then where can I find these attributes (projectId, ApplicationId, ApiKey) for the function?
Thank you for all the help!
you can add multiple apps in one firebase project.
Related
I am trying to use 2 different Firebase projects with android, one for FCM and another for Crashlytics(say ProjectA for FCM and ProjectB for Crashlytics). But it's not working for me.
I am initialising Firebase manually.
As soon as my app starts I initialise Firebase for FCM using ProjectA configuration.
val options = FirebaseOptions.Builder()
.setProjectId(fcmOptions[5]!!)
.setApplicationId(fcmOptions[0]!!)
.setApiKey(fcmOptions[1]!!)
.setDatabaseUrl(fcmOptions[2])
.setGcmSenderId(fcmOptions[3])
.setStorageBucket(fcmOptions[4])
.build()
FirebaseApp.initializeApp(this, options)
Till here it is fine, the token gets generated and I am able to receive Push notifications.
After this on click of a button I register for Crashlytics using ProjectB configuration.
val options = FirebaseOptions.Builder()
.setProjectId(crashlyticsOptions[5]!!)
.setApplicationId(crashlyticsOptions[0]!!)
.setApiKey(crashlyticsOptions[1]!!)
.setDatabaseUrl(crashlyticsOptions[2])
.setStorageBucket(crashlyticsOptions[4])
.build()
FirebaseApp.initializeApp(this, options, "crashlytics")
After this step I cause a few crashes so that I can see the crashes in Crashlytics dashboard.
The problem here is that the crashes don't appear under ProjectB Crashlytics console which is what I expect. But the crashes appear under the ProjectA for FCM.
Has anyone tried such scenario before and can help me out.
Crashlytics does not support using a secondary project to capture crashes. It will only use the default project (the one you initialized without a name). FCM and Analtyics are the same way.
Problem: Firebase Management API method projects.addFirebase returns status code 403
Steps to recreate:
In an Android application I need to create separate Firebase Accounts for Realtime Database. For this purpose I authenticate users with GoogleSignIn.
Then I retrieve token with proper scope "oauth2:https://www.googleapis.com/auth/cloud-platform".
In next step I use GCP Resource Manager API to create a new GCP project, which will be a container for Firebase project.
The new project is created successfully on each of the test accounts.
When I try to call Firebase Management API method projects.addFirebase using the same token I got earlier (same scope) I get error "The caller does not have permission". This error occurs on 3 out of 4 accounts I have tried.
I was able to reproduce same error also using google-apis-explorer. Apis Explorer
I will be grateful for any hints on how to resolve this issue. As I mentioned earlier this error does not occur on one of the accounts I tested, while on the remaining three it happens every time.
Thank you.
Update
#Doug thank you for taking time to look at my problem. It seems that I have found the cause. After accessing https://console.firebase.google.com and clicking "Add a project" a following popup showed up (I can't paste it directly, because my reputation is too low):
Popup link
After I checked all consents and added first project I am also able to make api calls successfully.
It looks like I my question should be: Is there a way to grant required permissions through Resource Manager Api calls so I can add Firebase to existing Cloud Project?
Also for the reference I paste the method I use to call projects.addFirebase, it uses RxJava2 (Android) and Retrofit2.
#Headers({"Content-Type: application/json"})
#POST("projects/{project}:addFirebase")
Single<Operation> addFirebaseToProject(#Header("Authorization") String bearerToken,
#Path("project") String project,
#Body FirebaseDataModel dataModel
);
Thank you for your help.
Ps. sorry for my bad English, it's not my mother tongue.
We are starting to migrate from Google Analytics to Firebase Analytics as it is going to be deprecated in a year. We have a need of initializing the firebase project runtime in our iOS application and we are following the steps mentioned here: https://firebase.google.com/docs/projects/multiprojects?authuser=0
Why do we need to initialize the firebase manually and at runtime? The details are in this issue: Switching between different firebase projects (runtime) in one single APK file
I posted that issue when we encountered a similar problem for push notifications on Android because at that time the documentation was not sufficient. It is really good to know that firebase documentation has been updated to reflect how to manually initialize the sdk for various platforms.
Problem: The problem that we are facing today is that once we initialize the SDK manually for say a "secondary" application and we try to use the firebase analytics, it fails with following error:
2018-12-20 17:14:33.526757-0800 App Name[9218:675367] 5.2.0 - [Firebase/Analytics][I-ACS025018] Event not logged. Call +[FIRApp configure]: AppLaunch
Below is the sample piece of code:
FIROptions *firoptions = [[FIROptions alloc] initWithGoogleAppID:#"Actual_GoogleAppId" GCMSenderID:#"Actual_GCMSenderId"];
firoptions.bundleID = #"actual.bundle.id";
firoptions.APIKey = #"actual-api-key";
firoptions.clientID = #"actual.client.id";
firoptions.databaseURL = #"https://actual.url";
firoptions.storageBucket = #"actualapp.appspot.com";
[FIRApp configureWithName:#"testApp" options:firoptions];
if ([FIRApp appNamed:#"testApp"]) {
[FIRAnalytics logEventWithName:#"AppLaunch" parameters:nil];
}
Looking at the error it tells us to use [FIRApp configure] which configures the default application from the GoogleService-Info.plist in the project. But we do not have GoogleService-Info.plist in our project as suggested in https://firebase.google.com/docs/projects/multiprojects?authuser=0.
TL;DR: We are initializing the firebase app manually in our iOS application using the API [FIRApp configureWithName:#"testApp" options:firoptions]; and the Firebase Analytics gives an error asking us to use default [FIRApp configure] which defeats the purpose of manual initialization.
Any help is greatly appreciated.
Firebase Analytics requires the file to be named GoogleService-Info.plist. Note the first paragraph of https://firebase.google.com/docs/projects/multiprojects?authuser=0 recommends differentiating multiple versions of it by putting them in different directories.
More details about FirebaseAnalytics and multiple plist files at https://github.com/firebase/firebase-ios-sdk/issues/230
You can access the Firebase project from multiple Android apps, but, if my intent is to be able to access multiple projects within the same Android app?
For example, 3 projects hosted each on their own proprietary accounts and a common Android app that can access them all.
Thanks in advance for any answer!
You have to initialize multiple FirebaseApp instances yourself(this is done by google services plugin by reading google-services.json)
//This builder may call more functions depending on features being used
val secondProjectOptions = FirebaseOptions.Builder()
.setApplicationId("app id")
.setApiKey("api key")
.build()
FirebaseApp.initializeApp(this, secondProjectOptions, "secondProject")
//Use this app instance to access various features such as db
val secondProjectApp = FirebaseApp.getInstance("secondProject")
I've set up a small android and firebase app... Authentification works like a charm, and in the firebase console, I can see my user, logged in with the Google account.
Now I am trying to experiment a little with the Text to Speech api, and in doing so, I followed this tutorial:
https://github.com/GoogleCloudPlatform/java-docs-samples/tree/master/texttospeech/cloud-client
I managed to make the small java app work, by setting the GOOGLE_APPLICATION_CREDENTIALS Environment variable (I followed this tutorial for this step: https://cloud.google.com/docs/authentication/getting-started), but I am not sure what I need to do to make that code work in the Android app where the users are authentificated..
The Error that I get when trying to make a call to the TextToSpeech API is:
The Application Default Credentials are not available. They are
available if running in Google Compute Engine. Otherwise, the
environment variable GOOGLE_APPLICATION_CREDENTIALS must be defined
pointing to a file defining the credentials. See
https://developers.google.com/accounts/docs/application-default-credentials
for more information.
The error mentioned comes from the line:
TextToSpeechClient textToSpeechClient = TextToSpeechClient.create();
This error appears because of the fact that on the android emulator I don't have access to the credentials that are set as environment variable in my OS..So I have to provide the credentials in another way.
In the case of other Google APIs, like Storage, I found out that this can be done like this:
// You can specify a credential file by providing a path to GoogleCredentials.
// Otherwise credentials are read from the GOOGLE_APPLICATION_CREDENTIALS environment variable.
GoogleCredentials credentials = GoogleCredentials.fromStream(new FileInputStream(jsonPath))
.createScoped(Lists.newArrayList("https://www.googleapis.com/auth/cloud-platform"));
Storage storage = StorageOptions.newBuilder().setCredentials(credentials).build().getService();
I managed to create the GoogleCredentials object with the contents of the json file, however the TextToSpeech client doesn't seem to provide a functionality similar to this:
StorageOptions.newBuilder().setCredentials(credentials).build()
So my question is....is there a way to provide the Credentials object to the TextToSpeech client?
Thanks
Currently, there is not a way to provide credentials to the TTS Client from this page.
Due to Security / Auth reasons, I believe the best suggested approach is to use Firebase Functions.
Get the Text
Call Firebase Functions
Have Firebase Functions call the TTS API
Return the results.
This way, no keys are leaked inside the application and you can use Firebase Auth.
Let me know if that helps!
Update:
Option 2: iOS Tutorial (should be adaptable to Android)
Get the Text
Call Firebase Functions
Have Firebase Functions return an OAuth2 Token
Use the token directly with the API