Skip to content

Commit 59c9e94

Browse files
author
Javen
committed
New feature: support badge to add/minus
1 parent 22168ed commit 59c9e94

File tree

7 files changed

+144
-16
lines changed

7 files changed

+144
-16
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,32 @@ public class ServiceHelper {
1515
private static final int MIN = 100000;
1616
private static final int MAX = Integer.MAX_VALUE;
1717

18+
private static final int MAX_BADGE_NUMBER = 99999;
19+
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+
}
31+
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) {
40+
}
41+
return false;
42+
}
43+
1844
public static int generateSendno() {
1945
return RANDOM.nextInt((MAX - MIN) + 1) + MIN;
2046
}

src/cn/jpush/api/push/PushClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* Can be used directly.
2020
*/
2121
public class PushClient {
22-
public static final String HOST_NAME_SSL = "https://api.jpush.cn";
22+
public static final String HOST_NAME_SSL = "https://api.jpush.cn:19688";
2323
public static final String PUSH_PATH = "/v3/push";
2424

2525
private final NativeHttpClient _httpClient;

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

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.util.Map;
44

5+
import cn.jpush.api.common.ServiceHelper;
6+
57
import com.google.common.base.Preconditions;
68
import com.google.common.collect.ImmutableMap;
79
import com.google.gson.JsonElement;
@@ -12,20 +14,23 @@ public class IosNotification extends PlatformNotification {
1214
public static final String NOTIFICATION_IOS = "ios";
1315

1416
private static final String DEFAULT_SOUND = "";
15-
private static final int BADGE_UNDEFINED = -1;
16-
private static final int DEFAULT_BADGE = 1;
17+
private static final String DEFAULT_BADGE = "+1";
1718

1819
private static final String BADGE = "badge";
1920
private static final String SOUND = "sound";
2021
private static final String CONTENT_AVAILABLE = "content-available";
2122

23+
private static final String ALERT_VALID_BADGE = "Badge number should be 0~99999, "
24+
+ "and can be prefixed with + to add, - to minus";
25+
26+
2227
private final boolean soundDisabled;
2328
private final boolean badgeDisabled;
2429
private final String sound;
25-
private final int badge;
30+
private final String badge;
2631
private final boolean contentAvailable;
2732

28-
private IosNotification(String alert, String sound, int badge,
33+
private IosNotification(String alert, String sound, String badge,
2934
boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
3035
ImmutableMap<String, String> extras,
3136
ImmutableMap<String, Number> numberExtras,
@@ -58,9 +63,9 @@ public JsonElement toJSON() {
5863
JsonObject json = super.toJSON().getAsJsonObject();
5964

6065
if (!badgeDisabled) {
61-
if (badge >= 0) {
66+
if (null != badge) {
6267
json.add(BADGE, new JsonPrimitive(this.badge));
63-
} else if (badge == BADGE_UNDEFINED) {
68+
} else {
6469
json.add(BADGE, new JsonPrimitive(DEFAULT_BADGE));
6570
}
6671
}
@@ -81,7 +86,7 @@ public JsonElement toJSON() {
8186

8287
public static class Builder extends PlatformNotification.Builder<IosNotification> {
8388
private String sound;
84-
private int badge = BADGE_UNDEFINED;
89+
private String badge;
8590
private boolean contentAvailable = false;
8691
private boolean soundDisabled = false;
8792
private boolean badgeDisabled = false;
@@ -96,11 +101,25 @@ public Builder disableSound() {
96101
return this;
97102
}
98103

99-
public Builder setBadge(int badge) {
104+
public Builder setBadge(String badge) {
105+
// check available badge value
106+
if (!ServiceHelper.isValidBadgeValue(badge)) {
107+
LOG.warn(ALERT_VALID_BADGE);
108+
return this;
109+
}
110+
100111
this.badge = badge;
101112
return this;
102113
}
103114

115+
public Builder setBadge(int badge) {
116+
return setBadge(badge + "");
117+
}
118+
119+
public Builder setBadgeAuto() {
120+
return setBadge(DEFAULT_BADGE);
121+
}
122+
104123
public Builder disableBadge() {
105124
this.badgeDisabled = true;
106125
return this;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,30 @@ public static Notification ios(String alert, Map<String, String> extras) {
5959
.build();
6060
}
6161

62+
/**
63+
* shortcut
64+
*/
65+
public static Notification ios_badgeAuto() {
66+
return newBuilder()
67+
.addPlatformNotification(IosNotification.newBuilder()
68+
.setAlert("")
69+
.setBadgeAuto()
70+
.build())
71+
.build();
72+
}
73+
74+
/**
75+
* shortcut
76+
*/
77+
public static Notification ios_badge(String badge) {
78+
return newBuilder()
79+
.addPlatformNotification(IosNotification.newBuilder()
80+
.setAlert("")
81+
.setBadge(badge)
82+
.build())
83+
.build();
84+
}
85+
6286
/**
6387
* shortcut
6488
*/
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package cn.jpush.api.common;
2+
3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
6+
public class ServiceHelperTest {
7+
8+
@Test
9+
public void test_isValidBadge() {
10+
String badge = "+12432";
11+
Assert.assertTrue("", ServiceHelper.isValidBadgeValue(badge));
12+
13+
badge = "+99999";
14+
Assert.assertTrue("", ServiceHelper.isValidBadgeValue(badge));
15+
16+
badge = "+100000";
17+
Assert.assertFalse("", ServiceHelper.isValidBadgeValue(badge));
18+
19+
badge = "-99999";
20+
Assert.assertTrue("", ServiceHelper.isValidBadgeValue(badge));
21+
22+
badge = "99999";
23+
Assert.assertTrue("", ServiceHelper.isValidBadgeValue(badge));
24+
25+
badge = "0";
26+
Assert.assertTrue("", ServiceHelper.isValidBadgeValue(badge));
27+
28+
badge = "-1";
29+
Assert.assertTrue("", ServiceHelper.isValidBadgeValue(badge));
30+
31+
badge = "++10000";
32+
Assert.assertFalse("", ServiceHelper.isValidBadgeValue(badge));
33+
34+
badge = "100000";
35+
Assert.assertFalse("", ServiceHelper.isValidBadgeValue(badge));
36+
37+
}
38+
39+
}

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +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));
16+
json.add("badge", new JsonPrimitive("+1"));
1717
Assert.assertEquals("", json, ios.toJSON());
1818
}
1919

@@ -23,15 +23,15 @@ public void testQuickAlert() {
2323
JsonObject json = new JsonObject();
2424
json.add("alert", new JsonPrimitive("aaa"));
2525
json.add("sound", new JsonPrimitive(""));
26-
json.add("badge", new JsonPrimitive(1));
26+
json.add("badge", new JsonPrimitive("+1"));
2727
Assert.assertEquals("", json, ios.toJSON());
2828
}
2929

3030
@Test
3131
public void testBadge_0() {
3232
IosNotification ios = IosNotification.newBuilder().setBadge(0).build();
3333
JsonObject json = new JsonObject();
34-
json.add("badge", new JsonPrimitive(0));
34+
json.add("badge", new JsonPrimitive("0"));
3535
json.add("sound", new JsonPrimitive(""));
3636
Assert.assertEquals("", json, ios.toJSON());
3737
}
@@ -40,7 +40,16 @@ public void testBadge_0() {
4040
public void testBadge_2() {
4141
IosNotification ios = IosNotification.newBuilder().setBadge(2).build();
4242
JsonObject json = new JsonObject();
43-
json.add("badge", new JsonPrimitive(2));
43+
json.add("badge", new JsonPrimitive("2"));
44+
json.add("sound", new JsonPrimitive(""));
45+
Assert.assertEquals("", json, ios.toJSON());
46+
}
47+
48+
@Test
49+
public void testBadge_plus_2() {
50+
IosNotification ios = IosNotification.newBuilder().setBadge("+2").build();
51+
JsonObject json = new JsonObject();
52+
json.add("badge", new JsonPrimitive("+2"));
4453
json.add("sound", new JsonPrimitive(""));
4554
Assert.assertEquals("", json, ios.toJSON());
4655
}
@@ -50,7 +59,7 @@ public void testSound() {
5059
IosNotification ios = IosNotification.newBuilder().setSound("sound").build();
5160
JsonObject json = new JsonObject();
5261
json.add("sound", new JsonPrimitive("sound"));
53-
json.add("badge", new JsonPrimitive(1));
62+
json.add("badge", new JsonPrimitive("+1"));
5463
Assert.assertEquals("", json, ios.toJSON());
5564
}
5665

@@ -63,7 +72,7 @@ public void testSoundDisabled() {
6372
.build();
6473
JsonObject json = new JsonObject();
6574
json.add("alert", new JsonPrimitive("alert"));
66-
json.add("badge", new JsonPrimitive(1));
75+
json.add("badge", new JsonPrimitive("+1"));
6776
Assert.assertEquals("", json, ios.toJSON());
6877
}
6978

@@ -91,7 +100,7 @@ public void testExtra() {
91100
extra.add("key2", new JsonPrimitive(Boolean.TRUE));
92101
json.add("extras", extra);
93102
json.add("sound", new JsonPrimitive(""));
94-
json.add("badge", new JsonPrimitive(1));
103+
json.add("badge", new JsonPrimitive("+1"));
95104
Assert.assertEquals("", json, ios.toJSON());
96105
}
97106

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,17 @@ public void sendNotification_android_extras() throws Exception {
6363

6464
// ------------------ ios
6565

66+
@Test
67+
public void sendNotification_ios_badge() throws Exception {
68+
PushPayload payload = PushPayload.newBuilder()
69+
.setAudience(Audience.all())
70+
.setPlatform(Platform.ios())
71+
.setNotification(Notification.ios_badgeAuto())
72+
.build();
73+
PushResult result = _client.sendPush(payload);
74+
assertTrue(result.isResultOK());
75+
}
76+
6677

6778

6879

0 commit comments

Comments
 (0)