From bc3dd9b59b61fc65d4468aa2c9cc8ded488338f6 Mon Sep 17 00:00:00 2001 From: David Neuhauss Date: Tue, 13 Jan 2026 16:48:18 +0100 Subject: [PATCH] correctly recognize firebase errors when sending exceptions --- .../services/NotificationSender.java | 39 ++++++++++++------- 1 file changed, 24 insertions(+), 15 deletions(-) diff --git a/src/main/java/SuMoC/backend/notification/services/NotificationSender.java b/src/main/java/SuMoC/backend/notification/services/NotificationSender.java index abfe545..a1af745 100644 --- a/src/main/java/SuMoC/backend/notification/services/NotificationSender.java +++ b/src/main/java/SuMoC/backend/notification/services/NotificationSender.java @@ -7,6 +7,10 @@ import SuMoC.backend.notification.models.Notification; import SuMoC.backend.notification.models.NotificationToken; import SuMoC.backend.notification.repositories.NotificationRepository; import SuMoC.backend.notification.repositories.NotificationTokenRepository; +import com.google.api.core.ApiFuture; +import com.google.api.core.ApiFutureCallback; +import com.google.api.core.ApiFutures; +import com.google.common.util.concurrent.MoreExecutors; import com.google.firebase.messaging.FirebaseMessaging; import com.google.firebase.messaging.Message; import org.springframework.beans.factory.annotation.Autowired; @@ -60,20 +64,25 @@ public class NotificationSender { Message message = firebaseMessageBuilder.generateMessage(notification, token.getToken()); - firebaseMessaging.sendAsync(message) - .addListener(() -> { - try { - notification.setStatus(NotificationStatus.SENT); - notificationRepository.save(notification); - logger.info("Notification sent successfully to user: " + notification.getUser().getId()); - } catch (Exception e) { - notification.incrementFailedSendAttempts(); - if (notification.getFailedSendAttempts() >= notificationProperties.getMaxFailedSendAttempts()) { - notification.setStatus(NotificationStatus.FAILED); - } - notificationRepository.save(notification); - logger.severe("Failed to send notification: " + e.getMessage()); - } - }, Runnable::run); + ApiFuture future = firebaseMessaging.sendAsync(message); + ApiFutures.addCallback(future, new ApiFutureCallback() { + @Override + public void onSuccess(String messageId) { + notification.setStatus(NotificationStatus.SENT); + notificationRepository.save(notification); + logger.info("Notification sent successfully to user: " + notification.getUser().getId()); + } + + @Override + public void onFailure(Throwable t) { + notification.incrementFailedSendAttempts(); + if (notification.getFailedSendAttempts() >= notificationProperties.getMaxFailedSendAttempts()) { + notification.setStatus(NotificationStatus.FAILED); + } + notificationRepository.save(notification); + logger.severe("Failed to send notification with id:" + notification.getId() + " " + t.getMessage()); + } + }, MoreExecutors.directExecutor()); + } }