Skip to content

Commit cd71623

Browse files
committed
Merge pull request #17 from Liuchy1/master
add custom click, fix responseWrapper, add schedule
2 parents ccfd1af + fd4e0e9 commit cd71623

File tree

15 files changed

+633
-7
lines changed

15 files changed

+633
-7
lines changed

src/main/java/cn/jpush/api/JPushClient.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@
2525
import cn.jpush.api.report.ReceivedsResult;
2626
import cn.jpush.api.report.ReportClient;
2727
import cn.jpush.api.report.UsersResult;
28+
import cn.jpush.api.schedule.ScheduleClient;
29+
import cn.jpush.api.schedule.ScheduleListResult;
30+
import cn.jpush.api.schedule.ScheduleResult;
31+
import cn.jpush.api.schedule.model.SchedulePayload;
32+
import cn.jpush.api.schedule.model.TriggerPayload;
2833

2934
/**
3035
* The global entrance of JPush API library.
@@ -33,6 +38,7 @@ public class JPushClient {
3338
private final PushClient _pushClient;
3439
private final ReportClient _reportClient;
3540
private final DeviceClient _deviceClient;
41+
private final ScheduleClient _scheduleClient;
3642

3743
/**
3844
* Create a JPush Client.
@@ -44,18 +50,21 @@ public JPushClient(String masterSecret, String appKey) {
4450
_pushClient = new PushClient(masterSecret, appKey);
4551
_reportClient = new ReportClient(masterSecret, appKey);
4652
_deviceClient = new DeviceClient(masterSecret, appKey);
53+
_scheduleClient = new ScheduleClient(masterSecret, appKey);
4754
}
4855

4956
public JPushClient(String masterSecret, String appKey, int maxRetryTimes) {
5057
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes);
5158
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes);
5259
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes);
60+
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes);
5361
}
5462

5563
public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
5664
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy);
5765
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy);
5866
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy);
67+
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy);
5968
}
6069

6170
/**
@@ -73,6 +82,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
7382
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
7483
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
7584
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
85+
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
7686
}
7787

7888
/**
@@ -94,6 +104,7 @@ public JPushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPr
94104
_pushClient = new PushClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
95105
_reportClient = new ReportClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
96106
_deviceClient = new DeviceClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
107+
_scheduleClient = new ScheduleClient(masterSecret, appKey, maxRetryTimes, proxy, conf);
97108
_pushClient.setDefaults(apnsProduction, timeToLive);
98109
}
99110

@@ -111,6 +122,7 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l
111122
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
112123
_reportClient = new ReportClient(masterSecret, appKey);
113124
_deviceClient = new DeviceClient(masterSecret, appKey);
125+
_scheduleClient = new ScheduleClient(masterSecret, appKey);
114126
}
115127

116128

@@ -392,7 +404,58 @@ public DefaultResult deleteAlias(String alias, String platform)
392404
throws APIConnectionException, APIRequestException {
393405
return _deviceClient.deleteAlias(alias, platform);
394406
}
395-
396-
407+
408+
public ScheduleResult createSingleSchedule(String name, String time, PushPayload push)
409+
throws APIConnectionException, APIRequestException {
410+
TriggerPayload trigger = TriggerPayload.newBuilder()
411+
.setSingleTime(time)
412+
.buildSingle();
413+
SchedulePayload payload = SchedulePayload.newBuilder()
414+
.setName(name)
415+
.setEnabled(true)
416+
.setTrigger(trigger)
417+
.setPush(push)
418+
.build();
419+
420+
return _scheduleClient.createSchedule(payload);
421+
}
422+
423+
public ScheduleResult createPeriodicalSchedule(String name, String start, String end, String time,
424+
TriggerPayload.TimeUnit timeUnit, int frequency, String[] point, PushPayload push)
425+
throws APIConnectionException, APIRequestException {
426+
TriggerPayload trigger = TriggerPayload.newBuilder()
427+
.setPeriodTime(start, end, time)
428+
.setTimeFrequency(timeUnit, frequency, point )
429+
.buildPeriodical();
430+
SchedulePayload payload = SchedulePayload.newBuilder()
431+
.setName(name)
432+
.setEnabled(true)
433+
.setTrigger(trigger)
434+
.setPush(push)
435+
.build();
436+
437+
return _scheduleClient.createSchedule(payload);
438+
}
439+
440+
public ScheduleResult getSchedule(String scheduleId)
441+
throws APIConnectionException, APIRequestException {
442+
return _scheduleClient.getSchedule(scheduleId);
443+
}
444+
445+
public ScheduleListResult getScheduleList(int page)
446+
throws APIConnectionException, APIRequestException {
447+
return _scheduleClient.getScheduleList(page);
448+
}
449+
450+
public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload)
451+
throws APIConnectionException, APIRequestException {
452+
return _scheduleClient.updateSchedule(scheduleId, payload);
453+
}
454+
455+
public void deleteSchedule(String scheduleId)
456+
throws APIConnectionException, APIRequestException {
457+
_scheduleClient.deleteSchedule(scheduleId);
458+
}
459+
397460
}
398461

src/main/java/cn/jpush/api/common/ClientConfig.java

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

3-
43
import java.util.HashMap;
54
import java.util.Map;
65

@@ -41,8 +40,14 @@ public class ClientConfig extends HashMap<String, Object> {
4140
public static final String REPORT_MESSAGE_PATH = "report.message.path";
4241
public static final Object REPORT_MESSAGE_PATH_SCHEMA = String.class;
4342

43+
public static final String SCHEDULE_HOST_NAME = "schedule.host.name";
44+
public static final Object SCHEDULE_HOST_NAME_SCHEMA = String.class;
45+
46+
public static final String SCHEDULE_PATH = "schedule.path";
47+
public static final Object SCHEDULE_PATH_SCHEMA = String.class;
48+
4449
private ClientConfig() {
45-
super();
50+
super(12);
4651
this.put(DEVICE_HOST_NAME, "https://device.jpush.cn");
4752
this.put(DEVICES_PATH, "/v3/devices");
4853
this.put(TAGS_PATH, "/v3/tags");
@@ -56,6 +61,9 @@ private ClientConfig() {
5661
this.put(REPORT_RECEIVE_PATH, "/v3/received");
5762
this.put(REPORT_USER_PATH, "/v3/users");
5863
this.put(REPORT_MESSAGE_PATH, "/v3/messages");
64+
65+
this.put(SCHEDULE_HOST_NAME, "https://api.jpush.cn");
66+
this.put(SCHEDULE_PATH, "/v3/schedules");
5967
}
6068

6169
public static ClientConfig getInstance() {
@@ -98,4 +106,12 @@ public void setReportHostName(String hostName) {
98106
setReportHostName(this, hostName);
99107
}
100108

109+
public static void setScheduleHostName(Map conf, String hostName) {
110+
conf.put(SCHEDULE_HOST_NAME, hostName);
111+
}
112+
113+
public void setScheduleHostName(String hostName) {
114+
setScheduleHostName(this, hostName);
115+
}
116+
101117
}

src/main/java/cn/jpush/api/common/resp/ResponseWrapper.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
import org.slf4j.Logger;
1010
import org.slf4j.LoggerFactory;
1111

12-
import java.util.Scanner;
13-
1412
public class ResponseWrapper {
1513
private static final Logger LOG = LoggerFactory.getLogger(ResponseWrapper.class);
1614
private static final int RESPONSE_CODE_NONE = -1;

src/main/java/cn/jpush/api/report/MessagesResult.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,16 @@ public static class Android {
2626
@Expose public int target;
2727
@Expose public int online_push;
2828
@Expose public int click;
29+
@Expose public int msg_click;
2930
}
3031

3132
public static class Ios {
3233
@Expose public int apns_sent;
3334
@Expose public int apns_target;
3435
@Expose public int click;
36+
@Expose public int target;
37+
@Expose public int received;
38+
@Expose public int msg_click;
3539
}
3640

3741
static MessagesResult fromResponse(ResponseWrapper responseWrapper) {
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package cn.jpush.api.schedule;
2+
3+
import cn.jpush.api.common.ClientConfig;
4+
import cn.jpush.api.common.ServiceHelper;
5+
import cn.jpush.api.common.connection.HttpProxy;
6+
import cn.jpush.api.common.connection.IHttpClient;
7+
import cn.jpush.api.common.connection.NativeHttpClient;
8+
import cn.jpush.api.common.resp.APIConnectionException;
9+
import cn.jpush.api.common.resp.APIRequestException;
10+
import cn.jpush.api.common.resp.ResponseWrapper;
11+
import cn.jpush.api.schedule.model.SchedulePayload;
12+
import org.slf4j.Logger;
13+
import org.slf4j.LoggerFactory;
14+
15+
import java.lang.reflect.Proxy;
16+
17+
public class ScheduleClient {
18+
19+
private static final Logger LOG = LoggerFactory.getLogger(ScheduleClient.class);
20+
private final NativeHttpClient _httpClient;
21+
22+
private String hostName;
23+
private String schedulePath;
24+
25+
public ScheduleClient(String masterSecret, String appkey) {
26+
this(masterSecret, appkey, IHttpClient.DEFAULT_MAX_RETRY_TIMES, null, ClientConfig.getInstance());
27+
}
28+
29+
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes) {
30+
this(masterSecret, appKey, maxRetryTimes, null, ClientConfig.getInstance());
31+
}
32+
33+
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy) {
34+
this(masterSecret, appKey, maxRetryTimes, proxy, ClientConfig.getInstance());
35+
}
36+
37+
/**
38+
* Create a Schedule Client with custom configuration.
39+
* @param masterSecret API access secret of the appKey.
40+
* @param appKey The KEY of one application on JPush.
41+
* @param maxRetryTimes Max retry times
42+
* @param proxy The proxy, if there is no proxy, should be null.
43+
* @param conf The client configuration. Can use ClientConfig.getInstance() as default.
44+
*/
45+
public ScheduleClient(String masterSecret, String appKey, int maxRetryTimes, HttpProxy proxy, ClientConfig conf) {
46+
ServiceHelper.checkBasic(appKey, masterSecret);
47+
hostName = (String) conf.get(ClientConfig.SCHEDULE_HOST_NAME);
48+
schedulePath = (String) conf.get(ClientConfig.SCHEDULE_PATH);
49+
50+
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
51+
this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
52+
}
53+
54+
public ScheduleResult createSchedule(SchedulePayload payload) throws APIConnectionException, APIRequestException {
55+
ResponseWrapper response = _httpClient.sendPost(hostName + schedulePath, payload.toString());
56+
return ScheduleResult.fromResponse(response, ScheduleResult.class);
57+
}
58+
59+
public ScheduleListResult getScheduleList(int page) throws APIConnectionException, APIRequestException{
60+
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "?page=" + page);
61+
return ScheduleListResult.fromResponse(response, ScheduleListResult.class);
62+
}
63+
64+
public ScheduleResult getSchedule(String scheduleId) throws APIConnectionException, APIRequestException{
65+
ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "/" + scheduleId);
66+
67+
return ScheduleResult.fromResponse(response, ScheduleResult.class);
68+
}
69+
70+
public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload) throws APIConnectionException, APIRequestException{
71+
ResponseWrapper response = _httpClient.sendPut(hostName + schedulePath + "/" + scheduleId,
72+
payload.toString());
73+
return ScheduleResult.fromResponse(response, ScheduleResult.class);
74+
}
75+
76+
public void deleteSchedule(String scheduleId) throws APIConnectionException, APIRequestException{
77+
_httpClient.sendDelete(hostName + schedulePath + "/" + scheduleId);
78+
}
79+
80+
81+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.jpush.api.schedule;
2+
3+
4+
import cn.jpush.api.common.resp.BaseResult;
5+
import com.google.gson.annotations.Expose;
6+
7+
import java.util.List;
8+
9+
public class ScheduleListResult extends BaseResult{
10+
11+
@Expose int total_count;
12+
@Expose int total_pages;
13+
@Expose int page;
14+
@Expose List<ScheduleResult> schedules;
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.jpush.api.schedule;
2+
3+
import cn.jpush.api.common.resp.BaseResult;
4+
import com.google.gson.JsonObject;
5+
import com.google.gson.annotations.Expose;
6+
7+
public class ScheduleResult extends BaseResult{
8+
9+
@Expose String schedule_id;
10+
@Expose String name;
11+
@Expose Boolean enabled;
12+
@Expose JsonObject trigger;
13+
@Expose JsonObject push;
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package cn.jpush.api.schedule.model;
2+
3+
import com.google.gson.JsonElement;
4+
5+
public interface IModel {
6+
7+
public JsonElement toJSON();
8+
}

0 commit comments

Comments
 (0)