correctly recognize firebase errors when sending exceptions

This commit is contained in:
2026-01-13 16:48:18 +01:00
parent 06aa0ce3cc
commit bc3dd9b59b
@@ -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<String> future = firebaseMessaging.sendAsync(message);
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
@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());
}
}