implemented scheduled worker that processes queued notifications
This commit is contained in:
@@ -1,16 +1,17 @@
|
||||
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 SuMoC.backend.user.models.User;
|
||||
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.NoSuchElementException;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
@Service
|
||||
@@ -20,24 +21,29 @@ public class NotificationSender {
|
||||
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
|
||||
@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(User user, Notification notification) {
|
||||
// get users notification token
|
||||
NotificationToken token;
|
||||
try {
|
||||
token = notificationTokenRepository.findByUserId(user.getId()).orElseThrow();
|
||||
} catch (NoSuchElementException e) {
|
||||
logger.warning("No notification token found for user id, skipping send: " + user.getId());
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -55,6 +61,19 @@ public class NotificationSender {
|
||||
Message message = firebaseMessageBuilder.generateMessage(notification, token.getToken());
|
||||
|
||||
firebaseMessaging.sendAsync(message)
|
||||
.addListener(() -> logger.info("Notification sent to user"), Runnable::run);
|
||||
.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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user