Skip to content

Commit 42b3f8a

Browse files
author
Javen
committed
1. Finished all JSON serialization.
2. Unit tests for JSON serialization. 3. HTTP requesting. TODO: feature tests.
1 parent 9e091a6 commit 42b3f8a

37 files changed

+780
-731
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 31 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,58 @@
11
package cn.jpush.api;
22

3-
import java.util.Map;
4-
5-
import cn.jpush.api.common.DeviceType;
6-
import cn.jpush.api.push.CustomMessageParams;
7-
import cn.jpush.api.push.MessageParams;
8-
import cn.jpush.api.push.MessageResult;
9-
import cn.jpush.api.push.NotificationParams;
103
import cn.jpush.api.push.PushClient;
11-
import cn.jpush.api.push.model.AudienceType;
4+
import cn.jpush.api.push.PushResult;
5+
import cn.jpush.api.push.model.PushPayload;
126
import cn.jpush.api.report.ReceivedsResult;
137
import cn.jpush.api.report.ReportClient;
148

159
/**
1610
* The overall entrance of JPush API library.
17-
*
1811
*/
1912
public class JPushClient {
2013
private PushClient _pushClient;
2114
private ReportClient _reportClient;
2215

2316
/**
24-
* Create a JPush Client.
25-
* Use some defaults - time_to_live:1 day; platform:all platforms; apnsProduction:true.
17+
* Create a JPush Client.
2618
*
27-
* @param masterSecret API access secret of the app_key.
28-
* @param appKey The app_key of one application on JPush
19+
* @param masterSecret API access secret of the appKey.
20+
* @param appKey The KEY of one application on JPush.
2921
*/
3022
public JPushClient(String masterSecret, String appKey) {
31-
this(masterSecret, appKey, MessageParams.NO_TIME_TO_LIVE, null, true);
23+
_pushClient = new PushClient(masterSecret, appKey);
24+
_reportClient = new ReportClient(masterSecret, appKey);
3225
}
3326

3427
/**
35-
* Create a JPush Client.
36-
* With defined sending push params.
28+
* Create a JPush Client with overall settings.
3729
*
38-
* @param masterSecret API access secret of the app_key.
39-
* @param appKey The app_key of one application on JPush.
40-
* @param timeToLive The off-line message live time long.
41-
* @param device The target device of the push. If null will send to all platforms.
42-
* @param apnsProduction If iOS push which environment should be use for sending APNs.
30+
* @param masterSecret API access secret of the appKey.
31+
* @param appKey The KEY of one application on JPush.
32+
* @param apnsProduction Overall APNs environment setting. It will override PushPayload Optional.
33+
* @param timeToLive Overall time_to_live setting. It will override PushPayload Optional.
4334
*/
44-
public JPushClient(String masterSecret, String appKey, long timeToLive, DeviceType device, boolean apnsProduction) {
45-
_pushClient = new PushClient(masterSecret, appKey, timeToLive, device, apnsProduction);
46-
_reportClient = new ReportClient(masterSecret, appKey);
47-
}
48-
49-
50-
public MessageResult sendNotification(String notificationContent, NotificationParams params, Map<String, Object> extras) {
51-
return _pushClient.sendNotification(notificationContent, params, extras);
52-
}
53-
54-
public MessageResult sendCustomMessage(String msgTitle, String msgContent, CustomMessageParams params, Map<String, Object> extras) {
55-
return _pushClient.sendCustomMessage(msgTitle, msgContent, params, extras);
35+
public JPushClient(String masterSecret, String appKey, boolean apnsProduction, long timeToLive) {
36+
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
37+
_reportClient = new ReportClient(masterSecret, appKey);
38+
}
39+
40+
/**
41+
* Send a push
42+
*
43+
* @param pushPayload payload of a push.
44+
* @return PushResult. Can be printed to a JSON.
45+
*/
46+
public PushResult sendPush(PushPayload pushPayload) {
47+
return _pushClient.sendPush(pushPayload);
5648
}
5749

58-
59-
public MessageResult sendCustomMessageAll(String msgTitle, String msgContent) {
60-
CustomMessageParams params = new CustomMessageParams();
61-
params.setReceiverType(AudienceType.APP_KEY);
62-
//params.setTimeToLive(MessageParams.DEFAULT_TIME_TO_LIVE);
63-
//params.setSendNo(1);
64-
//params.setOverrideMsgId("");
65-
return _pushClient.sendCustomMessage(msgTitle, msgContent, params, null);
66-
}
67-
68-
public MessageResult sendNotificationAll(String notificationContent) {
69-
NotificationParams params = new NotificationParams();
70-
params.setReceiverType(AudienceType.APP_KEY);
71-
//params.setTimeToLive(MessageParams.DEFAULT_TIME_TO_LIVE);
72-
//params.setSendNo(1);
73-
//params.setAndroidNotificationTitle("");
74-
//params.setAndroidBuilderId(0);
75-
//params.setOverrideMsgId("");
76-
return _pushClient.sendNotification(notificationContent, params, null);
77-
}
78-
79-
50+
/**
51+
* Get received report.
52+
*
53+
* @param msgIds 100 msgids to batch getting is supported.
54+
* @return ReceivedResult. Can be printed to JSON.
55+
*/
8056
public ReceivedsResult getReportReceiveds(String msgIds) {
8157
return _reportClient.getReceiveds(msgIds);
8258
}

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,26 +45,23 @@ public class BaseHttpClient {
4545
private final int DEFAULT_SOCKET_TIMEOUT = (30 * 1000); // milliseconds
4646

4747

48-
protected ResponseResult sendGet(String url, boolean enabledSSL, String params, String authCode) {
49-
return sendRequest(url, enabledSSL, params, "GET", authCode);
48+
protected ResponseResult sendGet(String url, String params, String authCode) {
49+
return sendRequest(url, params, "GET", authCode);
5050
}
5151

52-
protected ResponseResult sendPost(String url, final boolean enableSSL,String params, String authCode) {
53-
return sendRequest(url, enableSSL, params, "POST", authCode);
52+
protected ResponseResult sendPost(String url, String content, String authCode) {
53+
return sendRequest(url, content, "POST", authCode);
5454
}
5555

56-
protected ResponseResult sendRequest(String url, final boolean enableSSL,
57-
String params, String method, String authCode) {
58-
LOG.debug("Send request to - " + url + ", with params - " + params);
56+
protected ResponseResult sendRequest(String url, String content, String method, String authCode) {
57+
LOG.debug("Send request to - " + url + ", with content - " + content);
5958
HttpURLConnection conn = null;
6059
OutputStream out = null;
6160
StringBuffer sb = new StringBuffer();
6261
ResponseResult result = new ResponseResult();
6362

6463
try {
65-
if (enableSSL) {
66-
initSSL();
67-
}
64+
initSSL();
6865

6966
URL aUrl = new URL(url);
7067
conn = (HttpURLConnection) aUrl.openConnection();
@@ -82,7 +79,7 @@ protected ResponseResult sendRequest(String url, final boolean enableSSL,
8279
if (method.equals("POST")) {
8380
conn.setDoOutput(true); //POST Request
8481
conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
85-
byte[] data = params.getBytes(CHARSET);
82+
byte[] data = content.getBytes(CHARSET);
8683
conn.setRequestProperty("Content-Length", String.valueOf(data.length));
8784
out = conn.getOutputStream();
8885
out.write(data);

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

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cn.jpush.api.common;
22
import java.util.regex.Pattern;
33

4-
import cn.jpush.api.push.MessageParams;
54
import cn.jpush.api.utils.StringUtils;
65

76
public class ValidateRequestParams {
@@ -24,11 +23,6 @@ public static void checkBasic(String appKey, String masterSecret) {
2423

2524
}
2625

27-
public static void checkPushParams(MessageParams params) {
28-
checkBasic(params.getAppKey(), params.getMasterSecret());
29-
30-
31-
}
3226

3327
public static void checkReportParams(String appKey, String masterSecret, String msgIds) {
3428
checkBasic(appKey, masterSecret);

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

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
import org.slf4j.LoggerFactory;
55

66
import cn.jpush.api.JPushClient;
7-
import cn.jpush.api.common.DeviceType;
8-
import cn.jpush.api.push.MessageResult;
9-
import cn.jpush.api.push.NotificationParams;
10-
import cn.jpush.api.push.model.AudienceType;
117
import cn.jpush.api.report.ReceivedsResult;
128

139
public class JPushClientExample {
@@ -28,30 +24,6 @@ public static void main(String[] args) {
2824
}
2925

3026
private static void testSend() {
31-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, 0, DeviceType.Android, false);
32-
NotificationParams params = new NotificationParams();
33-
//params.setReceiverType(ReceiverTypeEnum.REGISTRATION_ID);
34-
//params.setReceiverValue(registrationID);
35-
params.setReceiverType(AudienceType.TAG);
36-
params.setReceiverValue(TAG);
37-
38-
MessageResult msgResult = jpushClient.sendNotification(CONTENT, params, null);
39-
LOG.debug("responseContent - " + msgResult.responseResult.responseContent);
40-
if (msgResult.isResultOK()) {
41-
LOG.info("msgResult - " + msgResult);
42-
LOG.info("messageId - " + msgResult.getMessageId());
43-
} else {
44-
if (msgResult.getErrorCode() > 0) {
45-
// 业务异常
46-
LOG.warn("Service error - ErrorCode: "
47-
+ msgResult.getErrorCode() + ", ErrorMessage: "
48-
+ msgResult.getErrorMessage());
49-
} else {
50-
// 未到达 JPush
51-
LOG.error("Other excepitons - "
52-
+ msgResult.responseResult.exceptionString);
53-
}
54-
}
5527
}
5628

5729
public static void testGetReport() {

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

Lines changed: 0 additions & 49 deletions
This file was deleted.

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

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)