Skip to content

Commit e7fb83a

Browse files
author
Javen
committed
Refactor badge +1 API - add incrBadge(int).
1 parent 59c9e94 commit e7fb83a

File tree

6 files changed

+60
-70
lines changed

6 files changed

+60
-70
lines changed

src/cn/jpush/api/common/ServiceHelper.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -17,26 +17,10 @@ public class ServiceHelper {
1717

1818
private static final int MAX_BADGE_NUMBER = 99999;
1919

20-
public static boolean isValidBadgeValue(String badge) {
21-
if (badge.startsWith("+")) {
22-
badge = badge.substring(1);
23-
return isValidIntBadge(badge);
24-
} else if (badge.startsWith("-")) {
25-
badge = badge.substring(1);
26-
return isValidIntBadge(badge);
27-
} else {
28-
return isValidIntBadge(badge);
29-
}
30-
}
3120

32-
private static boolean isValidIntBadge(String badge) {
33-
int intBadge = 0;
34-
try {
35-
intBadge = Integer.parseInt(badge);
36-
if (intBadge >= 0 && intBadge <= MAX_BADGE_NUMBER) {
37-
return true;
38-
}
39-
} catch (NumberFormatException e) {
21+
public static boolean isValidIntBadge(int intBadge) {
22+
if (intBadge >= 0 && intBadge <= MAX_BADGE_NUMBER) {
23+
return true;
4024
}
4125
return false;
4226
}

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

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,23 +101,38 @@ public Builder disableSound() {
101101
return this;
102102
}
103103

104-
public Builder setBadge(String badge) {
105-
// check available badge value
106-
if (!ServiceHelper.isValidBadgeValue(badge)) {
104+
public Builder incrBadge(int badge) {
105+
if (!ServiceHelper.isValidIntBadge(Math.abs(badge))) {
107106
LOG.warn(ALERT_VALID_BADGE);
108107
return this;
109108
}
110109

111-
this.badge = badge;
110+
if (badge == 0) {
111+
LOG.warn("No action for incrBadge(0)");
112+
return this;
113+
114+
} else if (badge > 0) {
115+
this.badge = "+" + badge;
116+
} else {
117+
this.badge = "" + badge;
118+
}
112119
return this;
113120
}
114121

115122
public Builder setBadge(int badge) {
116-
return setBadge(badge + "");
123+
if (!ServiceHelper.isValidIntBadge(badge)) {
124+
LOG.warn(ALERT_VALID_BADGE);
125+
return this;
126+
}
127+
this.badge = "" + badge;
128+
return this;
117129
}
118130

119-
public Builder setBadgeAuto() {
120-
return setBadge(DEFAULT_BADGE);
131+
/**
132+
* equals to: +1
133+
*/
134+
public Builder autoBadge() {
135+
return incrBadge(1);
121136
}
122137

123138
public Builder disableBadge() {

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

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,19 +62,19 @@ public static Notification ios(String alert, Map<String, String> extras) {
6262
/**
6363
* shortcut
6464
*/
65-
public static Notification ios_badgeAuto() {
65+
public static Notification ios_auto_badge() {
6666
return newBuilder()
6767
.addPlatformNotification(IosNotification.newBuilder()
6868
.setAlert("")
69-
.setBadgeAuto()
69+
.autoBadge()
7070
.build())
7171
.build();
7272
}
7373

7474
/**
7575
* shortcut
7676
*/
77-
public static Notification ios_badge(String badge) {
77+
public static Notification ios_set_badge(int badge) {
7878
return newBuilder()
7979
.addPlatformNotification(IosNotification.newBuilder()
8080
.setAlert("")
@@ -83,6 +83,18 @@ public static Notification ios_badge(String badge) {
8383
.build();
8484
}
8585

86+
/**
87+
* shortcut
88+
*/
89+
public static Notification ios_incr_badge(int badge) {
90+
return newBuilder()
91+
.addPlatformNotification(IosNotification.newBuilder()
92+
.setAlert("")
93+
.incrBadge(badge)
94+
.build())
95+
.build();
96+
}
97+
8698
/**
8799
* shortcut
88100
*/

test/cn/jpush/api/common/ServiceHelperTest.java

Lines changed: 0 additions & 39 deletions
This file was deleted.

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

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,33 @@ public void testBadge_2() {
4545
Assert.assertEquals("", json, ios.toJSON());
4646
}
4747

48+
@Test
49+
public void testBadge_auto() {
50+
IosNotification ios = IosNotification.newBuilder().autoBadge().build();
51+
JsonObject json = new JsonObject();
52+
json.add("badge", new JsonPrimitive("+1"));
53+
json.add("sound", new JsonPrimitive(""));
54+
Assert.assertEquals("", json, ios.toJSON());
55+
}
56+
4857
@Test
4958
public void testBadge_plus_2() {
50-
IosNotification ios = IosNotification.newBuilder().setBadge("+2").build();
59+
IosNotification ios = IosNotification.newBuilder().incrBadge(2).build();
5160
JsonObject json = new JsonObject();
5261
json.add("badge", new JsonPrimitive("+2"));
5362
json.add("sound", new JsonPrimitive(""));
5463
Assert.assertEquals("", json, ios.toJSON());
5564
}
5665

66+
@Test
67+
public void testBadge_minus_2() {
68+
IosNotification ios = IosNotification.newBuilder().incrBadge(-2).build();
69+
JsonObject json = new JsonObject();
70+
json.add("badge", new JsonPrimitive("-2"));
71+
json.add("sound", new JsonPrimitive(""));
72+
Assert.assertEquals("", json, ios.toJSON());
73+
}
74+
5775
@Test
5876
public void testSound() {
5977
IosNotification ios = IosNotification.newBuilder().setSound("sound").build();

test/cn/jpush/api/push/remote/NotificationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void sendNotification_ios_badge() throws Exception {
6868
PushPayload payload = PushPayload.newBuilder()
6969
.setAudience(Audience.all())
7070
.setPlatform(Platform.ios())
71-
.setNotification(Notification.ios_badgeAuto())
71+
.setNotification(Notification.ios_auto_badge())
7272
.build();
7373
PushResult result = _client.sendPush(payload);
7474
assertTrue(result.isResultOK());

0 commit comments

Comments
 (0)