Skip to content

feat(fcm): Add liveActivityToken to ApnsConfig #1094

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

Open
wants to merge 3 commits into
base: master
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
16 changes: 16 additions & 0 deletions src/main/java/com/google/firebase/messaging/ApnsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ public class ApnsConfig {
@Key("fcm_options")
private final ApnsFcmOptions fcmOptions;

@Key("live_activity_token")
private final String liveActivityToken;

private ApnsConfig(Builder builder) {
checkArgument(builder.aps != null, "aps must be specified");
checkArgument(!builder.customData.containsKey("aps"),
Expand All @@ -51,6 +54,7 @@ private ApnsConfig(Builder builder) {
.put("aps", builder.aps.getFields())
.build();
this.fcmOptions = builder.fcmOptions;
this.liveActivityToken = builder.liveActivityToken;
}

/**
Expand All @@ -68,6 +72,7 @@ public static class Builder {
private final Map<String, Object> customData = new HashMap<>();
private Aps aps;
private ApnsFcmOptions fcmOptions;
private String liveActivityToken;

private Builder() {}

Expand Down Expand Up @@ -137,6 +142,17 @@ public Builder setFcmOptions(ApnsFcmOptions apnsFcmOptions) {
return this;
}

/**
* Sets the Live Activity token.
*
* @param liveActivityToken Live Activity token.
* @return This builder.
*/
public Builder setLiveActivityToken(String liveActivityToken) {
this.liveActivityToken = liveActivityToken;
return this;
}

/**
* Creates a new {@link ApnsConfig} instance from the parameters set on this builder.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public void testSend() throws Exception {
.setBody("Body")
.build())
.build())
.setLiveActivityToken("integration-test-live-activity-token")
.build())
.setWebpushConfig(WebpushConfig.builder()
.putHeader("X-Custom-Val", "Foo")
Expand Down
46 changes: 41 additions & 5 deletions src/test/java/com/google/firebase/messaging/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,7 @@ public void testApnsMessageWithPayload() throws IOException {
.putCustomData("cd1", "cd-v1")
.putAllCustomData(ImmutableMap.<String, Object>of("cd2", "cd-v2", "cd3", true))
.setAps(Aps.builder().build())
.setLiveActivityToken("test-live-activity-token")
.build())
.setTopic("test-topic")
.build();
Expand All @@ -421,10 +422,11 @@ public void testApnsMessageWithPayload() throws IOException {
.put("cd3", true)
.put("aps", ImmutableMap.of())
.build();
Map<String, Object> data = ImmutableMap.<String, Object>of(
"headers", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
"payload", payload
);
Map<String, Object> data = ImmutableMap.<String, Object>builder()
.put("headers", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"))
.put("payload", payload)
.put("live_activity_token", "test-live-activity-token")
.build();
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "apns", data), message);
}

Expand All @@ -442,6 +444,7 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
.setSound("test-sound")
.setThreadId("test-thread-id")
.build())
.setLiveActivityToken("test-live-activity-token-aps")
.build())
.setTopic("test-topic")
.build();
Expand All @@ -459,7 +462,10 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
assertJsonEquals(
ImmutableMap.of(
"topic", "test-topic",
"apns", ImmutableMap.<String, Object>of("payload", payload)),
"apns", ImmutableMap.<String, Object>builder()
.put("payload", payload)
.put("live_activity_token", "test-live-activity-token-aps")
.build()),
message);

message = Message.builder()
Expand Down Expand Up @@ -825,6 +831,7 @@ public void testImageInApnsNotification() throws IOException {
.setApnsConfig(
ApnsConfig.builder().setAps(Aps.builder().build())
.setFcmOptions(ApnsFcmOptions.builder().setImage(TEST_IMAGE_URL_APNS).build())
.setLiveActivityToken("test-live-activity-token-image")
.build()).build();

ImmutableMap<String, Object> notification =
Expand All @@ -837,6 +844,7 @@ public void testImageInApnsNotification() throws IOException {
ImmutableMap.<String, Object>builder()
.put("fcm_options", ImmutableMap.of("image", TEST_IMAGE_URL_APNS))
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
.put("live_activity_token", "test-live-activity-token-image")
.build();
ImmutableMap<String, Object> expected =
ImmutableMap.<String, Object>builder()
Expand All @@ -847,6 +855,34 @@ public void testImageInApnsNotification() throws IOException {
assertJsonEquals(expected, message);
}

@Test
public void testApnsMessageWithOnlyLiveActivityToken() throws IOException {
Message message = Message.builder()
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder().build())
.setLiveActivityToken("only-live-activity")
.build())
.setTopic("test-topic")
.build();
Map<String, Object> expectedApns = ImmutableMap.<String, Object>builder()
.put("live_activity_token", "only-live-activity")
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
.build();
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "apns", expectedApns), message);

// Test without live activity token
message = Message.builder()
.setApnsConfig(ApnsConfig.builder()
.setAps(Aps.builder().build())
.build())
.setTopic("test-topic")
.build();
expectedApns = ImmutableMap.<String, Object>builder()
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
.build();
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "apns", expectedApns), message);
}

@Test
public void testInvalidColorInAndroidNotificationLightSettings() {
try {
Expand Down