Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Data retrieval code won't work if the *beta* Europe server is chosen for Realtime DB #137

Open
adamk90 opened this issue Jan 10, 2021 · 5 comments
Labels

Comments

@adamk90
Copy link

adamk90 commented Jan 10, 2021

Hi,

The provided code won't work if european (Belgium) server is chosen for realtime db, because it does not sits on the default url. I don't know how to solve this properly, but a workaround is to modify the Firebase DB reference initiation like this:

mFirebaseDatabaseReference = FirebaseDatabase.getInstance("url-to-europe-db").getReference();

Maybe this should be stated in codelabs, because gave a few hours to debug :)

thanks,
adam

@samtstern
Copy link
Contributor

@adamk90 can you explain a little more about your situation? Did you create a brand new project and select the European region when making your first Realtime Database instance? Or did you add a europe instance to a new project.

Sharing the contents of your google-services.json file would be useful!

@jjmrive
Copy link

jjmrive commented Jan 15, 2021

Hi,

This issue also happened to me. I created a new Firebase project and downloaded the google-services.json file following the initial instructions. After adding it to the project I created a European Realtime Database but it was impossible to read/write data from the DB.

Debugging the app I noticed the instance was pointing to the US location. Re-downloading the google-services.json file fixed the issue. The new version of the file contains a key-value "firebase_url" with the specific URL of the European server.

Hope this can help in some way.

Thanks,
JJ

@samtstern
Copy link
Contributor

@jjmrive thank you for sharing! It looks like what we need to do is change the codelab instructions to have you create a Realtime Database instance before you download the google-services.json file. Otherwise the JSON file won't contain your database URL and the SDK will make the wrong assumption.

For my own future reference here's what my google-services.json looked like when my project was brand new:

{
  "project_info": {
    "project_number": "54947790386",
    "project_id": "overground-test",
    "storage_bucket": "overground-test.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:54947790386:android:9a54efb6c1d417cb8e062b",
        "android_client_info": {
          "package_name": "com.google.foo"
        }
      },
      "oauth_client": [
        {
          "client_id": "54947790386-tagmgkg24kah1krulstn75p18ndophdu.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyDL_JX6a367eCohpKasBaTV9HOQmqb4Rlw"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "54947790386-tagmgkg24kah1krulstn75p18ndophdu.apps.googleusercontent.com",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

And here's what it looked like after I created RTDB in Europe:

{
  "project_info": {
    "project_number": "54947790386",
    "firebase_url": "https://overground-test-default-rtdb.europe-west1.firebasedatabase.app",
    "project_id": "overground-test",
    "storage_bucket": "overground-test.appspot.com"
  },
  "client": [
    {
      "client_info": {
        "mobilesdk_app_id": "1:54947790386:android:9a54efb6c1d417cb8e062b",
        "android_client_info": {
          "package_name": "com.google.foo"
        }
      },
      "oauth_client": [
        {
          "client_id": "54947790386-tagmgkg24kah1krulstn75p18ndophdu.apps.googleusercontent.com",
          "client_type": 3
        }
      ],
      "api_key": [
        {
          "current_key": "AIzaSyDL_JX6a367eCohpKasBaTV9HOQmqb4Rlw"
        }
      ],
      "services": {
        "appinvite_service": {
          "other_platform_oauth_client": [
            {
              "client_id": "54947790386-tagmgkg24kah1krulstn75p18ndophdu.apps.googleusercontent.com",
              "client_type": 3
            }
          ]
        }
      }
    }
  ],
  "configuration_version": "1"
}

@samtstern samtstern added bug and removed needs-info labels Jan 18, 2021
@samtstern
Copy link
Contributor

Here's the part of the SDK where it assumes it can use the Project ID:
https://cs.opensource.google/firebase-sdk/firebase-android-sdk/+/master:firebase-database/src/main/java/com/google/firebase/database/FirebaseDatabase.java;l=85;drc=a9027802801a903a47d3536ab375a196a7e8e900

  public static FirebaseDatabase getInstance(@NonNull FirebaseApp app) {
    String databaseUrl = app.getOptions().getDatabaseUrl();
    if (databaseUrl == null) {
      if (app.getOptions().getProjectId() == null) {
        throw new DatabaseException(
            "Failed to get FirebaseDatabase instance: Can't determine Firebase Database URL. "
                + "Be sure to include a Project ID in your configuration.");
      }
      databaseUrl = "https://" + app.getOptions().getProjectId() + "-default-rtdb.firebaseio.com";
    }
    return getInstance(app, databaseUrl);
  }

@vicbNI
Copy link

vicbNI commented Nov 5, 2023

Hi all I spent 4 hours on this problem.
If in your main activity if you have the app set up you MUST LOGIN A USER, before run and after run or the android studio to firebase will stay null
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants