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 subtitle in ApsAlert #219

Merged
merged 8 commits into from
Nov 27, 2018
68 changes: 68 additions & 0 deletions src/main/java/com/google/firebase/messaging/ApsAlert.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -49,6 +52,12 @@ public class ApsAlert {
@Key("title-loc-args")
private final List<String> titleLocArgs;

@Key("subtitle-loc-key")
private final String subtitleLocKey;

@Key("subtitle-loc-args")
private final List<String> subtitleLocArgs;

@Key("action-loc-key")
private final String actionLocKey;

Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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<String> locArgs = new ArrayList<>();
private String titleLocKey;
private List<String> titleLocArgs = new ArrayList<>();
private String subtitleLocKey;
private List<String> subtitleLocArgs = new ArrayList<>();
private String actionLocKey;
private String launchImage;

Expand All @@ -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}.
Expand Down Expand Up @@ -209,6 +241,42 @@ public Builder addAllTitleLocArgs(@NonNull List<String> 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<String> args) {
this.subtitleLocArgs.addAll(args);
return this;
}

/**
* Sets the launch image for the notification action.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
.setBadge(42)
.setAlert(ApsAlert.builder()
.setTitle("test-title")
.setSubtitle("test-subtitle")
.setBody("test-body")
.build())
.build())
Expand All @@ -657,7 +658,8 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
"payload", ImmutableMap.of("k1", "v1", "k2", true,
"aps", ImmutableMap.<String, Object>of("badge", new BigDecimal(42),
"alert", ImmutableMap.<String, Object>of(
"title", "test-title", "body", "test-body"))))
"title", "test-title", "subtitle", "test-subtitle",
"body", "test-body"))))
));

// Webpush message (no notification)
Expand Down
10 changes: 9 additions & 1 deletion src/test/java/com/google/firebase/messaging/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -417,12 +421,15 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
"aps", ImmutableMap.<String, Object>builder()
.put("alert", ImmutableMap.<String, Object>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"))
chemidy marked this conversation as resolved.
Show resolved Hide resolved
.put("launch-image", "test-image")
.build())
.put("category", "test-category")
Expand Down Expand Up @@ -474,7 +481,8 @@ public void testInvalidApnsConfig() {

List<ApsAlert.Builder> 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 {
Expand Down