Use `ApiFutures#addCallback` for FCM futures

This commit is contained in:
Jon Chambers 2022-08-04 10:20:41 -04:00 committed by Jon Chambers
parent 28076335e0
commit ab5d8ba120
1 changed files with 13 additions and 13 deletions

View File

@ -6,6 +6,8 @@
package org.whispersystems.textsecuregcm.push; package org.whispersystems.textsecuregcm.push;
import com.google.api.core.ApiFuture; import com.google.api.core.ApiFuture;
import com.google.api.core.ApiFutureCallback;
import com.google.api.core.ApiFutures;
import com.google.auth.oauth2.GoogleCredentials; import com.google.auth.oauth2.GoogleCredentials;
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.firebase.FirebaseApp; import com.google.firebase.FirebaseApp;
@ -15,14 +17,13 @@ import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.FirebaseMessagingException; import com.google.firebase.messaging.FirebaseMessagingException;
import com.google.firebase.messaging.Message; import com.google.firebase.messaging.Message;
import com.google.firebase.messaging.MessagingErrorCode; import com.google.firebase.messaging.MessagingErrorCode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class FcmSender implements PushNotificationSender { public class FcmSender implements PushNotificationSender {
@ -69,12 +70,15 @@ public class FcmSender implements PushNotificationSender {
final ApiFuture<String> sendFuture = firebaseMessagingClient.sendAsync(builder.build()); final ApiFuture<String> sendFuture = firebaseMessagingClient.sendAsync(builder.build());
final CompletableFuture<SendPushNotificationResult> completableSendFuture = new CompletableFuture<>(); final CompletableFuture<SendPushNotificationResult> completableSendFuture = new CompletableFuture<>();
sendFuture.addListener(() -> { ApiFutures.addCallback(sendFuture, new ApiFutureCallback<>() {
try { @Override
sendFuture.get(); public void onSuccess(final String result) {
completableSendFuture.complete(new SendPushNotificationResult(true, null, false)); completableSendFuture.complete(new SendPushNotificationResult(true, null, false));
} catch (ExecutionException e) { }
if (e.getCause() instanceof final FirebaseMessagingException firebaseMessagingException) {
@Override
public void onFailure(final Throwable cause) {
if (cause instanceof final FirebaseMessagingException firebaseMessagingException) {
final String errorCode; final String errorCode;
if (firebaseMessagingException.getMessagingErrorCode() != null) { if (firebaseMessagingException.getMessagingErrorCode() != null) {
@ -88,12 +92,8 @@ public class FcmSender implements PushNotificationSender {
errorCode, errorCode,
firebaseMessagingException.getMessagingErrorCode() == MessagingErrorCode.UNREGISTERED)); firebaseMessagingException.getMessagingErrorCode() == MessagingErrorCode.UNREGISTERED));
} else { } else {
completableSendFuture.completeExceptionally(e.getCause()); completableSendFuture.completeExceptionally(cause);
} }
} catch (InterruptedException e) {
// This should never happen; by definition, if we're in the future's listener, the future is done, and so
// `get()` should return immediately.
completableSendFuture.completeExceptionally(e);
} }
}, executor); }, executor);