diff --git a/src/main/java/SuMoC/backend/notification/NotificationController.java b/src/main/java/SuMoC/backend/notification/NotificationController.java index 8571ae5..c3d0e51 100644 --- a/src/main/java/SuMoC/backend/notification/NotificationController.java +++ b/src/main/java/SuMoC/backend/notification/NotificationController.java @@ -37,6 +37,14 @@ public class NotificationController { return ResponseEntity.ok().build(); } + @PutMapping("/unregister-device-token") + @Operation(description = "Unregisters the device token for push notifications for the authenticated user.") + public ResponseEntity unregisterDeviceToken() { + User user = getAuthenticatedUser(); + notificationTokenService.unregisterDeviceToken(user); + return ResponseEntity.ok().build(); + } + // Simple record for request body public record RegisterDeviceTokenRequest(String token, NotificationProvider provider) { } diff --git a/src/main/java/SuMoC/backend/notification/README.MD b/src/main/java/SuMoC/backend/notification/README.MD index 3f92a34..0603687 100644 --- a/src/main/java/SuMoC/backend/notification/README.MD +++ b/src/main/java/SuMoC/backend/notification/README.MD @@ -37,6 +37,13 @@ Each notification type includes a data payload. The main DTOs in the notificatio To receive push notifications, clients must register their device token. Token registration is handled in the `NotificationController`. The backend stores or updates the token for the authenticated user. +### Unregistering Device Token + +When a user logs out, the client should call the device token unregistration endpoint to remove the device token from +the backend. This ensures the user will no longer receive push notifications on that device after logout. + +- **Endpoint:** `PUT /notifications/unregister-device-token` + --- ## For Backend Developers diff --git a/src/main/java/SuMoC/backend/notification/services/NotificationSender.java b/src/main/java/SuMoC/backend/notification/services/NotificationSender.java index e4d2b58..69bd5bb 100644 --- a/src/main/java/SuMoC/backend/notification/services/NotificationSender.java +++ b/src/main/java/SuMoC/backend/notification/services/NotificationSender.java @@ -46,6 +46,8 @@ public class NotificationSender { if (token == null) { logger.warning("No notification token found notification id: " + notification.getId()); notification.incrementFailedSendAttempts(); + notification.setLastFailureReason("No notification token found for user"); + setFailedIfLimitExceeded(notification); notificationRepository.save(notification); return; } @@ -77,13 +79,17 @@ public class NotificationSender { public void onFailure(Throwable t) { notification.incrementFailedSendAttempts(); notification.setLastFailureReason(t.getMessage()); - if (notification.getFailedSendAttempts() >= notificationProperties.getMaxFailedSendAttempts()) { - notification.setStatus(NotificationStatus.FAILED); - } + setFailedIfLimitExceeded(notification); notificationRepository.save(notification); logger.severe("Failed to send notification with id:" + notification.getId() + " " + t.getMessage()); } }, MoreExecutors.directExecutor()); } + + private void setFailedIfLimitExceeded(Notification notification) { + if (notification.getFailedSendAttempts() >= notificationProperties.getMaxFailedSendAttempts()) { + notification.setStatus(NotificationStatus.FAILED); + } + } } diff --git a/src/main/java/SuMoC/backend/notification/services/NotificationTokenService.java b/src/main/java/SuMoC/backend/notification/services/NotificationTokenService.java index 978791a..81ecae8 100644 --- a/src/main/java/SuMoC/backend/notification/services/NotificationTokenService.java +++ b/src/main/java/SuMoC/backend/notification/services/NotificationTokenService.java @@ -4,6 +4,7 @@ import SuMoC.backend.notification.enums.NotificationProvider; import SuMoC.backend.notification.models.NotificationToken; import SuMoC.backend.notification.repositories.NotificationTokenRepository; import SuMoC.backend.user.models.User; +import jakarta.transaction.Transactional; import lombok.RequiredArgsConstructor; import org.springframework.stereotype.Service; @@ -27,4 +28,13 @@ public class NotificationTokenService { notificationTokenRepository.save(newToken); } } + + @Transactional + public void unregisterDeviceToken(User user) { + var existingToken = notificationTokenRepository.findByUserId(user.getId()); + existingToken.ifPresent(token -> { + user.setNotificationToken(null); + notificationTokenRepository.delete(token); + }); + } } diff --git a/src/main/java/SuMoC/backend/user/models/User.java b/src/main/java/SuMoC/backend/user/models/User.java index fde40ba..02357e8 100644 --- a/src/main/java/SuMoC/backend/user/models/User.java +++ b/src/main/java/SuMoC/backend/user/models/User.java @@ -59,7 +59,7 @@ public class User { @Column(name = "role") private Set roles = new HashSet<>(); - @OneToOne + @OneToOne(cascade = CascadeType.REMOVE, orphanRemoval = true) @JoinColumn(name = "id", referencedColumnName = "user_id") private NotificationToken notificationToken; @@ -76,7 +76,8 @@ public class User { @OneToOne(mappedBy = "user", cascade = CascadeType.ALL, orphanRemoval = true) private ProfileImageEntity profileImage; - public User() {} + public User() { + } // public User(Long id, String publicName, String privateName, String email, String password, VisibilityPrivacy visibilityPrivacy, String friendCode, Set trips, Set friendships, Set roles) { // this.id = id; @@ -103,7 +104,8 @@ public class User { this.statsList = new StatsList(this); this.profileImage = new ProfileImageEntity(); - this.profileImage.setUser(this); } + this.profileImage.setUser(this); + } @PrePersist private void ensureFriendCode() {