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

Live activity attributes type #1051

Merged
merged 2 commits into from
Feb 17, 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 @@ -89,6 +89,9 @@ public abstract class ApnsPayloadBuilder {

private Instant staleDate = null;

private String attributesType = null;
private Map<String, Object> attributes = null;

private Map<String, Object> contentState = null;

private static final String APS_KEY = "aps";
Expand Down Expand Up @@ -125,6 +128,8 @@ public abstract class ApnsPayloadBuilder {
private static final String DISMISSAL_DATE_KEY = "dismissal-date";
private static final String STALE_DATE_KEY = "stale-date";
private static final String EVENT_KEY = "event";
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";

public static final String[] EMPTY_STRING_ARRAY = new String[0];
Expand Down Expand Up @@ -756,6 +761,46 @@ public ApnsPayloadBuilder addCustomProperty(final String key, final Object value
return this;
}

/**
* Sets the type of iOS {@code ActivityAttributes} used for a Live Activity. Must be set for a notification that
* starts a live activity.
*
* @param attributesType the type of {@code ActivityAttributes} used for a Live Activity
*
* @return a reference to this payload builder
*
* @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>
*
* @since 0.15.4
*/
public ApnsPayloadBuilder setAttributesType(final String attributesType) {
this.attributesType = attributesType;
return this;
}

/**
* Sets the values of iOS {@code ContentState} attributes when starting a Live Activity. Must be set for a
* notification that starts a live activity.
*
* @param attributes the attributes used to populate the {@code ContentState} for the newly-started live activity
*
* @return a reference to this payload builder
*
* @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>
*
* @since 0.15.4
*/
public ApnsPayloadBuilder setAttributes(final Map<String, Object> attributes) {
this.attributes = attributes;
return this;
}

/**
* <p>Sets the content state object inside content-state payload for Live Activities.</p>
*
Expand All @@ -768,6 +813,8 @@ public ApnsPayloadBuilder addCustomProperty(final String key, final Object value
*
* @see <a href="https://developer.apple.com/documentation/activitykit/update-and-end-your-live-activity-with-remote-push-notifications">
* Updating and ending your Live Activity with remote push notifications</a>
*
* @since 0.15.2
*/
public ApnsPayloadBuilder setContentState(final Map<String, Object> contentState) {
this.contentState = contentState;
Expand All @@ -784,6 +831,8 @@ public ApnsPayloadBuilder setContentState(final Map<String, Object> contentState
*
* @see <a href="https://developer.apple.com/documentation/activitykit/update-and-end-your-live-activity-with-remote-push-notifications">
* Updating and ending your Live Activity with remote push notifications</a>
*
* @since 0.15.2
*/
public ApnsPayloadBuilder setEvent(final LiveActivityEvent event) {
this.event = event;
Expand All @@ -799,6 +848,8 @@ public ApnsPayloadBuilder setEvent(final LiveActivityEvent event) {
*
* @see <a href="https://developer.apple.com/documentation/activitykit/update-and-end-your-live-activity-with-remote-push-notifications">
* Updating and ending your Live Activity with remote push notifications</a>
*
* @since 0.15.2
*/
public ApnsPayloadBuilder setTimestamp(final Instant timestamp) {
this.timestamp = timestamp;
Expand All @@ -819,6 +870,8 @@ public ApnsPayloadBuilder setTimestamp(final Instant timestamp) {
*
* @see <a href="https://developer.apple.com/documentation/activitykit/update-and-end-your-live-activity-with-remote-push-notifications">
* Updating and ending your Live Activity with remote push notifications</a>
*
* @since 0.15.2
*/
public ApnsPayloadBuilder setDismissalDate(final Instant dismissalDate) {
this.dismissalDate = dismissalDate;
Expand Down Expand Up @@ -891,6 +944,14 @@ protected Map<String, Object> buildPayloadMap() {
aps.put(EVENT_KEY, this.event.getValue());
}

if (this.attributesType != null) {
aps.put(ATTRIBUTES_TYPE_KEY, this.attributesType);
}

if (this.attributes != null) {
aps.put(ATTRIBUTES_KEY, this.attributes);
}

if (this.contentState != null) {
aps.put(CONTENT_STATE_KEY, this.contentState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.EnumSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;

import java.text.ParseException;
import java.time.Instant;
Expand Down Expand Up @@ -665,6 +664,42 @@ void setEvent(LiveActivityEvent event) {
assertEquals(event.getValue(), aps.get("event"));
}

@Test
void testSetAttributesType() {
final String attributesType = "TestAttributes";
this.builder.setAttributesType(attributesType);

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

assertEquals(attributesType, aps.get("attributes-type"));
}

@Test
void testSetAttributes() {
final String keyForStringValue = "string";
final String stringValue = "Hello";

final String keyForLongValue = "integer";
final long longValue = 12;

final String keyForMapValue = "map";
final Map<String, Object> attributes = new HashMap<>();
final Map<String, Object> subMap = new HashMap<>();
subMap.put("boolean", true);
attributes.put(keyForLongValue, longValue);
attributes.put(keyForStringValue, stringValue);
attributes.put(keyForMapValue, subMap);

this.builder.setAttributes(attributes);

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

assertEquals(stringValue, serializedAttributes.get(keyForStringValue));
assertEquals(longValue, serializedAttributes.get(keyForLongValue));
assertEquals(subMap, serializedAttributes.get(keyForMapValue));
}

@Test
void testAddContentStateProperty() {
final String keyForStringValue = "string";
Expand Down
Loading