0

In Firebase Databases, it has a option of Rules, by which we can restrict database, and I have made it public.

So my question is, how can i allow some other app, which is not using google-services.json of my project, to read from this DB.

The following code can read the data from the same firebase project.

FirebaseFirestore.getInstance().collection("tahatest").
  addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

        if(e != null){
            Log.d("Firestore", "Exception");
            return;
        }

        if(queryDocumentSnapshots!=null){
            List<DocumentSnapshot> snapshotList = queryDocumentSnapshots.getDocuments();

        }else{

            Log.d("Firestore", "null in onEvent Change");
        }
    }
});

So in order to read the data from some other project, do i need to define some URL, in place of collection, or i understood it wrongly, and we cannot read the data from some other firebase project.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
dev90
  • 7,187
  • 15
  • 80
  • 153
  • You tagged your question with `firebase-realtime-database`, but the code you show is for Clloud Firestore. While both databases are part of Firebase, they are completely separate and don't share the same API or the same tag. Which Firebase database are you asking about? – Frank van Puffelen May 10 '20 at 14:20

1 Answers1

1

There are two ways to initialize the Firebase project that your app is connecting to:

  1. Through the google-services.json file that you can include in your application. If you do this, this becomes the default project that the Firebase SDKs access.
  2. By initializing a FirebaseApp instance in your code, and then retrieving the Firebase services from that app instance. An example of this:

    FirebaseOptions options = new FirebaseOptions.Builder()
            .setProjectId("my-firebase-project")
            .setApplicationId("1:27992087142:android:ce3b6448250083d1")
            .setApiKey("AIzaSyADUe90ULnQDuGShD9W23RDP0xmeDc6Mvw")
            // setDatabaseURL(...)
            // setStorageBucket(...)
            .build();
    
    // Initialize with secondary app
    FirebaseApp.initializeApp(this /* Context */, options, "secondary");
    
    // Retrieve secondary FirebaseApp
    FirebaseApp secondary = FirebaseApp.getInstance("secondary");
    
    FirebaseFirestore.getInstance(secondary).collection("tahatest").
      addSnapshotListener(...
    

See also:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807