Skip to content

Commit c6970a5

Browse files
lahirumarambagoogle-labs-jules[bot]jonathanedey
authored
feat(fcm): Add liveActivityToken to ApnsConfig (#1094)
* feat: Add liveActivityToken to ApnsConfig Adds a new `liveActivityToken` field to the `ApnsConfig` class, allowing you to specify a Live Activity token for APNS messages. The changes include: - Addition of the `liveActivityToken` field in `ApnsConfig.java` and its associated builder. - Updates to unit tests in `MessageTest.java` to verify the correct serialization of the new field and to include it in existing test cases. A new test method `testApnsMessageWithOnlyLiveActivityToken` was added for specific testing. - Update to the integration test in `FirebaseMessagingIT.java` to include the `liveActivityToken` in a test message. An unrelated fix was also included in `MessageTest.java` to correctly use `new BigDecimal(long)` instead of `BigDecimal.valueOf(long)` for `notification_count`. * fix: Correct ApnsConfig liveActivityToken JSON key Corrects the `@Key` annotation for the `liveActivityToken` field in `ApnsConfig.java` from "live-activity-token" to "live_activity_token" to follow snake_case convention for JSON mapping. Unit tests in `MessageTest.java` have been updated to reflect this change in the expected JSON output. * remove unrelated changes --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Jonathan Edey <145066863+jonathanedey@users.noreply.github.com>
1 parent ac4a8ad commit c6970a5

File tree

3 files changed

+58
-5
lines changed

3 files changed

+58
-5
lines changed

src/main/java/com/google/firebase/messaging/ApnsConfig.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,9 @@ public class ApnsConfig {
4141
@Key("fcm_options")
4242
private final ApnsFcmOptions fcmOptions;
4343

44+
@Key("live_activity_token")
45+
private final String liveActivityToken;
46+
4447
private ApnsConfig(Builder builder) {
4548
checkArgument(builder.aps != null, "aps must be specified");
4649
checkArgument(!builder.customData.containsKey("aps"),
@@ -51,6 +54,7 @@ private ApnsConfig(Builder builder) {
5154
.put("aps", builder.aps.getFields())
5255
.build();
5356
this.fcmOptions = builder.fcmOptions;
57+
this.liveActivityToken = builder.liveActivityToken;
5458
}
5559

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

7277
private Builder() {}
7378

@@ -137,6 +142,17 @@ public Builder setFcmOptions(ApnsFcmOptions apnsFcmOptions) {
137142
return this;
138143
}
139144

145+
/**
146+
* Sets the Live Activity token.
147+
*
148+
* @param liveActivityToken Live Activity token.
149+
* @return This builder.
150+
*/
151+
public Builder setLiveActivityToken(String liveActivityToken) {
152+
this.liveActivityToken = liveActivityToken;
153+
return this;
154+
}
155+
140156
/**
141157
* Creates a new {@link ApnsConfig} instance from the parameters set on this builder.
142158
*

src/test/java/com/google/firebase/messaging/FirebaseMessagingIT.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ public void testSend() throws Exception {
6363
.setBody("Body")
6464
.build())
6565
.build())
66+
.setLiveActivityToken("integration-test-live-activity-token")
6667
.build())
6768
.setWebpushConfig(WebpushConfig.builder()
6869
.putHeader("X-Custom-Val", "Foo")

src/test/java/com/google/firebase/messaging/MessageTest.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,7 @@ public void testApnsMessageWithPayload() throws IOException {
411411
.putCustomData("cd1", "cd-v1")
412412
.putAllCustomData(ImmutableMap.<String, Object>of("cd2", "cd-v2", "cd3", true))
413413
.setAps(Aps.builder().build())
414+
.setLiveActivityToken("test-live-activity-token")
414415
.build())
415416
.setTopic("test-topic")
416417
.build();
@@ -421,10 +422,11 @@ public void testApnsMessageWithPayload() throws IOException {
421422
.put("cd3", true)
422423
.put("aps", ImmutableMap.of())
423424
.build();
424-
Map<String, Object> data = ImmutableMap.<String, Object>of(
425-
"headers", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"),
426-
"payload", payload
427-
);
425+
Map<String, Object> data = ImmutableMap.<String, Object>builder()
426+
.put("headers", ImmutableMap.of("k1", "v1", "k2", "v2", "k3", "v3"))
427+
.put("payload", payload)
428+
.put("live_activity_token", "test-live-activity-token")
429+
.build();
428430
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "apns", data), message);
429431
}
430432

@@ -442,6 +444,7 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
442444
.setSound("test-sound")
443445
.setThreadId("test-thread-id")
444446
.build())
447+
.setLiveActivityToken("test-live-activity-token-aps")
445448
.build())
446449
.setTopic("test-topic")
447450
.build();
@@ -459,7 +462,10 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
459462
assertJsonEquals(
460463
ImmutableMap.of(
461464
"topic", "test-topic",
462-
"apns", ImmutableMap.<String, Object>of("payload", payload)),
465+
"apns", ImmutableMap.<String, Object>builder()
466+
.put("payload", payload)
467+
.put("live_activity_token", "test-live-activity-token-aps")
468+
.build()),
463469
message);
464470

465471
message = Message.builder()
@@ -825,6 +831,7 @@ public void testImageInApnsNotification() throws IOException {
825831
.setApnsConfig(
826832
ApnsConfig.builder().setAps(Aps.builder().build())
827833
.setFcmOptions(ApnsFcmOptions.builder().setImage(TEST_IMAGE_URL_APNS).build())
834+
.setLiveActivityToken("test-live-activity-token-image")
828835
.build()).build();
829836

830837
ImmutableMap<String, Object> notification =
@@ -837,6 +844,7 @@ public void testImageInApnsNotification() throws IOException {
837844
ImmutableMap.<String, Object>builder()
838845
.put("fcm_options", ImmutableMap.of("image", TEST_IMAGE_URL_APNS))
839846
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
847+
.put("live_activity_token", "test-live-activity-token-image")
840848
.build();
841849
ImmutableMap<String, Object> expected =
842850
ImmutableMap.<String, Object>builder()
@@ -847,6 +855,34 @@ public void testImageInApnsNotification() throws IOException {
847855
assertJsonEquals(expected, message);
848856
}
849857

858+
@Test
859+
public void testApnsMessageWithOnlyLiveActivityToken() throws IOException {
860+
Message message = Message.builder()
861+
.setApnsConfig(ApnsConfig.builder()
862+
.setAps(Aps.builder().build())
863+
.setLiveActivityToken("only-live-activity")
864+
.build())
865+
.setTopic("test-topic")
866+
.build();
867+
Map<String, Object> expectedApns = ImmutableMap.<String, Object>builder()
868+
.put("live_activity_token", "only-live-activity")
869+
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
870+
.build();
871+
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "apns", expectedApns), message);
872+
873+
// Test without live activity token
874+
message = Message.builder()
875+
.setApnsConfig(ApnsConfig.builder()
876+
.setAps(Aps.builder().build())
877+
.build())
878+
.setTopic("test-topic")
879+
.build();
880+
expectedApns = ImmutableMap.<String, Object>builder()
881+
.put("payload", ImmutableMap.of("aps", ImmutableMap.of()))
882+
.build();
883+
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "apns", expectedApns), message);
884+
}
885+
850886
@Test
851887
public void testInvalidColorInAndroidNotificationLightSettings() {
852888
try {

0 commit comments

Comments
 (0)