0

So here is my situation, I'm connected to ApplicationA project on firebase console to handle the notification. But I want to listen to a database from ApplicationB project also.

The thing is that I can only have one google-services.json, I have to choose between one or the other but not both. So, my question is, how can I specify to listen to a database from another app that the one from the google-services.json.

Since the update, the project link is handled internally by the json. Before the update, I was able to paste the entire link of the database I wanted to be listening to.

Here is the way I'm listening to certain database path currently:

    FirebaseDatabase database = FirebaseDatabase.getInstance();
    fireActionUpdate = database.getReference("org/" + nameId + "/action");
    fireRuleResponse = database.getReference("org/" + nameId + "/notify");

Before it was:

    fireActionUpdate = new Firebase("https://yellow.firebaseio.com/org/" + nameId + "/action");
    fireRuleResponse = new Firebase("https://yellow.firebaseio.com/org/" + nameId + "/notify");
Jaythaking
  • 2,200
  • 4
  • 25
  • 67
  • 1
    You can still perform the same, I recently did use one DB for both app for messaging and GCM implementation – silverFoxA Oct 11 '16 at 22:18
  • @silverFox - Seems like an answer. Jaythaking - Have you tried it yet? – AL. Oct 12 '16 at 03:16
  • 2
    @silverFox's answer has the gist of it. Have a look at my answer here: http://stackoverflow.com/questions/37634767/how-to-connect-to-more-than-one-firebase-database-from-an-android-app/37643374#37643374 – Frank van Puffelen Oct 12 '16 at 07:16
  • @FrankvanPuffelen Your answer doesn't work -_-, first comment is that is crashed, I just don't understand why is got accepted – Jaythaking Oct 12 '16 at 20:50
  • I ran with that code before I posted it. It works. If you have a crash about `[DEFAULT]` already existing, it is either because you didn't specify a name for the second app or because you also have a different problem. – Frank van Puffelen Oct 13 '16 at 08:29
  • @FrankvanPuffelen I'm not even creating two app, if you read my question carefully, it also isn't a duplicate. I want to use both Notification and database from two different app. Not two database from 2 different app... It might be the same answer for both question but it's starting from two different issue – Jaythaking Oct 13 '16 at 15:06
  • 1
    Re-opened. The problem is that you have two processes calling `FirebaseDatabase.getInstance()` and they're getting into each other's way. [Searching for the error message](https://www.google.com/webhp#q=FirebaseApp+name+%5BDEFAULT%5D+already+exists) leads to this answer: http://stackoverflow.com/questions/37337080/initialize-firebase-references-in-two-separate-files-in-the-new-api – Frank van Puffelen Oct 13 '16 at 15:15
  • @FrankvanPuffelen That's really weird though because it's there in my code only ONCE in the ``onCreate`` of a service... There only one process for each service in Android so I'm lost here... – Jaythaking Oct 13 '16 at 15:20
  • The only workaround I've found so far is to check if it already exist in a try catch... which is kind of ugly – Jaythaking Oct 13 '16 at 15:21
  • @FrankvanPuffelen If you are willing, we could check this in the chat and come back with an answer later? – Jaythaking Oct 13 '16 at 15:28
  • The problem is always the same: there are two processes (or two places in a single process) initializing the FirebaseApp and they lead to conflicts. The cause is sometimes in your own code, but more often then not there is a library used that introduces a second process. Common examples of such libraries are crash reporting tools such as (yes ironically) Firebase's own Crash Reporting (we're working on a fix for that one btw). – Frank van Puffelen Oct 13 '16 at 15:33
  • There you go, that last sentence should be the accepted answer to my question... I was getting the error by that Crash Reporting tool and I though I was doing something wrong. I'll edit my question as it wasn't really clear – Jaythaking Oct 13 '16 at 15:40

1 Answers1

2

This solution is if you want to bind two application to the same Firebase Database, yet wants to use all the features like FCM.

Please follow these steps

  1. Download google-services.json for the both the projects from the firebase console.
  2. Let's say Application A is connected to Main Database and the json file is file1, similarly for Application B, the DB is Sub Database and File 2.
  3. Go to the File1 and copy the header except project_number& project_id. Basically keep theFirebase_url&&storage_bucket` same for both the apps.

  4. Also keep the client id and api key same for both the projects.

Now you are all set.

To connect to a Firebase database like before follow the below code

        DatabaseReference baseRef = FirebaseDatabase.getInstance().getReferenceFromUrl("https://<APP_NAME>.firebaseio.com/");

Perform the rest of the operations as usual.

Hopefully they don't push another update to break this rules :D

NOTE: These changes are tested, I'm using it for my 2 separate apps for messaging purpose with FCM integrated

For few known issues which I ended up with, you may check this code I have posted as well Android not receiving Firebase Push Notification - MismatchSenderId

Happy Coding!!

Community
  • 1
  • 1
silverFoxA
  • 4,549
  • 7
  • 33
  • 73