diff --git a/src/main/java/com/google/firebase/messaging/ApsAlert.java b/src/main/java/com/google/firebase/messaging/ApsAlert.java index 6f7e249f3..2efa94433 100644 --- a/src/main/java/com/google/firebase/messaging/ApsAlert.java +++ b/src/main/java/com/google/firebase/messaging/ApsAlert.java @@ -34,6 +34,9 @@ public class ApsAlert { @Key("title") private final String title; + @Key("subtitle") + private final String subtitle; + @Key("body") private final String body; @@ -49,6 +52,12 @@ public class ApsAlert { @Key("title-loc-args") private final List titleLocArgs; + @Key("subtitle-loc-key") + private final String subtitleLocKey; + + @Key("subtitle-loc-args") + private final List subtitleLocArgs; + @Key("action-loc-key") private final String actionLocKey; @@ -57,6 +66,7 @@ public class ApsAlert { private ApsAlert(Builder builder) { this.title = builder.title; + this.subtitle = builder.subtitle; this.body = builder.body; this.actionLocKey = builder.actionLocKey; this.locKey = builder.locKey; @@ -76,6 +86,14 @@ private ApsAlert(Builder builder) { } else { this.titleLocArgs = null; } + this.subtitleLocKey = builder.subtitleLocKey; + if (!builder.subtitleLocArgs.isEmpty()) { + checkArgument(!Strings.isNullOrEmpty(builder.subtitleLocKey), + "subtitleLocKey is required when specifying subtitleLocArgs"); + this.subtitleLocArgs = ImmutableList.copyOf(builder.subtitleLocArgs); + } else { + this.subtitleLocArgs = null; + } this.launchImage = builder.launchImage; } @@ -91,11 +109,14 @@ public static Builder builder() { public static class Builder { private String title; + private String subtitle; private String body; private String locKey; private List locArgs = new ArrayList<>(); private String titleLocKey; private List titleLocArgs = new ArrayList<>(); + private String subtitleLocKey; + private List subtitleLocArgs = new ArrayList<>(); private String actionLocKey; private String launchImage; @@ -113,6 +134,17 @@ public Builder setTitle(String title) { return this; } + /** + * Sets the subtitle of the alert. + * + * @param subtitle Subtitle of the notification. + * @return This builder. + */ + public Builder setSubtitle(String subtitle) { + this.subtitle = subtitle; + return this; + } + /** * Sets the body of the alert. When provided, overrides the body sent * via {@link Notification}. @@ -209,6 +241,42 @@ public Builder addAllTitleLocArgs(@NonNull List args) { return this; } + /** + * Sets the key of the subtitle string in the app's string resources to use to localize + * the subtitle text. + * + * @param subtitleLocKey Resource key string. + * @return This builder. + */ + public Builder setSubtitleLocalizationKey(String subtitleLocKey) { + this.subtitleLocKey = subtitleLocKey; + return this; + } + + /** + * Adds a resource key string that will be used in place of the format specifiers in + * {@code subtitleLocKey}. + * + * @param arg Resource key string. + * @return This builder. + */ + public Builder addSubtitleLocalizationArg(@NonNull String arg) { + this.subtitleLocArgs.add(arg); + return this; + } + + /** + * Adds a list of resource keys that will be used in place of the format specifiers in + * {@code subtitleLocKey}. + * + * @param args List of resource key strings. + * @return This builder. + */ + public Builder addAllSubtitleLocArgs(@NonNull List args) { + this.subtitleLocArgs.addAll(args); + return this; + } + /** * Sets the launch image for the notification action. * diff --git a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java index 4b51513be..1ad74ac33 100644 --- a/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java +++ b/src/test/java/com/google/firebase/messaging/FirebaseMessagingTest.java @@ -644,6 +644,7 @@ private static Map> buildTestMessages() { .setBadge(42) .setAlert(ApsAlert.builder() .setTitle("test-title") + .setSubtitle("test-subtitle") .setBody("test-body") .build()) .build()) @@ -657,7 +658,8 @@ private static Map> buildTestMessages() { "payload", ImmutableMap.of("k1", "v1", "k2", true, "aps", ImmutableMap.of("badge", new BigDecimal(42), "alert", ImmutableMap.of( - "title", "test-title", "body", "test-body")))) + "title", "test-title", "subtitle", "test-subtitle", + "body", "test-body")))) )); // Webpush message (no notification) diff --git a/src/test/java/com/google/firebase/messaging/MessageTest.java b/src/test/java/com/google/firebase/messaging/MessageTest.java index 3313f2bb3..b6025c0bb 100644 --- a/src/test/java/com/google/firebase/messaging/MessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MessageTest.java @@ -393,14 +393,18 @@ public void testApnsMessageWithPayloadAndAps() throws IOException { .setAps(Aps.builder() .setAlert(ApsAlert.builder() .setTitle("test-title") + .setSubtitle("test-subtitle") .setBody("test-body") .setLocalizationKey("test-loc-key") .setActionLocalizationKey("test-action-loc-key") .setTitleLocalizationKey("test-title-loc-key") + .setSubtitleLocalizationKey("test-subtitle-loc-key") .addLocalizationArg("arg1") .addAllLocalizationArgs(ImmutableList.of("arg2", "arg3")) .addTitleLocalizationArg("arg4") .addAllTitleLocArgs(ImmutableList.of("arg5", "arg6")) + .addSubtitleLocalizationArg("arg7") + .addAllSubtitleLocArgs(ImmutableList.of("arg8", "arg9")) .setLaunchImage("test-image") .build()) .setCategory("test-category") @@ -417,12 +421,15 @@ public void testApnsMessageWithPayloadAndAps() throws IOException { "aps", ImmutableMap.builder() .put("alert", ImmutableMap.builder() .put("title", "test-title") + .put("subtitle", "test-subtitle") .put("body", "test-body") .put("loc-key", "test-loc-key") .put("action-loc-key", "test-action-loc-key") .put("title-loc-key", "test-title-loc-key") + .put("subtitle-loc-key", "test-subtitle-loc-key") .put("loc-args", ImmutableList.of("arg1", "arg2", "arg3")) .put("title-loc-args", ImmutableList.of("arg4", "arg5", "arg6")) + .put("subtitle-loc-args", ImmutableList.of("arg7", "arg8", "arg9")) .put("launch-image", "test-image") .build()) .put("category", "test-category") @@ -474,7 +481,8 @@ public void testInvalidApnsConfig() { List notificationBuilders = ImmutableList.of( ApsAlert.builder().addLocalizationArg("foo"), - ApsAlert.builder().addTitleLocalizationArg("foo") + ApsAlert.builder().addTitleLocalizationArg("foo"), + ApsAlert.builder().addSubtitleLocalizationArg("foo") ); for (int i = 0; i < notificationBuilders.size(); i++) { try {