Skip to content

Commit 331ec37

Browse files
authored
Check for mandatory Firebase Options. (#1140)
1 parent 4212044 commit 331ec37

File tree

1 file changed

+29
-13
lines changed

1 file changed

+29
-13
lines changed

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.installations;
1616

17+
import android.text.TextUtils;
1718
import androidx.annotation.GuardedBy;
1819
import androidx.annotation.NonNull;
1920
import androidx.annotation.Nullable;
@@ -107,12 +108,22 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
107108
}
108109

109110
/**
110-
* Perform pre-condition checks to make sure {@link FirebaseOptions#getApiKey()} and {@link
111-
* FirebaseOptions#getProjectId()} are not null.
111+
* Perform pre-condition checks to make sure {@link FirebaseOptions#getApiKey()}, {@link
112+
* FirebaseOptions#getApplicationId()} , and ({@link FirebaseOptions#getProjectId()} are not null
113+
* or empty.
112114
*/
113115
private void preConditionChecks() {
114-
Preconditions.checkNotNull(firebaseApp.getOptions().getApiKey());
115-
Preconditions.checkNotNull(firebaseApp.getOptions().getProjectId());
116+
Preconditions.checkNotEmpty(getApplicationId());
117+
Preconditions.checkNotEmpty(getProjectIdentifier());
118+
Preconditions.checkNotEmpty(getApiKey());
119+
}
120+
121+
/** Returns the Project Id or Project Number for the Firebase Project. */
122+
@Nullable
123+
String getProjectIdentifier() {
124+
return TextUtils.isEmpty(firebaseApp.getOptions().getProjectId())
125+
? firebaseApp.getOptions().getGcmSenderId()
126+
: firebaseApp.getOptions().getProjectId();
116127
}
117128

118129
/**
@@ -144,6 +155,12 @@ String getApplicationId() {
144155
return firebaseApp.getOptions().getApplicationId();
145156
}
146157

158+
/** API key used to identify your app to Google servers. */
159+
@Nullable
160+
String getApiKey() {
161+
return firebaseApp.getOptions().getApiKey();
162+
}
163+
147164
/** Returns the nick name of the {@link FirebaseApp} of this {@link FirebaseInstallations} */
148165
@VisibleForTesting
149166
String getName() {
@@ -193,7 +210,6 @@ public Task<InstallationTokenResult> getToken(@AuthTokenOption int authTokenOpti
193210
@NonNull
194211
@Override
195212
public Task<Void> delete() {
196-
preConditionChecks();
197213
return Tasks.call(executor, this::deleteFirebaseInstallationId);
198214
}
199215

@@ -374,9 +390,9 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn
374390

375391
InstallationResponse response =
376392
serviceClient.createFirebaseInstallation(
377-
/*apiKey= */ firebaseApp.getOptions().getApiKey(),
393+
/*apiKey= */ getApiKey(),
378394
/*fid= */ prefs.getFirebaseInstallationId(),
379-
/*projectID= */ firebaseApp.getOptions().getProjectId(),
395+
/*projectID= */ getProjectIdentifier(),
380396
/*appId= */ getApplicationId(),
381397
/* migration-header= */ iidToken);
382398

@@ -405,9 +421,9 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
405421
@NonNull PersistedInstallationEntry prefs) throws IOException {
406422
TokenResult tokenResult =
407423
serviceClient.generateAuthToken(
408-
/*apiKey= */ firebaseApp.getOptions().getApiKey(),
424+
/*apiKey= */ getApiKey(),
409425
/*fid= */ prefs.getFirebaseInstallationId(),
410-
/*projectID= */ firebaseApp.getOptions().getProjectId(),
426+
/*projectID= */ getProjectIdentifier(),
411427
/*refreshToken= */ prefs.getRefreshToken());
412428

413429
switch (tokenResult.getResponseCode()) {
@@ -437,10 +453,10 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
437453
// Call the FIS servers to delete this Firebase Installation Id.
438454
try {
439455
serviceClient.deleteFirebaseInstallation(
440-
firebaseApp.getOptions().getApiKey(),
441-
entry.getFirebaseInstallationId(),
442-
firebaseApp.getOptions().getProjectId(),
443-
entry.getRefreshToken());
456+
/*apiKey= */ getApiKey(),
457+
/*fid= */ entry.getFirebaseInstallationId(),
458+
/*projectID= */ getProjectIdentifier(),
459+
/*refreshToken= */ entry.getRefreshToken());
444460

445461
} catch (FirebaseException exception) {
446462
throw new FirebaseInstallationsException(

0 commit comments

Comments
 (0)