File tree Expand file tree Collapse file tree 3 files changed +27
-2
lines changed
androidTest/java/com/google/firebase/database
main/java/com/google/firebase/database Expand file tree Collapse file tree 3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 11# Unreleased
2-
2+ - [ changed] The SDK can now infer a default database URL if none is provided in
3+ the config.
34- [ changed] Added internal HTTP header to the WebChannel connection.
45- [ feature] Realtime Database now supports connecting to a local emulator via
56 ` FirebaseDatabase#useEmulator() `
Original file line number Diff line number Diff line change @@ -119,6 +119,21 @@ public void getInstanceForAppWithHttpsUrl() {
119119 assertEquals ("https://tests.fblocal.com:9000" , db .getReference ().toString ());
120120 }
121121
122+ @ Test
123+ public void canInferDatabaseUrlFromProjectId () {
124+ FirebaseApp app =
125+ FirebaseApp .initializeApp (
126+ InstrumentationRegistry .getInstrumentation ().getTargetContext (),
127+ new FirebaseOptions .Builder ()
128+ .setApplicationId ("appid" )
129+ .setApiKey ("apikey" )
130+ .setProjectId ("abc123" )
131+ .build (),
132+ "canInferDatabaseUrlFromProjectId" );
133+ FirebaseDatabase db = FirebaseDatabase .getInstance (app );
134+ assertEquals ("https://abc123-default-rtdb.firebaseio.com" , db .getReference ().toString ());
135+ }
136+
122137 @ Test
123138 public void getDifferentInstanceForAppWithUrl () {
124139 FirebaseApp app =
Original file line number Diff line number Diff line change @@ -83,7 +83,16 @@ public static FirebaseDatabase getInstance(@NonNull String url) {
8383 */
8484 @ NonNull
8585 public static FirebaseDatabase getInstance (@ NonNull FirebaseApp app ) {
86- return getInstance (app , app .getOptions ().getDatabaseUrl ());
86+ String databaseUrl = app .getOptions ().getDatabaseUrl ();
87+ if (databaseUrl == null ) {
88+ if (app .getOptions ().getProjectId () == null ) {
89+ throw new DatabaseException (
90+ "Failed to get FirebaseDatabase instance: Can't determine Firebase Database URL. "
91+ + "Be sure to include a Project ID in your configuration." );
92+ }
93+ databaseUrl = "https://" + app .getOptions ().getProjectId () + "-default-rtdb.firebaseio.com" ;
94+ }
95+ return getInstance (app , databaseUrl );
8796 }
8897
8998 /**
You can’t perform that action at this time.
0 commit comments