Skip to content

Commit 3bade2b

Browse files
chemidyhiranya911
authored andcommitted
Add subtitle in ApsAlert (#219)
* Add subtitle field in ApsAlert payload + tests * fix checkstyle * fix test * fix checkstyle * fix test * fix test * fix test * update test
1 parent 32f05c3 commit 3bade2b

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ public class ApsAlert {
3434
@Key("title")
3535
private final String title;
3636

37+
@Key("subtitle")
38+
private final String subtitle;
39+
3740
@Key("body")
3841
private final String body;
3942

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

55+
@Key("subtitle-loc-key")
56+
private final String subtitleLocKey;
57+
58+
@Key("subtitle-loc-args")
59+
private final List<String> subtitleLocArgs;
60+
5261
@Key("action-loc-key")
5362
private final String actionLocKey;
5463

@@ -57,6 +66,7 @@ public class ApsAlert {
5766

5867
private ApsAlert(Builder builder) {
5968
this.title = builder.title;
69+
this.subtitle = builder.subtitle;
6070
this.body = builder.body;
6171
this.actionLocKey = builder.actionLocKey;
6272
this.locKey = builder.locKey;
@@ -76,6 +86,14 @@ private ApsAlert(Builder builder) {
7686
} else {
7787
this.titleLocArgs = null;
7888
}
89+
this.subtitleLocKey = builder.subtitleLocKey;
90+
if (!builder.subtitleLocArgs.isEmpty()) {
91+
checkArgument(!Strings.isNullOrEmpty(builder.subtitleLocKey),
92+
"subtitleLocKey is required when specifying subtitleLocArgs");
93+
this.subtitleLocArgs = ImmutableList.copyOf(builder.subtitleLocArgs);
94+
} else {
95+
this.subtitleLocArgs = null;
96+
}
7997
this.launchImage = builder.launchImage;
8098
}
8199

@@ -91,11 +109,14 @@ public static Builder builder() {
91109
public static class Builder {
92110

93111
private String title;
112+
private String subtitle;
94113
private String body;
95114
private String locKey;
96115
private List<String> locArgs = new ArrayList<>();
97116
private String titleLocKey;
98117
private List<String> titleLocArgs = new ArrayList<>();
118+
private String subtitleLocKey;
119+
private List<String> subtitleLocArgs = new ArrayList<>();
99120
private String actionLocKey;
100121
private String launchImage;
101122

@@ -113,6 +134,17 @@ public Builder setTitle(String title) {
113134
return this;
114135
}
115136

137+
/**
138+
* Sets the subtitle of the alert.
139+
*
140+
* @param subtitle Subtitle of the notification.
141+
* @return This builder.
142+
*/
143+
public Builder setSubtitle(String subtitle) {
144+
this.subtitle = subtitle;
145+
return this;
146+
}
147+
116148
/**
117149
* Sets the body of the alert. When provided, overrides the body sent
118150
* via {@link Notification}.
@@ -209,6 +241,42 @@ public Builder addAllTitleLocArgs(@NonNull List<String> args) {
209241
return this;
210242
}
211243

244+
/**
245+
* Sets the key of the subtitle string in the app's string resources to use to localize
246+
* the subtitle text.
247+
*
248+
* @param subtitleLocKey Resource key string.
249+
* @return This builder.
250+
*/
251+
public Builder setSubtitleLocalizationKey(String subtitleLocKey) {
252+
this.subtitleLocKey = subtitleLocKey;
253+
return this;
254+
}
255+
256+
/**
257+
* Adds a resource key string that will be used in place of the format specifiers in
258+
* {@code subtitleLocKey}.
259+
*
260+
* @param arg Resource key string.
261+
* @return This builder.
262+
*/
263+
public Builder addSubtitleLocalizationArg(@NonNull String arg) {
264+
this.subtitleLocArgs.add(arg);
265+
return this;
266+
}
267+
268+
/**
269+
* Adds a list of resource keys that will be used in place of the format specifiers in
270+
* {@code subtitleLocKey}.
271+
*
272+
* @param args List of resource key strings.
273+
* @return This builder.
274+
*/
275+
public Builder addAllSubtitleLocArgs(@NonNull List<String> args) {
276+
this.subtitleLocArgs.addAll(args);
277+
return this;
278+
}
279+
212280
/**
213281
* Sets the launch image for the notification action.
214282
*

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
644644
.setBadge(42)
645645
.setAlert(ApsAlert.builder()
646646
.setTitle("test-title")
647+
.setSubtitle("test-subtitle")
647648
.setBody("test-body")
648649
.build())
649650
.build())
@@ -657,7 +658,8 @@ private static Map<Message, Map<String, Object>> buildTestMessages() {
657658
"payload", ImmutableMap.of("k1", "v1", "k2", true,
658659
"aps", ImmutableMap.<String, Object>of("badge", new BigDecimal(42),
659660
"alert", ImmutableMap.<String, Object>of(
660-
"title", "test-title", "body", "test-body"))))
661+
"title", "test-title", "subtitle", "test-subtitle",
662+
"body", "test-body"))))
661663
));
662664

663665
// Webpush message (no notification)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,14 +393,18 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
393393
.setAps(Aps.builder()
394394
.setAlert(ApsAlert.builder()
395395
.setTitle("test-title")
396+
.setSubtitle("test-subtitle")
396397
.setBody("test-body")
397398
.setLocalizationKey("test-loc-key")
398399
.setActionLocalizationKey("test-action-loc-key")
399400
.setTitleLocalizationKey("test-title-loc-key")
401+
.setSubtitleLocalizationKey("test-subtitle-loc-key")
400402
.addLocalizationArg("arg1")
401403
.addAllLocalizationArgs(ImmutableList.of("arg2", "arg3"))
402404
.addTitleLocalizationArg("arg4")
403405
.addAllTitleLocArgs(ImmutableList.of("arg5", "arg6"))
406+
.addSubtitleLocalizationArg("arg7")
407+
.addAllSubtitleLocArgs(ImmutableList.of("arg8", "arg9"))
404408
.setLaunchImage("test-image")
405409
.build())
406410
.setCategory("test-category")
@@ -417,12 +421,15 @@ public void testApnsMessageWithPayloadAndAps() throws IOException {
417421
"aps", ImmutableMap.<String, Object>builder()
418422
.put("alert", ImmutableMap.<String, Object>builder()
419423
.put("title", "test-title")
424+
.put("subtitle", "test-subtitle")
420425
.put("body", "test-body")
421426
.put("loc-key", "test-loc-key")
422427
.put("action-loc-key", "test-action-loc-key")
423428
.put("title-loc-key", "test-title-loc-key")
429+
.put("subtitle-loc-key", "test-subtitle-loc-key")
424430
.put("loc-args", ImmutableList.of("arg1", "arg2", "arg3"))
425431
.put("title-loc-args", ImmutableList.of("arg4", "arg5", "arg6"))
432+
.put("subtitle-loc-args", ImmutableList.of("arg7", "arg8", "arg9"))
426433
.put("launch-image", "test-image")
427434
.build())
428435
.put("category", "test-category")
@@ -474,7 +481,8 @@ public void testInvalidApnsConfig() {
474481

475482
List<ApsAlert.Builder> notificationBuilders = ImmutableList.of(
476483
ApsAlert.builder().addLocalizationArg("foo"),
477-
ApsAlert.builder().addTitleLocalizationArg("foo")
484+
ApsAlert.builder().addTitleLocalizationArg("foo"),
485+
ApsAlert.builder().addSubtitleLocalizationArg("foo")
478486
);
479487
for (int i = 0; i < notificationBuilders.size(); i++) {
480488
try {

0 commit comments

Comments
 (0)