test fix: correct notification building for ios
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good

This commit is contained in:
2026-02-09 10:27:07 +01:00
parent cc6990a2a1
commit 80abd0f34f
4 changed files with 39 additions and 3 deletions
@@ -116,7 +116,10 @@ public class DevController {
@GetMapping("/test-firebase-adhoc-route-notification/{fcm_token}")
@Operation(description = "Immediately sends a test notification of type ADHOC_ALTERNATIVE to the provided FCM token.")
public ResponseEntity<String> SendTestAdhocRouteNotificationFirebase(@PathVariable("fcm_token") @NotNull String fcmToken) {
public ResponseEntity<String> SendTestAdhocRouteNotificationFirebase(
@PathVariable("fcm_token") @NotNull String fcmToken,
@RequestParam(required = false, defaultValue = "false") boolean isIos
) {
if (firebaseMessaging == null) {
return ResponseEntity.status(503)
.body("✗ Firebase is not configured (firebase.config.enabled=false)");
@@ -127,7 +130,12 @@ public class DevController {
List<RouteDTO> mockRoutes = List.of(RouteDTO.getMockRouteDTO(), RouteDTO.getMockRouteDTO());
notification.setData(objectMapper.writeValueAsString(mockRoutes));
Message mockAdhocMessage = firebaseMessageBuilder.generateMessage(notification, fcmToken);
Message mockAdhocMessage;
if (isIos) {
mockAdhocMessage = firebaseMessageBuilder.generateMessageIos(notification, fcmToken);
} else {
mockAdhocMessage = firebaseMessageBuilder.generateMessage(notification, fcmToken);
}
// Send to a topic (no device registration needed)
String message = firebaseMessaging.sendAsync(
@@ -292,3 +300,4 @@ public class DevController {
}
}
@@ -14,4 +14,17 @@ public class FirebaseMessageBuilder {
return builder.build();
}
public Message generateMessageIos(Notification notification, String token) {
var builder = Message.builder().setToken(token);
builder.putData("type", notification.getType().toString());
builder.putData("data", notification.getData());
var apnConfig = com.google.firebase.messaging.ApnsConfig.builder()
.setAps(com.google.firebase.messaging.Aps.builder()
.setContentAvailable(true)
.build())
.build();
builder.setApnsConfig(apnConfig);
return builder.build();
}
}
@@ -2,4 +2,5 @@ package SuMoC.backend.notification.enums;
public enum NotificationProvider {
FIREBASE,
FIREBASE_IOS
}
@@ -52,9 +52,20 @@ public class NotificationSender {
switch (token.getProvider()) {
case FIREBASE -> sendViaFirebase(token, notification);
case FIREBASE_IOS -> sendViaFirebaseIos(token, notification);
}
}
private void sendViaFirebaseIos(NotificationToken token, Notification notification) {
if (firebaseMessaging == null) {
logger.warning("FirebaseMessaging not configured");
return;
}
Message message = firebaseMessageBuilder.generateMessageIos(notification, token.getToken());
sendAndHandleResultFirebase(message, notification);
}
private void sendViaFirebase(NotificationToken token, Notification notification) {
if (firebaseMessaging == null) {
logger.warning("FirebaseMessaging not configured");
@@ -62,7 +73,10 @@ public class NotificationSender {
}
Message message = firebaseMessageBuilder.generateMessage(notification, token.getToken());
sendAndHandleResultFirebase(message, notification);
}
private void sendAndHandleResultFirebase(Message message, Notification notification) {
ApiFuture<String> future = firebaseMessaging.sendAsync(message);
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
@Override
@@ -84,6 +98,5 @@ public class NotificationSender {
logger.severe("Failed to send notification with id:" + notification.getId() + " " + t.getMessage());
}
}, MoreExecutors.directExecutor());
}
}