Author SHA1 Message Date
neuhauss 856f530f99 extend notification docs
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
2026-02-09 10:32:32 +01:00
neuhauss 80abd0f34f test fix: correct notification building for ios
LocationBasedServices/sumoc-backend/pipeline/head This commit looks good
2026-02-09 10:27:07 +01:00
5 changed files with 52 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();
}
}
@@ -37,6 +37,19 @@ 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.
### Notification Providers
When registering a device token, clients must specify the notification provider. The supported providers are defined in
`NotificationProvider.java`:
- **FIREBASE**: Use this provider for Android devices. Tokens registered with this type will be used to send
notifications via Firebase Cloud Messaging (FCM) for Android.
- **FIREBASE_IOS**: Use this provider for iOS devices. Tokens registered with this type will be used to send
notifications via Firebase Cloud Messaging (FCM) for iOS.
Specify the appropriate provider when registering the device token to ensure notifications are delivered correctly to
the device platform.
---
## For Backend Developers
@@ -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());
}
}