Skip to content

Commit 16c4c7b

Browse files
author
Javen
committed
New feature: push validate API
1 parent d864f78 commit 16c4c7b

File tree

3 files changed

+72
-5
lines changed

3 files changed

+72
-5
lines changed

src/cn/jpush/api/JPushClient.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ public JPushClient(String masterSecret, String appKey, boolean apnsProduction, l
6060
_pushClient = new PushClient(masterSecret, appKey, apnsProduction, timeToLive);
6161
_reportClient = new ReportClient(masterSecret, appKey);
6262
}
63+
64+
65+
// ----------------------------- Push API
6366

6467
/**
6568
* Send a push with PushPayload object.
@@ -88,6 +91,25 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
8891
public PushResult sendPush(String payloadString) throws APIConnectionException, APIRequestException {
8992
return _pushClient.sendPush(payloadString);
9093
}
94+
95+
/**
96+
* Validate a push action, but do NOT send it actually.
97+
*
98+
* @param paylaod
99+
* @return
100+
* @throws APIConnectionException
101+
* @throws APIRequestException
102+
*/
103+
public PushResult sendPushValidate(PushPayload paylaod) throws APIConnectionException, APIRequestException {
104+
return _pushClient.sendPushValidate(paylaod);
105+
}
106+
107+
public PushResult sendPushValidate(String payloadString) throws APIConnectionException, APIRequestException {
108+
return _pushClient.sendPushValidate(payloadString);
109+
}
110+
111+
112+
// ------------------------------- Report API
91113

92114
/**
93115
* Get received report.

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

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
public class PushClient {
2828
public static final String HOST_NAME_SSL = "https://api.jpush.cn";
2929
public static final String PUSH_PATH = "/v3/push";
30+
public static final String PUSH_VALIDATE_PATH = "/v3/push/validate";
3031

3132
private final NativeHttpClient _httpClient;
3233
private JsonParser _jsonParser = new JsonParser();
@@ -66,7 +67,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
6667
ServiceHelper.checkBasic(appKey, masterSecret);
6768

6869
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
69-
this._baseUrl = HOST_NAME_SSL + PUSH_PATH;
70+
this._baseUrl = HOST_NAME_SSL;
7071
this._httpClient = new NativeHttpClient(authCode, maxRetryTimes, proxy);
7172
}
7273

@@ -106,7 +107,20 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
106107
pushPayload.resetOptionsApnsProduction(_apnsProduction);
107108
}
108109

109-
ResponseWrapper response = _httpClient.sendPost(_baseUrl, pushPayload.toString());
110+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, pushPayload.toString());
111+
112+
return PushResult.fromResponse(response);
113+
}
114+
115+
public PushResult sendPushValidate(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
116+
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
117+
118+
if (_globalSettingEnabled) {
119+
pushPayload.resetOptionsTimeToLive(_timeToLive);
120+
pushPayload.resetOptionsApnsProduction(_apnsProduction);
121+
}
122+
123+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, pushPayload.toString());
110124

111125
return PushResult.fromResponse(response);
112126
}
@@ -120,11 +134,26 @@ public PushResult sendPush(String payloadString) throws APIConnectionException,
120134
Preconditions.checkArgument(false, "payloadString should be a valid JSON string.");
121135
}
122136

123-
ResponseWrapper response = _httpClient.sendPost(_baseUrl, payloadString);
137+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_PATH, payloadString);
138+
139+
return PushResult.fromResponse(response);
140+
}
141+
142+
public PushResult sendPushValidate(String payloadString) throws APIConnectionException, APIRequestException {
143+
Preconditions.checkArgument(StringUtils.isNotEmpty(payloadString), "pushPayload should not be empty");
144+
145+
try {
146+
_jsonParser.parse(payloadString);
147+
} catch (JsonParseException e) {
148+
Preconditions.checkArgument(false, "payloadString should be a valid JSON string.");
149+
}
150+
151+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + PUSH_VALIDATE_PATH, payloadString);
124152

125153
return PushResult.fromResponse(response);
126154
}
127155

156+
128157
}
129158

130159

test/cn/jpush/api/push/PushClientTest.java

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

3+
import static org.junit.Assert.assertTrue;
4+
import static org.junit.Assert.fail;
5+
36
import org.junit.Test;
47

58
import cn.jpush.api.common.APIConnectionException;
69
import cn.jpush.api.common.APIRequestException;
710
import cn.jpush.api.common.HttpProxy;
11+
import cn.jpush.api.push.model.PushPayload;
812

913
public class PushClientTest {
1014
private static final String appKey ="dd1066407b044738b6479275";
@@ -34,14 +38,26 @@ public void test_empty_string() {
3438
} catch (APIRequestException e) {
3539
e.printStackTrace();
3640
}
37-
}
41+
}
3842

3943
@Test(expected = IllegalArgumentException.class)
4044
public void test_empty_password() {
4145
new HttpProxy("127.0.0.1", 8080, "", null);
4246
}
4347

44-
48+
@Test
49+
public void test_validate() {
50+
PushClient pushClient = new PushClient(masterSecret, appKey);
51+
52+
try {
53+
PushResult result = pushClient.sendPushValidate(PushPayload.alertAll("alert"));
54+
assertTrue("", result.isResultOK());
55+
} catch (APIRequestException e) {
56+
fail("Should not fail");
57+
} catch (APIConnectionException e) {
58+
e.printStackTrace();
59+
}
60+
}
4561

4662

4763

0 commit comments

Comments
 (0)