Skip to content

Commit 0f1909d

Browse files
author
Javen
committed
Generate sendno by default if not being set - for easy to debug.
1 parent a4a6569 commit 0f1909d

File tree

8 files changed

+69
-43
lines changed

8 files changed

+69
-43
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.common;
22

3+
import java.util.Random;
34
import java.util.regex.Pattern;
45

56
import cn.jpush.api.utils.Base64;
@@ -9,6 +10,13 @@ public class ServiceHelper {
910

1011
private final static Pattern PUSH_PATTERNS = Pattern.compile("[^a-zA-Z0-9]");
1112

13+
private static final Random RANDOM = new Random(System.currentTimeMillis());
14+
private static final int MIN = 100000;
15+
private static final int MAX = Integer.MAX_VALUE;
16+
17+
public static int generateSendno() {
18+
return RANDOM.nextInt((MAX - MIN) + 1) + MIN;
19+
}
1220

1321
public static String getAuthorizationBase64(String appKey, String masterSecret) {
1422
String encodeKey = appKey + ":" + masterSecret;

src/cn/jpush/api/examples/JPushClientExample.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public static void main(String[] args) {
2828

2929
private static void testSendNotification() {
3030
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
31-
PushPayload payload = PushPayload.notificationAlertAll(CONTENT);
31+
PushPayload payload = PushPayload.alertAll(CONTENT);
3232
LOG.info("Paylaod JSON - " + payload.toString());
3333

3434
PushResult result = jpushClient.sendPush(payload);
@@ -45,7 +45,7 @@ private static void testSendNotification() {
4545

4646
private static void testSendMesasge() {
4747
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
48-
PushPayload payload = PushPayload.simpleMessageAll(CONTENT);
48+
PushPayload payload = PushPayload.messageAll(CONTENT);
4949
jpushClient.sendPush(payload);
5050
}
5151

src/cn/jpush/api/push/model/Options.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package cn.jpush.api.push.model;
22

3+
import cn.jpush.api.common.ServiceHelper;
4+
35
import com.google.common.base.Preconditions;
46
import com.google.gson.JsonElement;
57
import com.google.gson.JsonObject;
@@ -27,6 +29,14 @@ public static Builder newBuilder() {
2729
return new Builder();
2830
}
2931

32+
public static Options sendno() {
33+
return newBuilder().setSendno(ServiceHelper.generateSendno()).build();
34+
}
35+
36+
public static Options sendno(int sendno) {
37+
return newBuilder().setSendno(sendno).build();
38+
}
39+
3040
public void setApnsProduction(boolean apnsProduction) {
3141
this.apnsProduction = apnsProduction;
3242
}
@@ -83,6 +93,10 @@ public Options build() {
8393
Preconditions.checkArgument(sendno >= 0, "sendno should be greater than 0.");
8494
Preconditions.checkArgument(overrideMsgId >= 0, "override_msg_id should be greater than 0.");
8595
Preconditions.checkArgument(timeToLive >= 0, "time_to_live should be greater than 0.");
96+
if (sendno <= 0) {
97+
sendno = ServiceHelper.generateSendno();
98+
}
99+
86100
return new Options(sendno, overrideMsgId, timeToLive, apnsProduction);
87101
}
88102
}

src/cn/jpush/api/push/model/PushPayload.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,18 @@ public static Builder newBuilder() {
3636
return new Builder();
3737
}
3838

39-
public static PushPayload notificationAlertAll(String alert) {
39+
public static PushPayload alertAll(String alert) {
4040
return new Builder()
4141
.setPlatform(Platform.all())
4242
.setAudience(Audience.all())
4343
.setNotification(Notification.alert(alert)).build();
4444
}
4545

46-
public static PushPayload simpleMessageAll(String content) {
46+
public static PushPayload messageAll(String msgContent) {
4747
return new Builder()
4848
.setPlatform(Platform.all())
4949
.setAudience(Audience.all())
50-
.setMessage(Message.content(content)).build();
50+
.setMessage(Message.content(msgContent)).build();
5151
}
5252

5353
public static PushPayload fromJSON(String payloadString) {
@@ -131,6 +131,10 @@ public Builder setOptions(Options options) {
131131
public PushPayload build() {
132132
Preconditions.checkArgument(! (null == audience || null == platform), "Audience/Platform should be set.");
133133
Preconditions.checkArgument(! (null == notification && null == message), "notification or message should be set at least one.");
134+
if (null == options) {
135+
options = Options.sendno();
136+
}
137+
134138
return new PushPayload(platform, audience, notification, message, options);
135139
}
136140
}

test/cn/jpush/api/push/BasicFunctionsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public void before() {
3434

3535
@Test
3636
public void sendSimpleNotification_Pall_Ndefault() {
37-
PushPayload payload = PushPayload.notificationAlertAll(ALERT);
37+
PushPayload payload = PushPayload.alertAll(ALERT);
3838
PushResult result = _client.sendPush(payload);
3939
assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode());
4040
}
@@ -138,7 +138,7 @@ public void sendSimpleNotification_Pall_Nall() {
138138

139139
@Test
140140
public void sendSimpleMessage_default() {
141-
PushPayload payload = PushPayload.simpleMessageAll(MSG_CONTENT);
141+
PushPayload payload = PushPayload.messageAll(MSG_CONTENT);
142142
PushResult result = _client.sendPush(payload);
143143
assertEquals(SUCCEED_RESULT_CODE, result.getErrorCode());
144144
}

test/cn/jpush/api/push/ExceptionTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void before() {
3939
public void appKeyNotExist() {
4040
String appKey = "dd1066407b044738b6479274";
4141
JPushClient client = new JPushClient(masterSecret, appKey);
42-
PushPayload payload = PushPayload.notificationAlertAll(ALERT);
42+
PushPayload payload = PushPayload.alertAll(ALERT);
4343
PushResult result = client.sendPush(payload);
4444
assertEquals(APPKEY_NOT_EXIST, result.getErrorCode());
4545
}
@@ -48,15 +48,15 @@ public void appKeyNotExist() {
4848
public void authenticationFail() {
4949
String masterSecret = "2b38ce69b1de2a7fa95706e2";
5050
JPushClient client = new JPushClient(masterSecret, appKey);
51-
PushPayload payload = PushPayload.notificationAlertAll(ALERT);
51+
PushPayload payload = PushPayload.alertAll(ALERT);
5252
PushResult result = client.sendPush(payload);
5353
assertEquals(AUTHENTICATION_FAIL, result.getErrorCode());
5454
}
5555

5656
@Test
5757
public void tooBig() {
5858
String msgContent = "深圳制造厂的朋友告诉我,过去的一年,他们服务了几十家小型创业公司,代工智能手表。不过,今年这些创业公司已经找不到了,庆幸的是,代工厂都是先付款再生产,也就没有损失。可穿戴设备、硬件创新,大潮初起,泥沙俱下,浪潮过后,却是遍地狼藉。国内的智能手环、手表们,如土曼、果壳,在 Jawbone、Google Glass 们引领下,纷纷推出“划时代”的产品,一时间,国内宣称要做可穿戴设备的公司,如过江之鲫。2013 年,不说句硬件创新,不戴款智能手环,都不好意思说自己是站在人文与科技的十字路口。2013 年,身边的朋友纷纷佩戴上了 Jawbone,幸运的人也会戴上传说中的智能手表。不过,现在越来越多的朋友开始放弃这些所谓的可穿戴式设备。";
59-
PushPayload payload = PushPayload.simpleMessageAll(msgContent);
59+
PushPayload payload = PushPayload.messageAll(msgContent);
6060
String content = payload.toString();
6161

6262
byte[] bytes = content.getBytes();

test/cn/jpush/api/push/model/OptionsTests.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import org.junit.Assert;
55
import org.junit.Test;
66

7-
import cn.jpush.api.push.model.Options;
7+
import cn.jpush.api.common.ServiceHelper;
88

99
import com.google.gson.JsonObject;
1010
import com.google.gson.JsonPrimitive;
@@ -36,10 +36,17 @@ public void testSendno() {
3636

3737
@Test
3838
public void testApnsProduction() {
39+
int sendno = ServiceHelper.generateSendno();
40+
3941
JsonObject json = new JsonObject();
4042
json.add("apns_production", new JsonPrimitive(false));
41-
Options optional = Options.newBuilder().setApnsProduction(false).build();
42-
Assert.assertEquals("", json, optional.toJSON());
43+
json.add("sendno", new JsonPrimitive(sendno));
44+
45+
Options options = Options.newBuilder()
46+
.setSendno(sendno)
47+
.setApnsProduction(false).build();
48+
49+
Assert.assertEquals("", json, options.toJSON());
4350
}
4451

4552
}

test/cn/jpush/api/push/model/PushPayloadTests.java

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import org.junit.Assert;
44
import org.junit.Test;
55

6+
import cn.jpush.api.common.ServiceHelper;
67
import cn.jpush.api.push.model.audience.Audience;
78
import cn.jpush.api.push.model.notification.Notification;
89

@@ -45,61 +46,53 @@ public void testIllegal_NoPlatform() {
4546
.setAudience(audience)
4647
.setNotification(notifcation).build();
4748
}
48-
49-
@Test
50-
public void testQuickNotification() {
51-
PushPayload payload = PushPayload.notificationAlertAll("alert");
52-
JsonObject json = new JsonObject();
53-
JsonObject noti = new JsonObject();
54-
noti.add("alert", new JsonPrimitive("alert"));
55-
json.add("notification", noti);
56-
json.add("audience", new JsonPrimitive("all"));
57-
json.add("platform", new JsonPrimitive("all"));
58-
Assert.assertEquals("", json, payload.toJSON());
59-
}
60-
61-
@Test
62-
public void testQuickMessage() {
63-
PushPayload payload = PushPayload.simpleMessageAll("message");
64-
JsonObject json = new JsonObject();
65-
JsonObject msg = new JsonObject();
66-
msg.add("content", new JsonPrimitive("message"));
67-
json.add("message", msg);
68-
json.add("audience", new JsonPrimitive("all"));
69-
json.add("platform", new JsonPrimitive("all"));
70-
Assert.assertEquals("", json, payload.toJSON());
71-
}
72-
49+
7350
@Test
7451
public void testNotification() {
52+
int sendno = ServiceHelper.generateSendno();
7553
Notification notifcation = Notification.alert("alert");
7654
PushPayload payload = PushPayload.newBuilder()
7755
.setPlatform(Platform.all())
7856
.setAudience(Audience.all())
57+
.setOptions(Options.sendno(sendno))
7958
.setNotification(notifcation).build();
8059

8160
JsonObject json = new JsonObject();
61+
json.add("audience", new JsonPrimitive("all"));
62+
json.add("platform", new JsonPrimitive("all"));
63+
8264
JsonObject noti = new JsonObject();
8365
noti.add("alert", new JsonPrimitive("alert"));
8466
json.add("notification", noti);
85-
json.add("audience", new JsonPrimitive("all"));
86-
json.add("platform", new JsonPrimitive("all"));
67+
68+
JsonObject options = new JsonObject();
69+
options.add("sendno", new JsonPrimitive(sendno));
70+
json.add("options", options);
71+
8772
Assert.assertEquals("", json, payload.toJSON());
8873
}
8974

9075
@Test
9176
public void testMessage() {
77+
int sendno = ServiceHelper.generateSendno();
9278
PushPayload payload = PushPayload.newBuilder()
9379
.setPlatform(Platform.all())
9480
.setAudience(Audience.all())
81+
.setOptions(Options.sendno(sendno))
9582
.setMessage(Message.content("message")).build();
9683

9784
JsonObject json = new JsonObject();
98-
JsonObject msg = new JsonObject();
99-
msg.add("content", new JsonPrimitive("message"));
100-
json.add("message", msg);
10185
json.add("audience", new JsonPrimitive("all"));
10286
json.add("platform", new JsonPrimitive("all"));
87+
88+
JsonObject msg = new JsonObject();
89+
msg.add("msg_content", new JsonPrimitive("message"));
90+
json.add("message", msg);
91+
92+
JsonObject options = new JsonObject();
93+
options.add("sendno", new JsonPrimitive(sendno));
94+
json.add("options", options);
95+
10396
Assert.assertEquals("", json, payload.toJSON());
10497
}
10598

0 commit comments

Comments
 (0)