Check for mandatory Firebase Options.
package com.google.firebase.installations;
+import android.text.TextUtils;
import androidx.annotation.GuardedBy;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
@@ -107,12 +108,22 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
}
/**
- * Perform pre-condition checks to make sure {@link FirebaseOptions#getApiKey()} and {@link
- * FirebaseOptions#getProjectId()} are not null.
+ * Perform pre-condition checks to make sure {@link FirebaseOptions#getApiKey()}, {@link
+ * FirebaseOptions#getApplicationId()} , and ({@link FirebaseOptions#getProjectId()} are not null
+ * or empty.
*/
private void preConditionChecks() {
- Preconditions.checkNotNull(firebaseApp.getOptions().getApiKey());
- Preconditions.checkNotNull(firebaseApp.getOptions().getProjectId());
+ Preconditions.checkNotEmpty(getApplicationId());
+ Preconditions.checkNotEmpty(getProjectIdentifier());
+ Preconditions.checkNotEmpty(getApiKey());
+ }
+
+ /** Returns the Project Id or Project Number for the Firebase Project. */
+ @Nullable
+ String getProjectIdentifier() {
+ return TextUtils.isEmpty(firebaseApp.getOptions().getProjectId())
+ ? firebaseApp.getOptions().getGcmSenderId()
+ : firebaseApp.getOptions().getProjectId();
}
/**
@@ -144,6 +155,12 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
return firebaseApp.getOptions().getApplicationId();
}
+ /** API key used to identify your app to Google servers. */
+ @Nullable
+ String getApiKey() {
+ return firebaseApp.getOptions().getApiKey();
+ }
+
/** Returns the nick name of the {@link FirebaseApp} of this {@link FirebaseInstallations} */
@VisibleForTesting
String getName() {
@@ -193,7 +210,6 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
@NonNull
@Override
public Task<Void> delete() {
- preConditionChecks();
return Tasks.call(executor, this::deleteFirebaseInstallationId);
}
@@ -374,9 +390,9 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
InstallationResponse response =
serviceClient.createFirebaseInstallation(
- /*apiKey= */ firebaseApp.getOptions().getApiKey(),
+ /*apiKey= */ getApiKey(),
/*fid= */ prefs.getFirebaseInstallationId(),
- /*projectID= */ firebaseApp.getOptions().getProjectId(),
+ /*projectID= */ getProjectIdentifier(),
/*appId= */ getApplicationId(),
/* migration-header= */ iidToken);
@@ -405,9 +421,9 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
@NonNull PersistedInstallationEntry prefs) throws IOException {
TokenResult tokenResult =
serviceClient.generateAuthToken(
- /*apiKey= */ firebaseApp.getOptions().getApiKey(),
+ /*apiKey= */ getApiKey(),
/*fid= */ prefs.getFirebaseInstallationId(),
- /*projectID= */ firebaseApp.getOptions().getProjectId(),
+ /*projectID= */ getProjectIdentifier(),
/*refreshToken= */ prefs.getRefreshToken());
switch (tokenResult.getResponseCode()) {
@@ -437,10 +453,10 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
// Call the FIS servers to delete this Firebase Installation Id.
try {
serviceClient.deleteFirebaseInstallation(
- firebaseApp.getOptions().getApiKey(),
- entry.getFirebaseInstallationId(),
- firebaseApp.getOptions().getProjectId(),
- entry.getRefreshToken());
+ /*apiKey= */ getApiKey(),
+ /*fid= */ entry.getFirebaseInstallationId(),
+ /*projectID= */ getProjectIdentifier(),
+ /*refreshToken= */ entry.getRefreshToken());
} catch (FirebaseException exception) {
throw new FirebaseInstallationsException(
