From a4de76c887bb245f0dba050c5dbfd35fa6d15de4 Mon Sep 17 00:00:00 2001 From: Adrian Regan Date: Mon, 9 Sep 2019 12:30:26 +0100 Subject: [PATCH 1/9] #308 Added Notification Count to AndroidNotification --- .../messaging/AndroidNotification.java | 30 ++++++++++++++++++- .../firebase/messaging/MessageTest.java | 5 ++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index ef286d499..feef275ec 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -70,6 +70,9 @@ public class AndroidNotification { @Key("image") private final String image; + @Key("notification_count") + private final Integer notificationCount; + private AndroidNotification(Builder builder) { this.title = builder.title; this.body = builder.body; @@ -101,6 +104,7 @@ private AndroidNotification(Builder builder) { } this.channelId = builder.channelId; this.image = builder.image; + this.notificationCount = builder.notificationCount; } /** @@ -127,7 +131,8 @@ public static class Builder { private List titleLocArgs = new ArrayList<>(); private String channelId; private String image; - + private Integer notificationCount; + private Builder() {} /** @@ -309,6 +314,29 @@ public Builder setImage(String imageUrl) { return this; } + /** + * Sets the number of items this notification represents. + * May be displayed as a badge count for launchers that support badging. + * + *

Accepts null values and any negative value supplied will be set to ZERO + * + * @see + * Android Notification Spec + * + * @param notificationCount the badge count null=unaffected, zero=reset, positive=count + * @return This builder + */ + public Builder setNotificationCount(Integer notificationCount) { + // + // If not null then must be zero or positive + // + this.notificationCount = notificationCount != null + ? (notificationCount >= 0 ? notificationCount : 0) + : null; + return this; + } + /** * Creates a new {@link AndroidNotification} instance from the parameters set on this builder. * diff --git a/src/test/java/com/google/firebase/messaging/MessageTest.java b/src/test/java/com/google/firebase/messaging/MessageTest.java index 2db28d5dc..716969450 100644 --- a/src/test/java/com/google/firebase/messaging/MessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MessageTest.java @@ -153,6 +153,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") @@ -170,6 +171,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", From c04039ef74d166f356ff57c77fa299d298701530 Mon Sep 17 00:00:00 2001 From: Adrian Regan Date: Mon, 9 Sep 2019 20:59:16 +0100 Subject: [PATCH 2/9] Moved Validation to Constructor --- .../google/firebase/messaging/AndroidNotification.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index feef275ec..0b16a942a 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -104,6 +104,11 @@ private AndroidNotification(Builder builder) { } this.channelId = builder.channelId; this.image = builder.image; + + if (builder.notificationCount != null) { + checkArgument(builder.notificationCount >= 0, + "notificationCount if specified must be zero or positive valued"); + } this.notificationCount = builder.notificationCount; } @@ -331,9 +336,7 @@ public Builder setNotificationCount(Integer notificationCount) { // // If not null then must be zero or positive // - this.notificationCount = notificationCount != null - ? (notificationCount >= 0 ? notificationCount : 0) - : null; + this.notificationCount = notificationCount; return this; } From 79805d26696f1159d3514879e4009d19f51b07d7 Mon Sep 17 00:00:00 2001 From: Adrian Regan Date: Tue, 10 Sep 2019 09:23:05 +0100 Subject: [PATCH 3/9] Set Notification Count Builder as Primative int --- .../google/firebase/messaging/AndroidNotification.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index 0b16a942a..07d9757cc 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -110,6 +110,7 @@ private AndroidNotification(Builder builder) { "notificationCount if specified must be zero or positive valued"); } this.notificationCount = builder.notificationCount; + } /** @@ -323,19 +324,16 @@ public Builder setImage(String imageUrl) { * Sets the number of items this notification represents. * May be displayed as a badge count for launchers that support badging. * - *

Accepts null values and any negative value supplied will be set to ZERO + *

If not invoked then notification count is left unchanged * * @see * Android Notification Spec * - * @param notificationCount the badge count null=unaffected, zero=reset, positive=count + * @param notificationCount 0=reset, positive=count * @return This builder */ - public Builder setNotificationCount(Integer notificationCount) { - // - // If not null then must be zero or positive - // + public Builder setNotificationCount(int notificationCount) { this.notificationCount = notificationCount; return this; } From b073839a50ae7a8701cb5b4d2262fceaf8857178 Mon Sep 17 00:00:00 2001 From: Adrian Regan Date: Tue, 10 Sep 2019 19:53:57 +0100 Subject: [PATCH 4/9] Comment Formatting. Additional Negative Test --- .../google/firebase/messaging/AndroidNotification.java | 8 ++++---- .../java/com/google/firebase/messaging/MessageTest.java | 5 +++++ 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index 07d9757cc..075cc6ad0 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -321,17 +321,17 @@ public Builder setImage(String imageUrl) { } /** - * Sets the number of items this notification represents. + * Sets the number of items this notification represents. * May be displayed as a badge count for launchers that support badging. * - *

If not invoked then notification count is left unchanged + *

If not invoked then notification count is left unchanged. * * @see * Android Notification Spec * - * @param notificationCount 0=reset, positive=count - * @return This builder + * @param notificationCount Zero or positive value. Zero indicates leave unchanged. + * @return This builder. */ public Builder setNotificationCount(int notificationCount) { this.notificationCount = notificationCount; diff --git a/src/test/java/com/google/firebase/messaging/MessageTest.java b/src/test/java/com/google/firebase/messaging/MessageTest.java index 716969450..6723739a8 100644 --- a/src/test/java/com/google/firebase/messaging/MessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MessageTest.java @@ -186,6 +186,11 @@ public void testAndroidMessageWithNotification() throws IOException { assertJsonEquals(ImmutableMap.of("topic", "test-topic", "android", data), message); } + @Test(expected = IllegalArgumentException.class) + public void testAndroidMessageWithNegativeNotificationCount() throws IllegalArgumentException { + AndroidNotification.builder().setNotificationCount(-1).build(); + } + @Test public void testAndroidMessageWithoutLocalization() throws IOException { Message message = Message.builder() From df458eddf54b0f00674442f42749e7b6bf558bcf Mon Sep 17 00:00:00 2001 From: Adrian Regan Date: Thu, 12 Sep 2019 17:14:54 +0100 Subject: [PATCH 5/9] More accurate test method name for AndroidNotification count --- src/test/java/com/google/firebase/messaging/MessageTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/google/firebase/messaging/MessageTest.java b/src/test/java/com/google/firebase/messaging/MessageTest.java index 6723739a8..fe441b4fd 100644 --- a/src/test/java/com/google/firebase/messaging/MessageTest.java +++ b/src/test/java/com/google/firebase/messaging/MessageTest.java @@ -187,7 +187,7 @@ public void testAndroidMessageWithNotification() throws IOException { } @Test(expected = IllegalArgumentException.class) - public void testAndroidMessageWithNegativeNotificationCount() throws IllegalArgumentException { + public void testAndroidNotificationWithNegativeCount() throws IllegalArgumentException { AndroidNotification.builder().setNotificationCount(-1).build(); } From c19a3fa7a00143aa5397fda49c510018230b9d2d Mon Sep 17 00:00:00 2001 From: chong-shao <31256040+chong-shao@users.noreply.github.com> Date: Mon, 28 Oct 2019 22:53:26 -0700 Subject: [PATCH 6/9] Fix format issues --- .../com/google/firebase/messaging/AndroidNotification.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index c8023f55d..aff7a39ee 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -588,13 +588,13 @@ 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. - *

If not invoked then notification count is left unchanged. + * + * If not invoked then notification count is left unchanged. * * @see * Android Notification Spec - * - For example, this might be useful if you're + * 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 From 7c50507aedfc3d0b79458400d1b753e796f29082 Mon Sep 17 00:00:00 2001 From: chong-shao <31256040+chong-shao@users.noreply.github.com> Date: Mon, 28 Oct 2019 23:03:53 -0700 Subject: [PATCH 7/9] Fix formatting issues --- .../java/com/google/firebase/messaging/AndroidNotification.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index aff7a39ee..ffe56384a 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -588,7 +588,6 @@ 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. - * * If not invoked then notification count is left unchanged. * * @see Date: Mon, 28 Oct 2019 23:13:00 -0700 Subject: [PATCH 8/9] Fix formatting issue --- .../firebase/messaging/AndroidNotification.java | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index ffe56384a..08c1be62e 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -589,14 +589,10 @@ 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. * If not invoked then notification count is left unchanged. - * - * @see - * Android Notification Spec - * 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 + * 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 Zero or positive value. Zero indicates leave unchanged. From 87621fef07a456bfe36d38792141beeecd85ff3f Mon Sep 17 00:00:00 2001 From: chong-shao <31256040+chong-shao@users.noreply.github.com> Date: Mon, 28 Oct 2019 23:18:19 -0700 Subject: [PATCH 9/9] Fix build issue --- .../com/google/firebase/messaging/AndroidNotification.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/com/google/firebase/messaging/AndroidNotification.java b/src/main/java/com/google/firebase/messaging/AndroidNotification.java index 08c1be62e..edfda0d95 100644 --- a/src/main/java/com/google/firebase/messaging/AndroidNotification.java +++ b/src/main/java/com/google/firebase/messaging/AndroidNotification.java @@ -119,9 +119,6 @@ public class AndroidNotification { .put(Priority.HIGH, "PRIORITY_HIGH") .put(Priority.MAX, "PRIORITY_MAX") .build(); - - @Key("notification_count") - private final Integer notificationCount; private AndroidNotification(Builder builder) { this.title = builder.title;