File tree 3 files changed +27
-2
lines changed
androidTest/java/com/google/firebase/database
main/java/com/google/firebase/database
3 files changed +27
-2
lines changed Original file line number Diff line number Diff line change 1
1
# Unreleased
2
-
2
+ - [ changed] The SDK can now infer a default database URL if none is provided in
3
+ the config.
3
4
- [ changed] Added internal HTTP header to the WebChannel connection.
4
5
- [ feature] Realtime Database now supports connecting to a local emulator via
5
6
` FirebaseDatabase#useEmulator() `
Original file line number Diff line number Diff line change @@ -119,6 +119,21 @@ public void getInstanceForAppWithHttpsUrl() {
119
119
assertEquals ("https://tests.fblocal.com:9000" , db .getReference ().toString ());
120
120
}
121
121
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
+
122
137
@ Test
123
138
public void getDifferentInstanceForAppWithUrl () {
124
139
FirebaseApp app =
Original file line number Diff line number Diff line change @@ -83,7 +83,16 @@ public static FirebaseDatabase getInstance(@NonNull String url) {
83
83
*/
84
84
@ NonNull
85
85
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 );
87
96
}
88
97
89
98
/**
You can’t perform that action at this time.
0 commit comments