diff --git a/apns/src/main/java/de/tum/cit/artemis/push/apns/ApnsSendService.java b/apns/src/main/java/de/tum/cit/artemis/push/apns/ApnsSendService.java index 4f9a5c6..447dc35 100644 --- a/apns/src/main/java/de/tum/cit/artemis/push/apns/ApnsSendService.java +++ b/apns/src/main/java/de/tum/cit/artemis/push/apns/ApnsSendService.java @@ -5,6 +5,7 @@ import com.eatthepath.pushy.apns.util.SimpleApnsPushNotification; import com.eatthepath.pushy.apns.util.concurrent.PushNotificationFuture; import de.tum.cit.artemis.push.common.NotificationRequest; +import de.tum.cit.artemis.push.common.PushNotificationApiType; import de.tum.cit.artemis.push.common.SendService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -64,18 +65,28 @@ public ResponseEntity send(NotificationRequest request) { // TODO: either we send this async (in a separate bean), but then we cannot return the response entity, or we send it sync and block the thread (as we do it now) // @Async private ResponseEntity sendApnsRequest(NotificationRequest request) { - String payload = new SimpleApnsPayloadBuilder() - .setContentAvailable(true) + var payload = new SimpleApnsPayloadBuilder() .addCustomProperty("iv", request.initializationVector()) - .addCustomProperty("payload", request.payloadCipherText()) - .build(); + .addCustomProperty("payload", request.payloadCipherText()); + + var isV2Api = request.apiType() == PushNotificationApiType.IOS_V2; + + if (isV2Api) { + payload.setMutableContent(true); + // Alert Body is a fallback in case we cannot decrypt the payload + payload.setAlertBody("There is a new notification in Artemis."); + } else { + payload.setContentAvailable(true); + } + + var playloadString = payload.build(); SimpleApnsPushNotification notification = new SimpleApnsPushNotification(request.token(), "de.tum.cit.ase.artemis", - payload, + playloadString, Instant.now().plus(Duration.ofDays(7)), DeliveryPriority.getFromCode(5), - PushType.BACKGROUND); + isV2Api ? PushType.ALERT : PushType.BACKGROUND); PushNotificationFuture> responsePushNotificationFuture = apnsClient.sendNotification(notification); try { diff --git a/common/src/main/java/de/tum/cit/artemis/push/common/NotificationRequest.java b/common/src/main/java/de/tum/cit/artemis/push/common/NotificationRequest.java index afee595..9eed319 100644 --- a/common/src/main/java/de/tum/cit/artemis/push/common/NotificationRequest.java +++ b/common/src/main/java/de/tum/cit/artemis/push/common/NotificationRequest.java @@ -1,4 +1,4 @@ package de.tum.cit.artemis.push.common; -public record NotificationRequest(String initializationVector, String payloadCipherText, String token) { +public record NotificationRequest(String initializationVector, String payloadCipherText, String token, PushNotificationApiType apiType) { } diff --git a/common/src/main/java/de/tum/cit/artemis/push/common/PushNotificationApiType.java b/common/src/main/java/de/tum/cit/artemis/push/common/PushNotificationApiType.java new file mode 100644 index 0000000..0a3bc14 --- /dev/null +++ b/common/src/main/java/de/tum/cit/artemis/push/common/PushNotificationApiType.java @@ -0,0 +1,23 @@ +package de.tum.cit.artemis.push.common; + +import java.util.Arrays; + +public enum PushNotificationApiType { + + DEFAULT((short) 0), IOS_V2((short) 1); + + private final short databaseKey; + + PushNotificationApiType(short databaseKey) { + this.databaseKey = databaseKey; + } + + public short getDatabaseKey() { + return databaseKey; + } + + public static PushNotificationApiType fromDatabaseKey(short databaseKey) { + return Arrays.stream(PushNotificationApiType.values()).filter(type -> type.getDatabaseKey() == databaseKey).findFirst() + .orElseThrow(() -> new IllegalArgumentException("Unknown database key: " + databaseKey)); + } +}