Skip to content

Commit 86de906

Browse files
committed
Merge pull request #18 from Liuchy1/master
add schedule test and example
2 parents cd71623 + 5102e0c commit 86de906

File tree

12 files changed

+415
-81
lines changed

12 files changed

+415
-81
lines changed

README.md

Lines changed: 70 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -87,27 +87,26 @@
8787
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.PushExample
8888
8989
```Java
90-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
91-
92-
// For push, all you need do is to build PushPayload object.
93-
PushPayload payload = buildPushObject_all_all_alert();
94-
95-
try {
96-
PushResult result = jpushClient.sendPush(payload);
97-
LOG.info("Got result - " + result);
98-
99-
} catch (APIConnectionException e) {
100-
// Connection error, should retry later
101-
LOG.error("Connection error, should retry later", e);
102-
103-
} catch (APIRequestException e) {
104-
// Should review the error, and fix the request
105-
LOG.error("Should review the error, and fix the request", e);
106-
LOG.info("HTTP Status: " + e.getStatus());
107-
LOG.info("Error Code: " + e.getErrorCode());
108-
LOG.info("Error Message: " + e.getErrorMessage());
109-
}
90+
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);
11091

92+
// For push, all you need do is to build PushPayload object.
93+
PushPayload payload = buildPushObject_all_all_alert();
94+
95+
try {
96+
PushResult result = jpushClient.sendPush(payload);
97+
LOG.info("Got result - " + result);
98+
99+
} catch (APIConnectionException e) {
100+
// Connection error, should retry later
101+
LOG.error("Connection error, should retry later", e);
102+
103+
} catch (APIRequestException e) {
104+
// Should review the error, and fix the request
105+
LOG.error("Should review the error, and fix the request", e);
106+
LOG.info("HTTP Status: " + e.getStatus());
107+
LOG.info("Error Code: " + e.getErrorCode());
108+
LOG.info("Error Message: " + e.getErrorMessage());
109+
}
111110
```
112111

113112
进行推送的关键在于构建一个 PushPayload 对象。以下示例一般的构建对象的用法。
@@ -190,42 +189,62 @@
190189
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ReportsExample
191190
192191
```Java
193-
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
194-
try {
195-
ReceivedsResult result = jpushClient.getReportReceiveds("1942377665");
196-
LOG.debug("Got result - " + result);
197-
198-
} catch (APIConnectionException e) {
199-
// Connection error, should retry later
200-
LOG.error("Connection error, should retry later", e);
201-
202-
} catch (APIRequestException e) {
203-
// Should review the error, and fix the request
204-
LOG.error("Should review the error, and fix the request", e);
205-
LOG.info("HTTP Status: " + e.getStatus());
206-
LOG.info("Error Code: " + e.getErrorCode());
207-
LOG.info("Error Message: " + e.getErrorMessage());
208-
}
192+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
193+
try {
194+
ReceivedsResult result = jpushClient.getReportReceiveds("1942377665");
195+
LOG.debug("Got result - " + result);
196+
197+
} catch (APIConnectionException e) {
198+
// Connection error, should retry later
199+
LOG.error("Connection error, should retry later", e);
200+
201+
} catch (APIRequestException e) {
202+
// Should review the error, and fix the request
203+
LOG.error("Should review the error, and fix the request", e);
204+
LOG.info("HTTP Status: " + e.getStatus());
205+
LOG.info("Error Code: " + e.getErrorCode());
206+
LOG.info("Error Message: " + e.getErrorMessage());
207+
}
209208
```
210209

211210
### Tag/Alias 样例
212211

213212
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.DeviceExample
214213
215214
```Java
216-
try {
217-
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
218-
219-
LOG.info(result.alias);
220-
LOG.info(result.tags.toString());
221-
222-
} catch (APIConnectionException e) {
223-
LOG.error("Connection error. Should retry later. ", e);
224-
225-
} catch (APIRequestException e) {
226-
LOG.error("Error response from JPush server. Should review and fix it. ", e);
227-
LOG.info("HTTP Status: " + e.getStatus());
228-
LOG.info("Error Code: " + e.getErrorCode());
229-
LOG.info("Error Message: " + e.getErrorMessage());
230-
}
215+
try {
216+
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
217+
218+
LOG.info(result.alias);
219+
LOG.info(result.tags.toString());
220+
} catch (APIConnectionException e) {
221+
LOG.error("Connection error. Should retry later. ", e);
222+
} catch (APIRequestException e) {
223+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
224+
LOG.info("HTTP Status: " + e.getStatus());
225+
LOG.info("Error Code: " + e.getErrorCode());
226+
LOG.info("Error Message: " + e.getErrorMessage());
227+
}
228+
```
229+
230+
### Schedule 样例
231+
232+
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ScheduleExample
233+
234+
```Java
235+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
236+
String name = "test_schedule_example";
237+
String time = "2016-07-30 12:30:25";
238+
PushPayload push = PushPayload.alertAll("test schedule example.");
239+
try {
240+
ScheduleResult result = jpushClient.createSingleSchedule(name, time, push);
241+
LOG.info("schedule result is " + result);
242+
} catch (APIConnectionException e) {
243+
LOG.error("Connection error. Should retry later. ", e);
244+
} catch (APIRequestException e) {
245+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
246+
LOG.info("HTTP Status: " + e.getStatus());
247+
LOG.info("Error Code: " + e.getErrorCode());
248+
LOG.info("Error Message: " + e.getErrorMessage());
249+
}
231250
```
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package cn.jpush.api.examples;
2+
3+
import cn.jpush.api.JPushClient;
4+
import cn.jpush.api.common.resp.APIConnectionException;
5+
import cn.jpush.api.common.resp.APIRequestException;
6+
import cn.jpush.api.push.model.PushPayload;
7+
import cn.jpush.api.schedule.ScheduleListResult;
8+
import cn.jpush.api.schedule.ScheduleResult;
9+
import cn.jpush.api.schedule.model.SchedulePayload;
10+
import org.slf4j.Logger;
11+
import org.slf4j.LoggerFactory;
12+
13+
public class ScheduleExample {
14+
15+
protected static final Logger LOG = LoggerFactory.getLogger(ScheduleExample.class);
16+
17+
private static final String appKey ="e5c0d34f58732cf09b2d4d74";
18+
private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3";
19+
20+
public static void main(String[] args) {
21+
22+
// testDeleteSchedule();
23+
testGetScheduleList();
24+
// testUpdateSchedule();
25+
testGetSchedule();
26+
}
27+
28+
public static void testCreateSingleSchedule() {
29+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
30+
String name = "test_schedule_example";
31+
String time = "2016-07-30 12:30:25";
32+
PushPayload push = PushPayload.alertAll("test schedule example.");
33+
try {
34+
ScheduleResult result = jpushClient.createSingleSchedule(name, time, push);
35+
LOG.info("schedule result is " + result);
36+
} catch (APIConnectionException e) {
37+
LOG.error("Connection error. Should retry later. ", e);
38+
} catch (APIRequestException e) {
39+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
40+
LOG.info("HTTP Status: " + e.getStatus());
41+
LOG.info("Error Code: " + e.getErrorCode());
42+
LOG.info("Error Message: " + e.getErrorMessage());
43+
}
44+
}
45+
46+
public static void testDeleteSchedule() {
47+
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
48+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
49+
50+
try {
51+
jpushClient.deleteSchedule(scheduleId);
52+
} catch (APIConnectionException e) {
53+
LOG.error("Connection error. Should retry later. ", e);
54+
} catch (APIRequestException e) {
55+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
56+
LOG.info("HTTP Status: " + e.getStatus());
57+
LOG.info("Error Code: " + e.getErrorCode());
58+
LOG.info("Error Message: " + e.getErrorMessage());
59+
}
60+
}
61+
62+
public static void testGetScheduleList() {
63+
int page = 1;
64+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
65+
66+
try {
67+
ScheduleListResult list = jpushClient.getScheduleList(page);
68+
LOG.info("total " + list.getTotal_count());
69+
for(ScheduleResult s : list.getSchedules()) {
70+
LOG.info(s.toString());
71+
}
72+
} catch (APIConnectionException e) {
73+
LOG.error("Connection error. Should retry later. ", e);
74+
} catch (APIRequestException e) {
75+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
76+
LOG.info("HTTP Status: " + e.getStatus());
77+
LOG.info("Error Code: " + e.getErrorCode());
78+
LOG.info("Error Message: " + e.getErrorMessage());
79+
}
80+
}
81+
82+
public static void testUpdateSchedule() {
83+
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
84+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
85+
SchedulePayload payload = SchedulePayload.newBuilder()
86+
.setEnabled(false)
87+
.build();
88+
try {
89+
jpushClient.updateSchedule(scheduleId, payload);
90+
} catch (APIConnectionException e) {
91+
LOG.error("Connection error. Should retry later. ", e);
92+
} catch (APIRequestException e) {
93+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
94+
LOG.info("HTTP Status: " + e.getStatus());
95+
LOG.info("Error Code: " + e.getErrorCode());
96+
LOG.info("Error Message: " + e.getErrorMessage());
97+
}
98+
}
99+
100+
public static void testGetSchedule() {
101+
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
102+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
103+
104+
try {
105+
ScheduleResult result = jpushClient.getSchedule(scheduleId);
106+
LOG.info("schedule " + result);
107+
} catch (APIConnectionException e) {
108+
LOG.error("Connection error. Should retry later. ", e);
109+
} catch (APIRequestException e) {
110+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
111+
LOG.info("HTTP Status: " + e.getStatus());
112+
LOG.info("Error Code: " + e.getErrorCode());
113+
LOG.info("Error Message: " + e.getErrorMessage());
114+
}
115+
}
116+
117+
118+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
2323
<jdkVersion>1.6</jdkVersion>
2424
<jdkVersion.test>1.6</jdkVersion.test>
25-
<additionalparam>-Xdoclint:none</additionalparam>
25+
<!--<additionalparam>-Xdoclint:none</additionalparam>-->
2626
</properties>
2727

2828
<parent>

src/main/java/cn/jpush/api/common/connection/NativeHttpClient.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,10 @@ private ResponseWrapper _doRequest(String url, String content,
208208
LOG.error("Request is forbidden! Maybe your appkey is listed in blacklist or your params is invalid.");
209209
wrapper.setErrorObject();
210210
break;
211+
case 404:
212+
LOG.error("Request page is not found! Maybe your params is invalid.");
213+
wrapper.setErrorObject();
214+
break;
211215
case 410:
212216
LOG.error("Request resource is no longer in service. Please according to notice on official website.");
213217
wrapper.setErrorObject();

src/main/java/cn/jpush/api/schedule/ScheduleClient.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
import cn.jpush.api.common.resp.APIRequestException;
1010
import cn.jpush.api.common.resp.ResponseWrapper;
1111
import cn.jpush.api.schedule.model.SchedulePayload;
12+
import cn.jpush.api.utils.Preconditions;
13+
import cn.jpush.api.utils.StringUtils;
1214
import org.slf4j.Logger;
1315
import org.slf4j.LoggerFactory;
1416

15-
import java.lang.reflect.Proxy;
16-
1717
public class ScheduleClient {
1818

1919
private static final Logger LOG = LoggerFactory.getLogger(ScheduleClient.class);
@@ -52,28 +52,43 @@ public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, Htt
5252
}
5353

5454
public ScheduleResult createSchedule(SchedulePayload payload) throws APIConnectionException, APIRequestException {
55+
56+
Preconditions.checkArgument(null != payload, "payload should not be null");
57+
5558
ResponseWrapper response = _httpClient.sendPost(hostName + schedulePath, payload.toString());
5659
return ScheduleResult.fromResponse(response, ScheduleResult.class);
5760
}
5861

5962
public ScheduleListResult getScheduleList(int page) throws APIConnectionException, APIRequestException{
63+
64+
Preconditions.checkArgument(page > 0, "page should more than 0.");
65+
6066
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "?page=" + page);
6167
return ScheduleListResult.fromResponse(response, ScheduleListResult.class);
6268
}
6369

6470
public ScheduleResult getSchedule(String scheduleId) throws APIConnectionException, APIRequestException{
65-
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "/" + scheduleId);
6671

72+
Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
73+
74+
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "/" + scheduleId);
6775
return ScheduleResult.fromResponse(response, ScheduleResult.class);
6876
}
6977

7078
public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload) throws APIConnectionException, APIRequestException{
79+
80+
Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
81+
Preconditions.checkArgument(null != payload, "payload should not be null");
82+
7183
ResponseWrapper response = _httpClient.sendPut(hostName + schedulePath + "/" + scheduleId,
7284
payload.toString());
7385
return ScheduleResult.fromResponse(response, ScheduleResult.class);
7486
}
7587

7688
public void deleteSchedule(String scheduleId) throws APIConnectionException, APIRequestException{
89+
90+
Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
91+
7792
_httpClient.sendDelete(hostName + schedulePath + "/" + scheduleId);
7893
}
7994

src/main/java/cn/jpush/api/schedule/ScheduleListResult.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,20 @@ public class ScheduleListResult extends BaseResult{
1212
@Expose int total_pages;
1313
@Expose int page;
1414
@Expose List<ScheduleResult> schedules;
15+
16+
public int getTotal_count() {
17+
return total_count;
18+
}
19+
20+
public int getTotal_pages() {
21+
return total_pages;
22+
}
23+
24+
public int getPage() {
25+
return page;
26+
}
27+
28+
public List<ScheduleResult> getSchedules() {
29+
return schedules;
30+
}
1531
}

src/main/java/cn/jpush/api/schedule/ScheduleResult.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,24 @@ public class ScheduleResult extends BaseResult{
1111
@Expose Boolean enabled;
1212
@Expose JsonObject trigger;
1313
@Expose JsonObject push;
14+
15+
public String getSchedule_id() {
16+
return schedule_id;
17+
}
18+
19+
public String getName() {
20+
return name;
21+
}
22+
23+
public Boolean getEnabled() {
24+
return enabled;
25+
}
26+
27+
public JsonObject getTrigger() {
28+
return trigger;
29+
}
30+
31+
public JsonObject getPush() {
32+
return push;
33+
}
1434
}

0 commit comments

Comments
 (0)