89 lines
3.8 KiB
Java
89 lines
3.8 KiB
Java
package SuMoC.backend.notification.services;
|
|
|
|
import SuMoC.backend.firebase.FirebaseMessageBuilder;
|
|
import SuMoC.backend.notification.config.NotificationProperties;
|
|
import SuMoC.backend.notification.enums.NotificationStatus;
|
|
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;
|
|
import org.springframework.stereotype.Service;
|
|
|
|
import java.util.logging.Logger;
|
|
|
|
@Service
|
|
public class NotificationSender {
|
|
|
|
private final NotificationTokenRepository notificationTokenRepository;
|
|
private final FirebaseMessageBuilder firebaseMessageBuilder;
|
|
private final Logger logger = Logger.getLogger(NotificationSender.class.getName());
|
|
private final FirebaseMessaging firebaseMessaging;
|
|
private final NotificationRepository notificationRepository;
|
|
private final NotificationProperties notificationProperties;
|
|
|
|
public NotificationSender(
|
|
NotificationTokenRepository notificationTokenRepository,
|
|
FirebaseMessageBuilder firebaseMessageBuilder,
|
|
@Autowired(required = false) FirebaseMessaging firebaseMessaging,
|
|
NotificationRepository notificationRepository, NotificationProperties notificationProperties
|
|
) {
|
|
this.notificationTokenRepository = notificationTokenRepository;
|
|
this.firebaseMessageBuilder = firebaseMessageBuilder;
|
|
this.firebaseMessaging = firebaseMessaging;
|
|
this.notificationRepository = notificationRepository;
|
|
this.notificationProperties = notificationProperties;
|
|
}
|
|
|
|
public void sendNotification(Notification notification) {
|
|
NotificationToken token = notification.getUser().getNotificationToken();
|
|
|
|
if (token == null) {
|
|
logger.warning("No notification token found notification id: " + notification.getId());
|
|
notification.incrementFailedSendAttempts();
|
|
notificationRepository.save(notification);
|
|
return;
|
|
}
|
|
|
|
switch (token.getProvider()) {
|
|
case FIREBASE -> sendViaFirebase(token, notification);
|
|
}
|
|
}
|
|
|
|
private void sendViaFirebase(NotificationToken token, Notification notification) {
|
|
if (firebaseMessaging == null) {
|
|
logger.warning("FirebaseMessaging not configured");
|
|
return;
|
|
}
|
|
|
|
Message message = firebaseMessageBuilder.generateMessage(notification, token.getToken());
|
|
|
|
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());
|
|
|
|
}
|
|
}
|