Firebase Database from a library - mixed with app's Firebase database - android

I'm building a library that is using Firebase database.
when i'm taking the exported .aar and importing it via another app - i get the following error:
FirebaseAuthException: This operation is not allowed
the original app didnt allow the firebase database option - so we get this message. problem is I don't need to use the app's Firebase database within the sdk - what I want is the lib's (aar) firebase database to modify.
maybe if there was a way to define two google-services.json files, one for the aar and one for the app - without them overriding themselves?
how can an autonomous firebase database within a library?

You basically need to have an instance of FirebaseApp created from your library credentials. See this blog, it might help.
Instead of creating and managing 2 different google.json files (I'm not sure whether it's possible or not but even if it is..), i think it's better to wrap your library's firebase database instance and expose a neat api for the consumer apps to interact with your library's database.

Related

Firebase Realtime Database connection killed: Different Region

I am trying to build a simple login user authentication Android application that uses Firebases Realtime Database but I am getting the error:
[Firebase Database connection was forcefully killed by the server. Will not attempt to reconnect. Reason: The database lives in a different region. Please change your database URL to https://vax-in-60807-default-rtdb.asia-southeast1.firebasedatabase.app]
I am currently using the Singapore(asia-southeast1) server since I live in the Philippines. Is this wrong? or Should I be using the US one? How do I change my Database URL?
It looks like the google-services.json file that you use doesn't contain the Realtime Database URL, probably because you downloaded it before the database was created. In such cases the SDK assumes that the database is in the US (the original region), and you get an error that there's a mismatch.
There are two possible solutions:
Download an updated google-services.json from the Firebase console, and add that to your Android app.
Specify the database URL in your code instead, like this: FirebaseDatabase.getInstance("https://vax-in-60807-default-rtdb.asia-southeast1.firebasedatabase.app")...
Both have the same result, so pick whichever one seems easiest to you.
It cat still not work after the accepted answer (as in my case). If so, try:
Clean Project
Rebuild Project
Invalidate Caches & Restarte
and run it again.
If you are a New Flutter User who followed the new firebase doc, run this command in the terminal. It will automatically update the firebase_options.dart file.
flutterfire configure
I faced the same problem and that's because when you set up the app, the google-service.json doesn't contain the firebase_url.
These problems faced you when configuring your set up manually, You can fix all that problems by using flutterfire, it will help you a lot and set up your app in a few steps.
see docs firebase
It looks like the google-services.json file that you use doesn't contain the Realtime Database URL, probably because you downloaded it before the database was created. In such cases, the SDK assumes that the database is in the US (the original region), and you get an error that there's a mismatch.
You can fix it either way.
After configuring the database service setup you have to download the new "GoogleService-Info.plist" file and replace the previous one.
or
You can manually set the key in your "GoogleService-Info.plist" file
<key>DATABASE_URL</key>
<string>https://<app-instance-default-rtdb>.asia-southeast1.firebasedatabase.app</string> '

Does we start writing cloud functions for firebase locally?

I'm going to use cloud functions for firebase to recursively delete documents and collections in Firestore that is triggered by HTTP request (I'm using HTTP Callable function specifically). I have found some useful information from the following links:
https://firebase.google.com/docs/functions/callable
https://firebase.google.com/docs/firestore/solutions/delete-collections
But I'm not sure about one thing: It looks like from the tutorial, the functions are written locally and then deployed to cloud server. So where do I keep all the functions and their dependencies? If I keep the local written function within my android project, then each time when I update the function locally I will have to publish a new version of the app, which is too much work. Is it ok to start the local written function in another folder other than the android project? Also, if I want to start writing the function directly at cloud functions at GCP (so that I can update the function in cloud server), where can I get the dependencies files such as package.json, package-lock.json and .eslintrc.json etc.? I didn't find useful tutorial to that. Can someone help me? Thanks!
To answer your questions, first you can keep your functions and dependencies anywhere as long as you have access and logged your account to the CLI with the right project configured in it. Each time you update your function locally then yes, you have to redeploy it.
If you want, you can consider setting up CI/CD with your Firebase Functions so you can automate deployments whenever you hit commit on master and staging branch of your repo.
Second, you can start the local written function anywhere, as long as it contains the right dependencies for your app. As Doug mentioned, it doesn't matter how your files are organized.
And third, the dependencies files such as package.json, package-lock.json, and .eslintrc.json is something that you need to provide on your own. I suggest that you read through their docs and see what works best for you.
If I keep the local written function within my android project, then each time when I update the function locally I will have to publish a new version of the app, which is too much work.
That's not true. It doesn't really matter where you project files are organized. You can still deploy the function whenever you want. It's completely separate from your app code.
Is it ok to start the local written function in another folder other than the android project?
If that's your preference, then do it. As I mentioned, it doesn't matter how your files are organized.
Also, if I want to start writing the function directly at cloud functions at GCP (so that I can update the function in cloud server), where can I get the dependencies files such as package.json, package-lock.json and .eslintrc.json etc.?
If you're having problems working with functions in the console, you should ask a separate question about that and indicate where you are stuck in that process.

How to configure Firestore end-points at run-time?

I have developed an Android APP based on Firestore for DB storing data.
Now I need that the user is able to configure on the APP at run-time the end-point of Firestore DB - these parameters are typically stored in google-services.json (firebase_url, project_id, storage_bucket)
How can these values be changed at run time? Is there a specific Android API to do this?
Thanks in advance for any suggestion/support on this matter :)
You will have to take control of the initialization of your app, rather than allow the default initialization. This is kind of complicated, and it involves calling FirebaseApp.initializeApp() correctly with the details of the project you're trying to access.
I've written a couple blogs about this:
https://firebase.googleblog.com/2016/12/how-does-firebase-initialize-on-android.html
https://firebase.googleblog.com/2017/03/take-control-of-your-firebase-init-on.html

How to modify the Firebase database structure (for testing) without causing problems at the client's side

I have already published my app on Google Play, and I use the structure (shown in the figure) to store users' data on Firebase. I had also defined a "FirebaseUserData" class in my app, and at the client's side, each user downloads his/her data using:
FirebaseUserData firebaseUserData = dataSnapshot.getValue(FirebaseUserData.class);
The problem is that now I want to add a "score" node under each userUID and TEST my app -- but that way, the whole structure of the database would be changed, and users can no longer download their data since the database structure does not FIT the FirebaseUserData.class anymore.
Any way I can modify the firebase database structure without causing any problem at the clients' side?
Create separate build variant for testing via productFlavors.
In new build variant change applicationId
Add separate project in Firebase and move there existing database.

Versioning objects in firebase

I am using firebase as a database for a mobile application. Mobile application version 1 using a certain DB structure. But in version 2 I have a major schema changes. I could not find any specific documentation which would mention the best practices for managing DB upgrades. So I am thinking of following steps, which looks good on paper.
Application version 1 is in production using firebase/v1
Copy version 1 schema firebase/v1 to firebase/v2
Upgrade firebase/v2 schema
Disable write operations on firebase/v1
Distribute application v2 pointing to firebase/v2
With these steps users with older versions app would be able to only read the data. So unless they dont upgrade the app they wont be able to modify any data.
Do I going in the right direction in managing my schema updates? Or is there any better way to do this.
Use cloud function database functions to migrate data from db/v1 to db/v2. On update event in db/v1, you can write to db/v2 in parallel, so all active user data can move into db/v2.
https://firebase.google.com/docs/functions/database-events
Migrate data asap!

Categories

Resources