Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 70 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,27 +87,26 @@
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.PushExample

```Java
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_all_all_alert();

try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);

} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);

} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 3);

// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_all_all_alert();

try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);

} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);

} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
```

进行推送的关键在于构建一个 PushPayload 对象。以下示例一般的构建对象的用法。
Expand Down Expand Up @@ -190,42 +189,62 @@
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ReportsExample

```Java
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
try {
ReceivedsResult result = jpushClient.getReportReceiveds("1942377665");
LOG.debug("Got result - " + result);
} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
try {
ReceivedsResult result = jpushClient.getReportReceiveds("1942377665");
LOG.debug("Got result - " + result);

} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);

} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
```

### Tag/Alias 样例

> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.DeviceExample

```Java
try {
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);

LOG.info(result.alias);
LOG.info(result.tags.toString());

} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);

} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
try {
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);

LOG.info(result.alias);
LOG.info(result.tags.toString());
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
```

### Schedule 样例

> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.ScheduleExample

```Java
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
String name = "test_schedule_example";
String time = "2016-07-30 12:30:25";
PushPayload push = PushPayload.alertAll("test schedule example.");
try {
ScheduleResult result = jpushClient.createSingleSchedule(name, time, push);
LOG.info("schedule result is " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
```
118 changes: 118 additions & 0 deletions example/main/java/cn/jpush/api/examples/ScheduleExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
package cn.jpush.api.examples;

import cn.jpush.api.JPushClient;
import cn.jpush.api.common.resp.APIConnectionException;
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.schedule.ScheduleListResult;
import cn.jpush.api.schedule.ScheduleResult;
import cn.jpush.api.schedule.model.SchedulePayload;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ScheduleExample {

protected static final Logger LOG = LoggerFactory.getLogger(ScheduleExample.class);

private static final String appKey ="e5c0d34f58732cf09b2d4d74";
private static final String masterSecret = "4cdda6d3c8b029941dbc5cb3";

public static void main(String[] args) {

// testDeleteSchedule();
testGetScheduleList();
// testUpdateSchedule();
testGetSchedule();
}

public static void testCreateSingleSchedule() {
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
String name = "test_schedule_example";
String time = "2016-07-30 12:30:25";
PushPayload push = PushPayload.alertAll("test schedule example.");
try {
ScheduleResult result = jpushClient.createSingleSchedule(name, time, push);
LOG.info("schedule result is " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}

public static void testDeleteSchedule() {
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
JPushClient jpushClient = new JPushClient(masterSecret, appKey);

try {
jpushClient.deleteSchedule(scheduleId);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}

public static void testGetScheduleList() {
int page = 1;
JPushClient jpushClient = new JPushClient(masterSecret, appKey);

try {
ScheduleListResult list = jpushClient.getScheduleList(page);
LOG.info("total " + list.getTotal_count());
for(ScheduleResult s : list.getSchedules()) {
LOG.info(s.toString());
}
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}

public static void testUpdateSchedule() {
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
SchedulePayload payload = SchedulePayload.newBuilder()
.setEnabled(false)
.build();
try {
jpushClient.updateSchedule(scheduleId, payload);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}

public static void testGetSchedule() {
String scheduleId = "95bbd066-3a88-11e5-8e62-0021f652c102";
JPushClient jpushClient = new JPushClient(masterSecret, appKey);

try {
ScheduleResult result = jpushClient.getSchedule(scheduleId);
LOG.info("schedule " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}


}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jdkVersion>1.6</jdkVersion>
<jdkVersion.test>1.6</jdkVersion.test>
<additionalparam>-Xdoclint:none</additionalparam>
<!--<additionalparam>-Xdoclint:none</additionalparam>-->
</properties>

<parent>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,10 @@ private ResponseWrapper _doRequest(String url, String content,
LOG.error("Request is forbidden! Maybe your appkey is listed in blacklist or your params is invalid.");
wrapper.setErrorObject();
break;
case 404:
LOG.error("Request page is not found! Maybe your params is invalid.");
wrapper.setErrorObject();
break;
case 410:
LOG.error("Request resource is no longer in service. Please according to notice on official website.");
wrapper.setErrorObject();
Expand Down
21 changes: 18 additions & 3 deletions src/main/java/cn/jpush/api/schedule/ScheduleClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.common.resp.ResponseWrapper;
import cn.jpush.api.schedule.model.SchedulePayload;
import cn.jpush.api.utils.Preconditions;
import cn.jpush.api.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.lang.reflect.Proxy;

public class ScheduleClient {

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

public ScheduleResult createSchedule(SchedulePayload payload) throws APIConnectionException, APIRequestException {

Preconditions.checkArgument(null != payload, "payload should not be null");

ResponseWrapper response = _httpClient.sendPost(hostName + schedulePath, payload.toString());
return ScheduleResult.fromResponse(response, ScheduleResult.class);
}

public ScheduleListResult getScheduleList(int page) throws APIConnectionException, APIRequestException{

Preconditions.checkArgument(page > 0, "page should more than 0.");

ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "?page=" + page);
return ScheduleListResult.fromResponse(response, ScheduleListResult.class);
}

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

Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");

ResponseWrapper response = _httpClient.sendGet(hostName + schedulePath + "/" + scheduleId);
return ScheduleResult.fromResponse(response, ScheduleResult.class);
}

public ScheduleResult updateSchedule(String scheduleId, SchedulePayload payload) throws APIConnectionException, APIRequestException{

Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");
Preconditions.checkArgument(null != payload, "payload should not be null");

ResponseWrapper response = _httpClient.sendPut(hostName + schedulePath + "/" + scheduleId,
payload.toString());
return ScheduleResult.fromResponse(response, ScheduleResult.class);
}

public void deleteSchedule(String scheduleId) throws APIConnectionException, APIRequestException{

Preconditions.checkArgument(StringUtils.isNotEmpty(scheduleId), "scheduleId should not be empty");

_httpClient.sendDelete(hostName + schedulePath + "/" + scheduleId);
}

Expand Down
16 changes: 16 additions & 0 deletions src/main/java/cn/jpush/api/schedule/ScheduleListResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,20 @@ public class ScheduleListResult extends BaseResult{
@Expose int total_pages;
@Expose int page;
@Expose List<ScheduleResult> schedules;

public int getTotal_count() {
return total_count;
}

public int getTotal_pages() {
return total_pages;
}

public int getPage() {
return page;
}

public List<ScheduleResult> getSchedules() {
return schedules;
}
}
20 changes: 20 additions & 0 deletions src/main/java/cn/jpush/api/schedule/ScheduleResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,24 @@ public class ScheduleResult extends BaseResult{
@Expose Boolean enabled;
@Expose JsonObject trigger;
@Expose JsonObject push;

public String getSchedule_id() {
return schedule_id;
}

public String getName() {
return name;
}

public Boolean getEnabled() {
return enabled;
}

public JsonObject getTrigger() {
return trigger;
}

public JsonObject getPush() {
return push;
}
}
Loading