codelab-friendlychat-android icon indicating copy to clipboard operation
codelab-friendlychat-android copied to clipboard

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

Open adamk90 opened this issue 4 years ago • 5 comments

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

adamk90 avatar Jan 10 '21 22:01 adamk90

@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!

samtstern avatar Jan 11 '21 16:01 samtstern

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

jjmrive avatar Jan 15 '21 22:01 jjmrive

@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 avatar Jan 18 '21 17:01 samtstern

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);
  }

samtstern avatar Jan 18 '21 17:01 samtstern

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

vicbNI avatar Nov 05 '23 09:11 vicbNI