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

Add support for starting a Live Activity with input-push-token on iOS 18 #1079

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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 @@ -82,6 +82,7 @@ public abstract class ApnsPayloadBuilder {
private boolean preferStringRepresentationForAlerts = false;

private LiveActivityEvent event = null;
private Boolean 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,23 @@ public ApnsPayloadBuilder setTimestamp(final Instant timestamp) {
return this;
}

/**
* Sets the flag to control whether the OS wakes up the app and delivers a push token to send updates to the
* started Live Activity. It is only respected in iOS 18 and later. Older versions provide a new push token
* by default.

* @param inputPushToken Flag to decide whether the app is provided a push token to update the Live Activity

* @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(final boolean inputPushToken) {
this.inputPushToken = inputPushToken;
return this;
}

/**
* <p>Sets a dismissal timestamp for Live Activity notifications. According to Apple's documentation:</p>
*
Expand Down Expand Up @@ -964,6 +983,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 ? 1 : 0);
}

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(true);

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