Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
856f530f99 | ||
|
|
80abd0f34f |
@@ -116,7 +116,10 @@ public class DevController {
|
|||||||
|
|
||||||
@GetMapping("/test-firebase-adhoc-route-notification/{fcm_token}")
|
@GetMapping("/test-firebase-adhoc-route-notification/{fcm_token}")
|
||||||
@Operation(description = "Immediately sends a test notification of type ADHOC_ALTERNATIVE to the provided 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) {
|
if (firebaseMessaging == null) {
|
||||||
return ResponseEntity.status(503)
|
return ResponseEntity.status(503)
|
||||||
.body("✗ Firebase is not configured (firebase.config.enabled=false)");
|
.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());
|
List<RouteDTO> mockRoutes = List.of(RouteDTO.getMockRouteDTO(), RouteDTO.getMockRouteDTO());
|
||||||
notification.setData(objectMapper.writeValueAsString(mockRoutes));
|
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)
|
// Send to a topic (no device registration needed)
|
||||||
String message = firebaseMessaging.sendAsync(
|
String message = firebaseMessaging.sendAsync(
|
||||||
@@ -292,3 +300,4 @@ public class DevController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -14,4 +14,17 @@ public class FirebaseMessageBuilder {
|
|||||||
return builder.build();
|
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
|
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.
|
`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
|
## For Backend Developers
|
||||||
|
|||||||
@@ -2,4 +2,5 @@ package SuMoC.backend.notification.enums;
|
|||||||
|
|
||||||
public enum NotificationProvider {
|
public enum NotificationProvider {
|
||||||
FIREBASE,
|
FIREBASE,
|
||||||
|
FIREBASE_IOS
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -52,9 +52,20 @@ public class NotificationSender {
|
|||||||
|
|
||||||
switch (token.getProvider()) {
|
switch (token.getProvider()) {
|
||||||
case FIREBASE -> sendViaFirebase(token, notification);
|
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) {
|
private void sendViaFirebase(NotificationToken token, Notification notification) {
|
||||||
if (firebaseMessaging == null) {
|
if (firebaseMessaging == null) {
|
||||||
logger.warning("FirebaseMessaging not configured");
|
logger.warning("FirebaseMessaging not configured");
|
||||||
@@ -62,7 +73,10 @@ public class NotificationSender {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Message message = firebaseMessageBuilder.generateMessage(notification, token.getToken());
|
Message message = firebaseMessageBuilder.generateMessage(notification, token.getToken());
|
||||||
|
sendAndHandleResultFirebase(message, notification);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void sendAndHandleResultFirebase(Message message, Notification notification) {
|
||||||
ApiFuture<String> future = firebaseMessaging.sendAsync(message);
|
ApiFuture<String> future = firebaseMessaging.sendAsync(message);
|
||||||
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
|
ApiFutures.addCallback(future, new ApiFutureCallback<String>() {
|
||||||
@Override
|
@Override
|
||||||
@@ -84,6 +98,5 @@ public class NotificationSender {
|
|||||||
logger.severe("Failed to send notification with id:" + notification.getId() + " " + t.getMessage());
|
logger.severe("Failed to send notification with id:" + notification.getId() + " " + t.getMessage());
|
||||||
}
|
}
|
||||||
}, MoreExecutors.directExecutor());
|
}, MoreExecutors.directExecutor());
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user