Skip to content

Commit 612ef40

Browse files
authored
Merge pull request #85 from jpush/dev
Add new APIs, prepare release v3.3.0
2 parents 2569f66 + b23c670 commit 612ef40

File tree

14 files changed

+374
-89
lines changed

14 files changed

+374
-89
lines changed

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 123 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
import cn.jiguang.common.connection.NativeHttpClient;
1212
import cn.jiguang.common.connection.NettyHttpClient;
1313
import cn.jiguang.common.resp.ResponseWrapper;
14+
import cn.jpush.api.push.CIDResult;
15+
import cn.jpush.api.push.GroupPushClient;
1416
import cn.jpush.api.push.model.notification.*;
1517
import com.google.gson.*;
1618
import io.netty.handler.codec.http.HttpMethod;
@@ -34,11 +36,10 @@ public class PushExample {
3436
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
3537

3638
// demo App defined in resources/jpush-api.conf
37-
private static final String appKey ="dd1066407b044738b6479275";
38-
private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7";
39-
4039
protected static final String APP_KEY ="d4ee2375846bc30fa51334f5";
41-
protected static final String MASTER_SECRET = "2bf52ee46fdeaadb8718fc15";
40+
protected static final String MASTER_SECRET = "61807e56ddaebf2e47172159";
41+
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
42+
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";
4243

4344
public static final String TITLE = "Test from API example";
4445
public static final String ALERT = "Test from API Example - alert";
@@ -85,9 +86,74 @@ public static void testSendPush() {
8586
NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
8687
// Call setHttpClient to set httpClient,
8788
// If you don't invoke this method, default httpClient will use NativeHttpClient.
89+
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
90+
// jpushClient.getPushClient().setHttpClient(httpClient);
91+
final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
92+
// // For push, all you need do is to build PushPayload object.
93+
// PushPayload payload = buildPushObject_all_alias_alert();
94+
try {
95+
PushResult result = jpushClient.sendPush(payload);
96+
LOG.info("Got result - " + result);
97+
System.out.println(result);
98+
// 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
99+
// If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
100+
// jpushClient.close();
101+
} catch (APIConnectionException e) {
102+
LOG.error("Connection error. Should retry later. ", e);
103+
LOG.error("Sendno: " + payload.getSendno());
104+
105+
} catch (APIRequestException e) {
106+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
107+
LOG.info("HTTP Status: " + e.getStatus());
108+
LOG.info("Error Code: " + e.getErrorCode());
109+
LOG.info("Error Message: " + e.getErrorMessage());
110+
LOG.info("Msg ID: " + e.getMsgId());
111+
LOG.error("Sendno: " + payload.getSendno());
112+
}
113+
}
114+
115+
//use String to build PushPayload instance
116+
public static void testSendPush_fromJSON() {
117+
ClientConfig clientConfig = ClientConfig.getInstance();
118+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
119+
Gson gson = new GsonBuilder()
120+
.registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())
121+
.create();
122+
// Since the type of DeviceType is enum, thus the value should be uppercase, same with the AudienceType.
123+
String payloadString = "{\"platform\":{\"all\":false,\"deviceTypes\":[\"IOS\"]},\"audience\":{\"all\":false,\"targets\":[{\"audienceType\":\"TAG_AND\",\"values\":[\"tag1\",\"tag_all\"]}]},\"notification\":{\"notifications\":[{\"soundDisabled\":false,\"badgeDisabled\":false,\"sound\":\"happy\",\"badge\":\"5\",\"contentAvailable\":false,\"alert\":\"Test from API Example - alert\",\"extras\":{\"from\":\"JPush\"},\"type\":\"cn.jpush.api.push.model.notification.IosNotification\"}]},\"message\":{\"msgContent\":\"Test from API Example - msgContent\"},\"options\":{\"sendno\":1429488213,\"overrideMsgId\":0,\"timeToLive\":-1,\"apnsProduction\":true,\"bigPushDuration\":0}}";
124+
PushPayload payload = gson.fromJson(payloadString, PushPayload.class);
125+
try {
126+
PushResult result = jpushClient.sendPush(payload);
127+
LOG.info("Got result - " + result);
128+
129+
} catch (APIConnectionException e) {
130+
LOG.error("Connection error. Should retry later. ", e);
131+
LOG.error("Sendno: " + payload.getSendno());
132+
133+
} catch (APIRequestException e) {
134+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
135+
LOG.info("HTTP Status: " + e.getStatus());
136+
LOG.info("Error Code: " + e.getErrorCode());
137+
LOG.info("Error Message: " + e.getErrorMessage());
138+
LOG.info("Msg ID: " + e.getMsgId());
139+
LOG.error("Sendno: " + payload.getSendno());
140+
}
141+
}
142+
143+
/**
144+
* 测试多线程发送 2000 条推送耗时
145+
*/
146+
public static void testSendPushes() {
147+
ClientConfig clientConfig = ClientConfig.getInstance();
148+
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
149+
String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
150+
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
151+
NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
152+
// Call setHttpClient to set httpClient,
153+
// If you don't invoke this method, default httpClient will use NativeHttpClient.
88154
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
89155
jpushClient.getPushClient().setHttpClient(httpClient);
90-
final PushPayload payload = buildPushObject_android_newly_support();
156+
final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
91157
for(int i=0;i<10;i++) {
92158
Thread thread = new Thread() {
93159
public void run() {
@@ -116,41 +182,13 @@ public void run() {
116182
};
117183
thread.start();
118184
}
119-
120-
// // For push, all you need do is to build PushPayload object.
121-
// PushPayload payload = buildPushObject_all_alias_alert();
122-
// try {
123-
// PushResult result = jpushClient.sendPush(payload);
124-
// LOG.info("Got result - " + result);
125-
// // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
126-
// // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
127-
// // jpushClient.close();
128-
// } catch (APIConnectionException e) {
129-
// LOG.error("Connection error. Should retry later. ", e);
130-
// LOG.error("Sendno: " + payload.getSendno());
131-
//
132-
// } catch (APIRequestException e) {
133-
// LOG.error("Error response from JPush server. Should review and fix it. ", e);
134-
// LOG.info("HTTP Status: " + e.getStatus());
135-
// LOG.info("Error Code: " + e.getErrorCode());
136-
// LOG.info("Error Message: " + e.getErrorMessage());
137-
// LOG.info("Msg ID: " + e.getMsgId());
138-
// LOG.error("Sendno: " + payload.getSendno());
139-
// }
140185
}
141186

142-
//use String to build PushPayload instance
143-
public static void testSendPush_fromJSON() {
144-
ClientConfig clientConfig = ClientConfig.getInstance();
145-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
146-
Gson gson = new GsonBuilder()
147-
.registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())
148-
.create();
149-
// Since the type of DeviceType is enum, thus the value should be uppercase, same with the AudienceType.
150-
String payloadString = "{\"platform\":{\"all\":false,\"deviceTypes\":[\"IOS\"]},\"audience\":{\"all\":false,\"targets\":[{\"audienceType\":\"TAG_AND\",\"values\":[\"tag1\",\"tag_all\"]}]},\"notification\":{\"notifications\":[{\"soundDisabled\":false,\"badgeDisabled\":false,\"sound\":\"happy\",\"badge\":\"5\",\"contentAvailable\":false,\"alert\":\"Test from API Example - alert\",\"extras\":{\"from\":\"JPush\"},\"type\":\"cn.jpush.api.push.model.notification.IosNotification\"}]},\"message\":{\"msgContent\":\"Test from API Example - msgContent\"},\"options\":{\"sendno\":1429488213,\"overrideMsgId\":0,\"timeToLive\":-1,\"apnsProduction\":true,\"bigPushDuration\":0}}";
151-
PushPayload payload = gson.fromJson(payloadString, PushPayload.class);
187+
public static void testSendGroupPush() {
188+
GroupPushClient groupPushClient = new GroupPushClient(GROUP_MASTER_SECRET, GROUP_PUSH_KEY);
189+
final PushPayload payload = buildPushObject_android_and_ios();
152190
try {
153-
PushResult result = jpushClient.sendPush(payload);
191+
PushResult result = groupPushClient.sendGroupPush(payload);
154192
LOG.info("Got result - " + result);
155193

156194
} catch (APIConnectionException e) {
@@ -165,10 +203,6 @@ public static void testSendPush_fromJSON() {
165203
LOG.info("Msg ID: " + e.getMsgId());
166204
LOG.error("Sendno: " + payload.getSendno());
167205
}
168-
}
169-
170-
public static void testSendPushes() {
171-
172206
}
173207

174208
public static PushPayload buildPushObject_all_all_alert() {
@@ -277,9 +311,12 @@ public static PushPayload buildPushObject_android_newly_support() {
277311
.build();
278312
return PushPayload.newBuilder()
279313
.setPlatform(Platform.all())
280-
.setAudience(Audience.registrationId("18071adc030dcba91c0"))
314+
.setAudience(Audience.all())
281315
.setNotification(notification)
282-
.setOptions(Options.sendno())
316+
.setOptions(Options.newBuilder()
317+
.setApnsProduction(true)
318+
.setSendno(ServiceHelper.generateSendno())
319+
.build())
283320
.build();
284321
}
285322

@@ -305,12 +342,21 @@ public static PushPayload buildPushObject_all_tag_not() {
305342
.build();
306343
}
307344

345+
public static PushPayload buildPushObject_android_cid() {
346+
return PushPayload.newBuilder()
347+
.setPlatform(Platform.android())
348+
.setAudience(Audience.registrationId("18071adc030dcba91c0"))
349+
.setNotification(Notification.alert(ALERT))
350+
.setCid("cid")
351+
.build();
352+
}
353+
308354
public static void testSendPushWithCustomConfig() {
309355
ClientConfig config = ClientConfig.getInstance();
310356
// Setup the custom hostname
311357
config.setPushHostName("https://api.jpush.cn");
312358

313-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, config);
359+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, config);
314360

315361
// For push, all you need do is to build PushPayload object.
316362
PushPayload payload = buildPushObject_all_all_alert();
@@ -332,7 +378,7 @@ public static void testSendPushWithCustomConfig() {
332378
}
333379

334380
public static void testSendIosAlert() {
335-
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
381+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
336382

337383
IosAlert alert = IosAlert.newBuilder()
338384
.setTitleAndBody("test alert", "subtitle", "test ios alert json")
@@ -352,7 +398,7 @@ public static void testSendIosAlert() {
352398
}
353399

354400
public static void testSendWithSMS() {
355-
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
401+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY);
356402
try {
357403
SMS sms = SMS.content("Test SMS", 10);
358404
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
@@ -367,5 +413,36 @@ public static void testSendWithSMS() {
367413
}
368414
}
369415

416+
public static void testGetCidList() {
417+
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
418+
try {
419+
CIDResult result = jPushClient.getCidList(3, null);
420+
LOG.info("Got result - " + result);
421+
} catch (APIConnectionException e) {
422+
LOG.error("Connection error. Should retry later. ", e);
423+
} catch (APIRequestException e) {
424+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
425+
LOG.info("HTTP Status: " + e.getStatus());
426+
LOG.info("Error Code: " + e.getErrorCode());
427+
LOG.info("Error Message: " + e.getErrorMessage());
428+
}
429+
}
430+
431+
public static void testSendPushWithCid() {
432+
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
433+
PushPayload pushPayload = buildPushObject_android_cid();
434+
try {
435+
PushResult result = jPushClient.sendPush(pushPayload);
436+
LOG.info("Got result - " + result);
437+
} catch (APIConnectionException e) {
438+
LOG.error("Connection error. Should retry later. ", e);
439+
} catch (APIRequestException e) {
440+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
441+
LOG.info("HTTP Status: " + e.getStatus());
442+
LOG.info("Error Code: " + e.getErrorCode());
443+
LOG.info("Error Message: " + e.getErrorMessage());
444+
}
445+
}
446+
370447
}
371448

pom.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.2.21-SNAPSHOT</version>
6+
<version>3.3.0-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>
@@ -35,14 +35,14 @@
3535
<url>https://github.com/jpush/jpush-api-java-client</url>
3636
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
3737
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
38-
<tag>v3.2.20</tag>
38+
<tag>v3.3.0</tag>
3939
</scm>
4040

4141
<dependencies>
4242
<dependency>
4343
<groupId>cn.jpush.api</groupId>
4444
<artifactId>jiguang-common</artifactId>
45-
<version>1.0.6</version>
45+
<version>1.0.7</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>org.apache.httpcomponents</groupId>

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.Map;
44
import java.util.Set;
55

6+
import cn.jpush.api.push.CIDResult;
67
import com.google.gson.JsonObject;
78

89
import cn.jiguang.common.ClientConfig;
@@ -226,6 +227,18 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
226227
return _pushClient.sendPushValidate(payloadString);
227228
}
228229

230+
/**
231+
* Get cid list, the data form of cid is appKey-uuid.
232+
* @param count the count of cid list, from 1 to 1000. default is 1.
233+
* @param type default is push, option: schedule
234+
* @return CIDResult, an array of cid
235+
* @throws APIConnectionException connect exception
236+
* @throws APIRequestException request exception
237+
*/
238+
public CIDResult getCidList(int count, String type) throws APIConnectionException, APIRequestException {
239+
return _pushClient.getCidList(count, type);
240+
}
241+
229242

230243
// ------------------------------- Report API
231244

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package cn.jpush.api.push;
2+
3+
import cn.jiguang.common.resp.BaseResult;
4+
import com.google.gson.annotations.Expose;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
9+
public class CIDResult extends BaseResult {
10+
11+
@Expose public List<String> cidlist = new ArrayList<String>();
12+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.jpush.api.push;
2+
3+
import cn.jiguang.common.ClientConfig;
4+
import cn.jiguang.common.ServiceHelper;
5+
import cn.jiguang.common.connection.HttpProxy;
6+
import cn.jiguang.common.connection.IHttpClient;
7+
import cn.jiguang.common.connection.NativeHttpClient;
8+
import cn.jiguang.common.resp.APIConnectionException;
9+
import cn.jiguang.common.resp.APIRequestException;
10+
import cn.jiguang.common.resp.BaseResult;
11+
import cn.jiguang.common.resp.ResponseWrapper;
12+
import cn.jiguang.common.utils.Preconditions;
13+
import cn.jpush.api.push.model.PushPayload;
14+
15+
/**
16+
* Created by caiyaoguan on 2017/7/4.
17+
*/
18+
public class GroupPushClient {
19+
20+
private IHttpClient _httpClient;
21+
private String _baseUrl;
22+
private String _groupPushPath;
23+
24+
public GroupPushClient(String groupMasterSecret, String groupKey) {
25+
this(groupMasterSecret, groupKey, null, ClientConfig.getInstance());
26+
}
27+
28+
public GroupPushClient(String groupMasterSecret, String groupKey, HttpProxy proxy, ClientConfig conf) {
29+
ServiceHelper.checkBasic(groupKey, groupMasterSecret);
30+
this._baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
31+
this._groupPushPath = (String) conf.get(ClientConfig.GROUP_PUSH_PATH);
32+
String authCode = ServiceHelper.getBasicAuthorization("group-" + groupKey, groupMasterSecret);
33+
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
34+
}
35+
36+
public PushResult sendGroupPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
37+
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
38+
39+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _groupPushPath, pushPayload.toString());
40+
41+
return BaseResult.fromResponse(response, PushResult.class);
42+
}
43+
44+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,26 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
211211
return BaseResult.fromResponse(response, PushResult.class);
212212
}
213213

214+
/**
215+
* Get cid list, the data form of cid is appKey-uuid.
216+
* @param count the count of cid list, from 1 to 1000. default is 1.
217+
* @param type default is "push", option: "schedule"
218+
* @return CIDResult, an array of cid
219+
* @throws APIConnectionException connect exception
220+
* @throws APIRequestException request exception
221+
*/
222+
public CIDResult getCidList(int count, String type) throws APIConnectionException, APIRequestException {
223+
Preconditions.checkArgument(count >= 1 && count <= 1000, "count should not less than 1 or larger than 1000");
224+
Preconditions.checkArgument(type == null || type.equals("push") || type.equals("schedule"), "type should be \"push\" or \"schedule\"");
225+
ResponseWrapper responseWrapper;
226+
if (type != null) {
227+
responseWrapper = _httpClient.sendGet(_baseUrl + _pushPath + "/cid?count=" + count + "&type=" + type);
228+
} else {
229+
responseWrapper = _httpClient.sendGet(_baseUrl + _pushPath + "/cid?count=" + count);
230+
}
231+
return BaseResult.fromResponse(responseWrapper, CIDResult.class);
232+
}
233+
214234
public void setHttpClient(IHttpClient client) {
215235
this._httpClient = client;
216236
}

0 commit comments

Comments
 (0)