Skip to content

Commit

Permalink
New iOS Notification api
Browse files Browse the repository at this point in the history
  • Loading branch information
PaRangger committed Nov 15, 2024
1 parent dea4722 commit ec52c8a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -64,18 +65,28 @@ public ResponseEntity<Void> 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<Void> 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<SimpleApnsPushNotification, PushNotificationResponse<SimpleApnsPushNotification>> responsePushNotificationFuture = apnsClient.sendNotification(notification);
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -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) {
}
Original file line number Diff line number Diff line change
@@ -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));
}
}

0 comments on commit ec52c8a

Please sign in to comment.