Skip to content

Commit e918da8

Browse files
knocknareachong-shao
authored andcommitted
feat(fcm): Updated code for setting Notification Count parameter in AndroidNotification class (#309)
* Enforce positive integer for the notification count parameter * Update code comments
1 parent 76f0f95 commit e918da8

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

‎src/main/java/com/google/firebase/messaging/AndroidNotification.java

+14-8
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public class AndroidNotification {
119119
.put(Priority.HIGH, "PRIORITY_HIGH")
120120
.put(Priority.MAX, "PRIORITY_MAX")
121121
.build();
122-
122+
123123
private AndroidNotification(Builder builder) {
124124
this.title = builder.title;
125125
this.body = builder.body;
@@ -150,7 +150,7 @@ private AndroidNotification(Builder builder) {
150150
this.titleLocArgs = null;
151151
}
152152
this.channelId = builder.channelId;
153-
this.image = builder.image;
153+
this.image = builder.image;
154154
this.ticker = builder.ticker;
155155
this.sticky = builder.sticky;
156156
this.eventTime = builder.eventTime;
@@ -174,6 +174,10 @@ private AndroidNotification(Builder builder) {
174174
} else {
175175
this.visibility = null;
176176
}
177+
if (builder.notificationCount != null) {
178+
checkArgument(builder.notificationCount >= 0,
179+
"notificationCount if specified must be zero or positive valued");
180+
}
177181
this.notificationCount = builder.notificationCount;
178182
}
179183

@@ -220,6 +224,7 @@ public static class Builder {
220224
private List<String> titleLocArgs = new ArrayList<>();
221225
private String channelId;
222226
private String image;
227+
private Integer notificationCount;
223228
private String ticker;
224229
private Boolean sticky;
225230
private String eventTime;
@@ -231,7 +236,6 @@ public static class Builder {
231236
private LightSettings lightSettings;
232237
private Boolean defaultLightSettings;
233238
private Visibility visibility;
234-
private Integer notificationCount;
235239

236240
private Builder() {}
237241

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

581585
/**
582586
* Sets the number of items this notification represents. May be displayed as a badge
583-
* count for launchers that support badging. For example, this might be useful if you're
584-
* using just one notification to represent multiple new messages but you want the count
585-
* here to represent the number of total new messages. If zero or unspecified, systems
586-
* that support badging use the default, which is to increment a number displayed on
587+
* count for launchers that support badging.
588+
* If not invoked then notification count is left unchanged.
589+
* For example, this might be useful if you're using just one notification to represent
590+
* multiple new messages but you want the count here to represent the number of total
591+
* new messages. If zero or unspecified, systems that support badging use the default,
592+
* which is to increment a number displayed on
587593
* the long-press menu each time a new notification arrives.
588594
*
589-
* @param notificationCount The notification count
595+
* @param notificationCount Zero or positive value. Zero indicates leave unchanged.
590596
* @return This builder.
591597
*/
592598
public Builder setNotificationCount(int notificationCount) {

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

+10
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ public void testAndroidMessageWithNotification() throws IOException {
167167
.addBodyLocalizationArg("body-arg1")
168168
.addAllBodyLocalizationArgs(ImmutableList.of("body-arg2", "body-arg3"))
169169
.setChannelId("channel-id")
170+
.setNotificationCount(4)
170171
.build())
171172
.build())
172173
.setTopic("test-topic")
@@ -184,6 +185,10 @@ public void testAndroidMessageWithNotification() throws IOException {
184185
.put("body_loc_key", "body-loc")
185186
.put("body_loc_args", ImmutableList.of("body-arg1", "body-arg2", "body-arg3"))
186187
.put("channel_id", "channel-id")
188+
// There is a problem with the JsonParser assignment to BigDecimal takes priority over
189+
// all other number types and so this integer value is interpreted as a BigDecimal
190+
// rather than an Integer.
191+
.put("notification_count", BigDecimal.valueOf(4L))
187192
.build();
188193
Map<String, Object> data = ImmutableMap.of(
189194
"collapse_key", "test-key",
@@ -195,6 +200,11 @@ public void testAndroidMessageWithNotification() throws IOException {
195200
assertJsonEquals(ImmutableMap.of("topic", "test-topic", "android", data), message);
196201
}
197202

203+
@Test(expected = IllegalArgumentException.class)
204+
public void testAndroidNotificationWithNegativeCount() throws IllegalArgumentException {
205+
AndroidNotification.builder().setNotificationCount(-1).build();
206+
}
207+
198208
@Test
199209
public void testAndroidMessageWithoutLocalization() throws IOException {
200210
Message message = Message.builder()

0 commit comments

Comments
 (0)