Skip to content

Commit 69799bf

Browse files
author
Javen
committed
if not set, ios badge is set 1 by default. Add disableBadge method.
1 parent 4dcdf4b commit 69799bf

File tree

3 files changed

+41
-8
lines changed

3 files changed

+41
-8
lines changed

src/cn/jpush/api/push/model/notification/IosNotification.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,21 @@ public class IosNotification extends PlatformNotification {
1010
private static final String NOTIFICATION_IOS = "ios";
1111

1212
private static final String DEFAULT_SOUND = "";
13+
private static final int BADGE_UNDEFINED = -1;
14+
private static final int DEFAULT_BADGE = 1;
1315

1416
private static final String BADGE = "badge";
1517
private static final String SOUND = "sound";
1618
private static final String CONTENT_AVAILABLE = "content-available";
1719

1820
private final boolean soundDisabled;
21+
private final boolean badgeDisabled;
1922
private final String sound;
2023
private final int badge;
2124
private final boolean contentAvailable;
2225

2326
private IosNotification(String alert, String sound, int badge,
24-
boolean contentAvailable, boolean soundDisabled,
27+
boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
2528
ImmutableMap<String, String> extras,
2629
ImmutableMap<String, Number> numberExtras,
2730
ImmutableMap<String, Boolean> booleanExtras) {
@@ -31,6 +34,7 @@ private IosNotification(String alert, String sound, int badge,
3134
this.badge = badge;
3235
this.contentAvailable = contentAvailable;
3336
this.soundDisabled = soundDisabled;
37+
this.badgeDisabled = badgeDisabled;
3438
}
3539

3640
public static Builder newBuilder() {
@@ -51,8 +55,12 @@ public String getPlatform() {
5155
public JsonElement toJSON() {
5256
JsonObject json = super.toJSON().getAsJsonObject();
5357

54-
if (badge >= 0) {
55-
json.add(BADGE, new JsonPrimitive(this.badge));
58+
if (!badgeDisabled) {
59+
if (badge >= 0) {
60+
json.add(BADGE, new JsonPrimitive(this.badge));
61+
} else if (badge == BADGE_UNDEFINED) {
62+
json.add(BADGE, new JsonPrimitive(DEFAULT_BADGE));
63+
}
5664
}
5765
if (!soundDisabled) {
5866
if (null != sound) {
@@ -71,9 +79,10 @@ public JsonElement toJSON() {
7179

7280
public static class Builder extends PlatformNotification.Builder<IosNotification> {
7381
private String sound;
74-
private int badge = -1;
82+
private int badge = BADGE_UNDEFINED;
7583
private boolean contentAvailable = false;
7684
private boolean soundDisabled = false;
85+
private boolean badgeDisabled = false;
7786

7887
public Builder setSound(String sound) {
7988
this.sound = sound;
@@ -90,6 +99,11 @@ public Builder setBadge(int badge) {
9099
return this;
91100
}
92101

102+
public Builder disableBadge() {
103+
this.badgeDisabled = true;
104+
return this;
105+
}
106+
93107
public Builder setContentAvailable(boolean contentAvailable) {
94108
this.contentAvailable = contentAvailable;
95109
return this;
@@ -128,7 +142,8 @@ public Builder addExtra(String key, Boolean value) {
128142
}
129143

130144
public IosNotification build() {
131-
return new IosNotification(alert, sound, badge, contentAvailable, soundDisabled,
145+
return new IosNotification(alert, sound, badge, contentAvailable,
146+
soundDisabled, badgeDisabled,
132147
(null == extrasBuilder) ? null : extrasBuilder.build(),
133148
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
134149
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());

test/cn/jpush/api/push/model/notification/IosNotificationTests.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ public void testEmpty() {
1313
IosNotification ios = IosNotification.newBuilder().build();
1414
JsonObject json = new JsonObject();
1515
json.add("sound", new JsonPrimitive(""));
16+
json.add("badge", new JsonPrimitive(1));
1617
Assert.assertEquals("", json, ios.toJSON());
1718
}
1819

@@ -22,6 +23,7 @@ public void testQuickAlert() {
2223
JsonObject json = new JsonObject();
2324
json.add("alert", new JsonPrimitive("aaa"));
2425
json.add("sound", new JsonPrimitive(""));
26+
json.add("badge", new JsonPrimitive(1));
2527
Assert.assertEquals("", json, ios.toJSON());
2628
}
2729

@@ -35,10 +37,10 @@ public void testBadge_0() {
3537
}
3638

3739
@Test
38-
public void testBadge_1() {
39-
IosNotification ios = IosNotification.newBuilder().setBadge(1).build();
40+
public void testBadge_2() {
41+
IosNotification ios = IosNotification.newBuilder().setBadge(2).build();
4042
JsonObject json = new JsonObject();
41-
json.add("badge", new JsonPrimitive(1));
43+
json.add("badge", new JsonPrimitive(2));
4244
json.add("sound", new JsonPrimitive(""));
4345
Assert.assertEquals("", json, ios.toJSON());
4446
}
@@ -48,6 +50,7 @@ public void testSound() {
4850
IosNotification ios = IosNotification.newBuilder().setSound("sound").build();
4951
JsonObject json = new JsonObject();
5052
json.add("sound", new JsonPrimitive("sound"));
53+
json.add("badge", new JsonPrimitive(1));
5154
Assert.assertEquals("", json, ios.toJSON());
5255
}
5356

@@ -60,6 +63,19 @@ public void testSoundDisabled() {
6063
.build();
6164
JsonObject json = new JsonObject();
6265
json.add("alert", new JsonPrimitive("alert"));
66+
json.add("badge", new JsonPrimitive(1));
67+
Assert.assertEquals("", json, ios.toJSON());
68+
}
69+
70+
@Test
71+
public void testBadgeDisabled() {
72+
IosNotification ios = IosNotification.newBuilder()
73+
.disableBadge()
74+
.setAlert("alert")
75+
.build();
76+
JsonObject json = new JsonObject();
77+
json.add("alert", new JsonPrimitive("alert"));
78+
json.add("sound", new JsonPrimitive(""));
6379
Assert.assertEquals("", json, ios.toJSON());
6480
}
6581

@@ -75,6 +91,7 @@ public void testExtra() {
7591
extra.add("key2", new JsonPrimitive(Boolean.TRUE));
7692
json.add("extras", extra);
7793
json.add("sound", new JsonPrimitive(""));
94+
json.add("badge", new JsonPrimitive(1));
7895
Assert.assertEquals("", json, ios.toJSON());
7996
}
8097

test/cn/jpush/api/push/model/notification/NotificationTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public void testAlertIos() {
5252
JsonObject ios = new JsonObject();
5353
ios.add("alert", new JsonPrimitive("alert"));
5454
ios.add("sound", new JsonPrimitive(""));
55+
ios.add("badge", new JsonPrimitive(1));
5556
json.add("ios", ios);
5657
Assert.assertEquals("", json, notification.toJSON());
5758
}

0 commit comments

Comments
 (0)