Skip to content

Commit aa8ad28

Browse files
author
Javen
committed
Adding support for report users
1 parent 1e4f44c commit aa8ad28

File tree

9 files changed

+113
-14
lines changed

9 files changed

+113
-14
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
package cn.jpush.api;
22

3+
import cn.jpush.api.common.TimeUnit;
34
import cn.jpush.api.push.PushClient;
45
import cn.jpush.api.push.PushResult;
56
import cn.jpush.api.push.model.PushPayload;
67
import cn.jpush.api.report.ReceivedsResult;
78
import cn.jpush.api.report.ReportClient;
9+
import cn.jpush.api.report.UsersResult;
810

911
/**
10-
* The overall entrance of JPush API library.
12+
* The global entrance of JPush API library.
1113
*/
1214
public class JPushClient {
1315
private final PushClient _pushClient;
@@ -62,6 +64,11 @@ public PushResult sendPush(String payloadString) {
6264
public ReceivedsResult getReportReceiveds(String msgIds) {
6365
return _reportClient.getReceiveds(msgIds);
6466
}
65-
67+
68+
public UsersResult getReportUsersCount(TimeUnit timeUnit, String start, int step) {
69+
return _reportClient.getUsersCount(timeUnit, start, step);
70+
}
71+
72+
6673
}
6774

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public ResponseWrapper sendRequest(String url, String content, String method, St
9797

9898
if (status == 200) {
9999
LOG.debug("Succeed to get response - 200 OK");
100+
LOG.debug(responseContent);
100101

101102
} else {
102103
LOG.info("Got error response - responseCode:" + status + ", responseContent:" + responseContent);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void setErrorObject() {
4141
error = _gson.fromJson(responseContent, ErrorObject.class);
4242
}
4343

44-
public boolean isServerResonse() {
44+
public boolean isServerResponse() {
4545
if (responseCode == 200) return true;
4646
if (responseCode > 0 && null != error && error.error.code > 0) return true;
4747
return false;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package cn.jpush.api.common;
2+
3+
public enum TimeUnit {
4+
5+
HOUR,
6+
DAY,
7+
MONTH
8+
9+
10+
}

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

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
import org.slf4j.LoggerFactory;
55

66
import cn.jpush.api.JPushClient;
7+
import cn.jpush.api.common.TimeUnit;
78
import cn.jpush.api.report.ReceivedsResult;
9+
import cn.jpush.api.report.UsersResult;
810

911
public class ReportsExample {
1012
protected static final Logger LOG = LoggerFactory.getLogger(ReportsExample.class);
@@ -14,14 +16,14 @@ public class ReportsExample {
1416
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
1517

1618
public static void main(String[] args) {
17-
testGetReport();
19+
// testGetReport();
20+
testGetUsers();
1821
}
1922

2023

2124
public static void testGetReport() {
2225
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
2326
ReceivedsResult receivedsResult = jpushClient.getReportReceiveds("1708010723,1774452771");
24-
LOG.debug("responseContent - " + receivedsResult.getOriginalContent());
2527
if (receivedsResult.isResultOK()) {
2628
LOG.info("Receiveds - " + receivedsResult);
2729
} else {
@@ -38,5 +40,25 @@ public static void testGetReport() {
3840
}
3941
}
4042

43+
public static void testGetUsers() {
44+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
45+
UsersResult result = jpushClient.getReportUsersCount(TimeUnit.DAY, "20140606", 2);
46+
47+
if (result.isResultOK()) {
48+
LOG.info("Users Count - " + result);
49+
} else {
50+
if (result.getErrorCode() > 0) {
51+
// 业务异常
52+
LOG.warn("Service error - ErrorCode: "
53+
+ result.getErrorCode() + ", ErrorMessage: "
54+
+ result.getErrorMessage());
55+
} else {
56+
// 未到达 JPush
57+
LOG.error("Other excepitons - "
58+
+ result.getExceptionString());
59+
}
60+
}
61+
}
62+
4163
}
4264

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PushResult extends BaseResult {
1313
public static PushResult fromResponse(ResponseWrapper responseWrapper) {
1414
PushResult pushResult = null;
1515

16-
if (responseWrapper.isServerResonse()) {
16+
if (responseWrapper.isServerResponse()) {
1717
pushResult = _gson.fromJson(responseWrapper.responseContent, PushResult.class);
1818
} else {
1919
pushResult = new PushResult();

src/cn/jpush/api/report/ReceivedsResult.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public List<Received> getReceivedList() {
2828

2929
public static ReceivedsResult fromResponse(ResponseWrapper responseWrapper) {
3030
ReceivedsResult receivedsResult = new ReceivedsResult();
31-
if (responseWrapper.isServerResonse()) {
31+
if (responseWrapper.isServerResponse()) {
3232
receivedsResult.received_list = _gson.fromJson(responseWrapper.responseContent, RECEIVED_TYPE);
3333
}
3434

src/cn/jpush/api/report/ReportClient.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
import cn.jpush.api.common.NativeHttpClient;
66
import cn.jpush.api.common.ResponseWrapper;
77
import cn.jpush.api.common.ServiceHelper;
8+
import cn.jpush.api.common.TimeUnit;
89
import cn.jpush.api.utils.StringUtils;
910

1011
public class ReportClient {
11-
private static final String REPORT_HOST_NAME = "https://report.jpush.cn";
12+
private static final String REPORT_HOST_NAME = "http://183.232.25.237:9900"; //"https://report.jpush.cn";
1213
private static final String REPORT_RECEIVE_PATH = "/v2/received";
14+
private static final String REPORT_USER_PATH = "/v3/user";
1315

1416
private NativeHttpClient _httpClient = new NativeHttpClient();;
1517

@@ -28,19 +30,26 @@ public ReceivedsResult getReceiveds(String[] msgIdArray) {
2830
}
2931

3032
public ReceivedsResult getReceiveds(String msgIds) {
31-
String authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
32-
return getResportReceived(msgIds, authCode);
33-
}
34-
35-
public ReceivedsResult getResportReceived(String msgIds, String authCode) {
3633
checkMsgids(msgIds);
34+
String authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
3735

3836
String url = REPORT_HOST_NAME + REPORT_RECEIVE_PATH + "?msg_ids=" + msgIds;
3937
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
4038

4139
return ReceivedsResult.fromResponse(response);
40+
}
41+
42+
public UsersResult getUsersCount(TimeUnit timeUnit, String start, int step) {
43+
String authCode = ServiceHelper.getAuthorizationBase64(_appKey, _masterSecret);
44+
45+
String url = REPORT_HOST_NAME + REPORT_USER_PATH
46+
+ "?time_unit=" + timeUnit.toString()
47+
+ "&start=" + start + "&step=" + step;
48+
ResponseWrapper response = _httpClient.sendGet(url, null, authCode);
49+
50+
return UsersResult.fromResponse(response);
4251
}
43-
52+
4453

4554
private final static Pattern MSGID_PATTERNS = Pattern.compile("[^0-9, ]");
4655

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package cn.jpush.api.report;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import cn.jpush.api.common.BaseResult;
7+
import cn.jpush.api.common.ResponseWrapper;
8+
import cn.jpush.api.common.TimeUnit;
9+
10+
import com.google.gson.annotations.Expose;
11+
import com.google.gson.annotations.SerializedName;
12+
13+
public class UsersResult extends BaseResult {
14+
15+
@Expose public TimeUnit time_unit;
16+
@Expose public String start;
17+
@Expose public int step;
18+
@Expose public List<User> items = new ArrayList<User>();
19+
20+
21+
public static class User {
22+
@Expose public String time;
23+
@Expose public Android android;
24+
@Expose public Ios ios;
25+
}
26+
27+
public static class Android {
28+
@SerializedName("new") @Expose public long add;
29+
@Expose public int online;
30+
@Expose public int active;
31+
}
32+
33+
public static class Ios {
34+
@SerializedName("new") @Expose public long add;
35+
@Expose public int active;
36+
}
37+
38+
public static UsersResult fromResponse(ResponseWrapper responseWrapper) {
39+
UsersResult usersResult = new UsersResult();
40+
if (responseWrapper.isServerResponse()) {
41+
usersResult = _gson.fromJson(responseWrapper.responseContent, UsersResult.class);
42+
}
43+
44+
usersResult.setResponseWrapper(responseWrapper);
45+
return usersResult;
46+
}
47+
48+
}
49+
50+

0 commit comments

Comments
 (0)