diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index adb84c638..edfda0d95 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -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; @@ -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; @@ -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; } @@ -220,6 +224,7 @@ public static class Builder { private List titleLocArgs = new ArrayList<>(); private String channelId; private String image; + private Integer notificationCount; private String ticker; private Boolean sticky; private String eventTime; @@ -231,7 +236,6 @@ public static class Builder { private LightSettings lightSettings; private Boolean defaultLightSettings; private Visibility visibility; - private Integer notificationCount; private Builder() {} @@ -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) { diff --git a/src/test/java/com/google/firebase/messaging/MessageTest.java b/src/test/java/com/google/firebase/messaging/MessageTest.java index 7c8f528df..f87b40f22 100644 --- a/src/test/java/com/google/firebase/messaging/MessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MessageTest.java @@ -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") @@ -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 data = ImmutableMap.of( "collapse_key", "test-key", @@ -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()