Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notifications: Add new iOS notification api #11

Merged
merged 1 commit into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}
Loading