|
14 | 14 |
|
15 | 15 | package com.google.firebase.installations;
|
16 | 16 |
|
| 17 | +import android.text.TextUtils; |
17 | 18 | import androidx.annotation.GuardedBy;
|
18 | 19 | import androidx.annotation.NonNull;
|
19 | 20 | import androidx.annotation.Nullable;
|
@@ -107,12 +108,22 @@ public class FirebaseInstallations implements FirebaseInstallationsApi {
|
107 | 108 | }
|
108 | 109 |
|
109 | 110 | /**
|
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. |
112 | 114 | */
|
113 | 115 | 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(); |
116 | 127 | }
|
117 | 128 |
|
118 | 129 | /**
|
@@ -144,6 +155,12 @@ String getApplicationId() {
|
144 | 155 | return firebaseApp.getOptions().getApplicationId();
|
145 | 156 | }
|
146 | 157 |
|
| 158 | + /** API key used to identify your app to Google servers. */ |
| 159 | + @Nullable |
| 160 | + String getApiKey() { |
| 161 | + return firebaseApp.getOptions().getApiKey(); |
| 162 | + } |
| 163 | + |
147 | 164 | /** Returns the nick name of the {@link FirebaseApp} of this {@link FirebaseInstallations} */
|
148 | 165 | @VisibleForTesting
|
149 | 166 | String getName() {
|
@@ -193,7 +210,6 @@ public Task<InstallationTokenResult> getToken(@AuthTokenOption int authTokenOpti
|
193 | 210 | @NonNull
|
194 | 211 | @Override
|
195 | 212 | public Task<Void> delete() {
|
196 |
| - preConditionChecks(); |
197 | 213 | return Tasks.call(executor, this::deleteFirebaseInstallationId);
|
198 | 214 | }
|
199 | 215 |
|
@@ -374,9 +390,9 @@ private PersistedInstallationEntry registerFidWithServer(PersistedInstallationEn
|
374 | 390 |
|
375 | 391 | InstallationResponse response =
|
376 | 392 | serviceClient.createFirebaseInstallation(
|
377 |
| - /*apiKey= */ firebaseApp.getOptions().getApiKey(), |
| 393 | + /*apiKey= */ getApiKey(), |
378 | 394 | /*fid= */ prefs.getFirebaseInstallationId(),
|
379 |
| - /*projectID= */ firebaseApp.getOptions().getProjectId(), |
| 395 | + /*projectID= */ getProjectIdentifier(), |
380 | 396 | /*appId= */ getApplicationId(),
|
381 | 397 | /* migration-header= */ iidToken);
|
382 | 398 |
|
@@ -405,9 +421,9 @@ private PersistedInstallationEntry fetchAuthTokenFromServer(
|
405 | 421 | @NonNull PersistedInstallationEntry prefs) throws IOException {
|
406 | 422 | TokenResult tokenResult =
|
407 | 423 | serviceClient.generateAuthToken(
|
408 |
| - /*apiKey= */ firebaseApp.getOptions().getApiKey(), |
| 424 | + /*apiKey= */ getApiKey(), |
409 | 425 | /*fid= */ prefs.getFirebaseInstallationId(),
|
410 |
| - /*projectID= */ firebaseApp.getOptions().getProjectId(), |
| 426 | + /*projectID= */ getProjectIdentifier(), |
411 | 427 | /*refreshToken= */ prefs.getRefreshToken());
|
412 | 428 |
|
413 | 429 | switch (tokenResult.getResponseCode()) {
|
@@ -437,10 +453,10 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
|
437 | 453 | // Call the FIS servers to delete this Firebase Installation Id.
|
438 | 454 | try {
|
439 | 455 | 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()); |
444 | 460 |
|
445 | 461 | } catch (FirebaseException exception) {
|
446 | 462 | throw new FirebaseInstallationsException(
|
|
0 commit comments