Skip to content
Merged
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public class AndroidNotification {
.put(Priority.HIGH, "PRIORITY_HIGH")
.put(Priority.MAX, "PRIORITY_MAX")
.build();

private AndroidNotification(Builder builder) {
this.title = builder.title;
this.body = builder.body;
Expand Down Expand Up @@ -150,7 +150,7 @@ private AndroidNotification(Builder builder) {
this.titleLocArgs = null;
}
this.channelId = builder.channelId;
this.image = builder.image;
this.image = builder.image;
this.ticker = builder.ticker;
this.sticky = builder.sticky;
this.eventTime = builder.eventTime;
Expand All @@ -174,6 +174,10 @@ private AndroidNotification(Builder builder) {
} else {
this.visibility = null;
}
if (builder.notificationCount != null) {
checkArgument(builder.notificationCount >= 0,
"notificationCount if specified must be zero or positive valued");
}
this.notificationCount = builder.notificationCount;
}

Expand Down Expand Up @@ -220,6 +224,7 @@ public static class Builder {
private List<String> titleLocArgs = new ArrayList<>();
private String channelId;
private String image;
private Integer notificationCount;
private String ticker;
private Boolean sticky;
private String eventTime;
Expand All @@ -231,7 +236,6 @@ public static class Builder {
private LightSettings lightSettings;
private Boolean defaultLightSettings;
private Visibility visibility;
private Integer notificationCount;

private Builder() {}

Expand Down Expand Up @@ -580,13 +584,15 @@ public Builder setVisibility(Visibility visibility) {

/**
* Sets the number of items this notification represents. May be displayed as a badge
* count for launchers that support badging. For example, this might be useful if you're
* using just one notification to represent multiple new messages but you want the count
* here to represent the number of total new messages. If zero or unspecified, systems
* that support badging use the default, which is to increment a number displayed on
* count for launchers that support badging.
* If not invoked then notification count is left unchanged.
* For example, this might be useful if you're using just one notification to represent
* multiple new messages but you want the count here to represent the number of total
* new messages. If zero or unspecified, systems that support badging use the default,
* which is to increment a number displayed on
* the long-press menu each time a new notification arrives.
*
* @param notificationCount The notification count
* @param notificationCount Zero or positive value. Zero indicates leave unchanged.
* @return This builder.
*/
public Builder setNotificationCount(int notificationCount) {
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/com/google/firebase/messaging/MessageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ public void testAndroidMessageWithNotification() throws IOException {
.addBodyLocalizationArg("body-arg1")
.addAllBodyLocalizationArgs(ImmutableList.of("body-arg2", "body-arg3"))
.setChannelId("channel-id")
.setNotificationCount(4)
.build())
.build())
.setTopic("test-topic")
Expand All @@ -183,6 +184,10 @@ public void testAndroidMessageWithNotification() throws IOException {
.put("body_loc_key", "body-loc")
.put("body_loc_args", ImmutableList.of("body-arg1", "body-arg2", "body-arg3"))
.put("channel_id", "channel-id")
// There is a problem with the JsonParser assignment to BigDecimal takes priority over
// all other number types and so this integer value is interpreted as a BigDecimal
// rather than an Integer.
.put("notification_count", BigDecimal.valueOf(4L))
.build();
Map<String, Object> data = ImmutableMap.of(
"collapse_key", "test-key",
Expand All @@ -194,6 +199,11 @@ public void testAndroidMessageWithNotification() throws IOException {
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "android", data), message);
}

@Test(expected = IllegalArgumentException.class)
public void testAndroidNotificationWithNegativeCount() throws IllegalArgumentException {
AndroidNotification.builder().setNotificationCount(-1).build();
}

@Test
public void testAndroidMessageWithoutLocalization() throws IOException {
Message message = Message.builder()
Expand Down