Skip to content

Commit

Permalink
Add support for starting a Live Activity with input-push-token on iOS 18
Browse files Browse the repository at this point in the history
  • Loading branch information
ifrins committed Aug 17, 2024
1 parent 143584b commit 13af288
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public abstract class ApnsPayloadBuilder {
private boolean preferStringRepresentationForAlerts = false;

private LiveActivityEvent event = null;
private Integer inputPushToken = null;

private Instant timestamp = null;

Expand Down Expand Up @@ -131,6 +132,7 @@ public abstract class ApnsPayloadBuilder {
private static final String ATTRIBUTES_TYPE_KEY = "attributes-type";
private static final String ATTRIBUTES_KEY = "attributes";
private static final String CONTENT_STATE_KEY = "content-state";
private static final String INPUT_PUSH_TOKEN_KEY = "input-push-token";

public static final String[] EMPTY_STRING_ARRAY = new String[0];

Expand Down Expand Up @@ -769,7 +771,7 @@ public ApnsPayloadBuilder addCustomProperty(final String key, final Object value
*
* @return a reference to this payload builder
*
* @see #setAttributes(Map)
* @see #setAttributes(Map)
* @see <a href="https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications#Construct-the-payload-that-starts-a-Live-Activity">
* Construct the payload that starts a Live Activity</a>
* @see <a href="https://developer.apple.com/documentation/activitykit/activityattributes">ActivityKit - ActivityAttributes</a>
Expand All @@ -789,7 +791,7 @@ public ApnsPayloadBuilder setAttributesType(final String attributesType) {
*
* @return a reference to this payload builder
*
* @see #setAttributesType(String)
* @see #setAttributesType(String)
* @see <a href="https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications#Construct-the-payload-that-starts-a-Live-Activity">
* Construct the payload that starts a Live Activity</a>
* @see <a href="https://developer.apple.com/documentation/activitykit/activityattributes/contentstate">ActivityKit - ContentState</a>
Expand Down Expand Up @@ -856,6 +858,19 @@ public ApnsPayloadBuilder setTimestamp(final Instant timestamp) {
return this;
}

/**
* <p>Sets the flag to wake up the app and receive a push token to send updates to the started Live Activity.</p>
*
* @return a reference to this payload builder
*
* @see <a href="https://developer.apple.com/documentation/activitykit/starting-and-updating-live-activities-with-activitykit-push-notifications">
* Starting and updating Live Activities with ActivityKit push notifications</a>
*/
public ApnsPayloadBuilder setInputPushToken() {
this.inputPushToken = 1;
return this;
}

/**
* <p>Sets a dismissal timestamp for Live Activity notifications. According to Apple's documentation:</p>
*
Expand Down Expand Up @@ -964,6 +979,10 @@ protected Map<String, Object> buildPayloadMap() {
aps.put(STALE_DATE_KEY, this.staleDate.getEpochSecond());
}

if (this.inputPushToken != null) {
aps.put(INPUT_PUSH_TOKEN_KEY, this.inputPushToken);
}

final Map<String, Object> alert = new HashMap<>();
{
if (this.alertBody != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,15 @@ void testAddContentStateProperty() {
assertEquals(subMap, serializedContentState.get(keyForMapValue));
}

@Test
void setInputPushToken() {
this.builder.setInputPushToken();

final Map<String, Object> aps = this.extractApsObjectFromPayloadString(this.builder.build());

assertEquals(1L, aps.get("input-push-token"));
}

@SuppressWarnings("unchecked")
private Map<String, Object> extractApsObjectFromPayloadString(final String payloadString) {
final Map<String, Object> payload;
Expand Down

0 comments on commit 13af288

Please sign in to comment.