Skip to content

Commit dada04f

Browse files
author
许丹侠
committed
完善example
1 parent 675eebf commit dada04f

File tree

7 files changed

+105
-147
lines changed

7 files changed

+105
-147
lines changed

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

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
package main.java.cn.jpush.api.examples;
1+
package cn.jpush.api.examples;
22

33
import cn.jiguang.common.ClientConfig;
4-
import cn.jiguang.common.DeviceType;
54
import cn.jiguang.common.ServiceHelper;
65
import cn.jiguang.common.connection.NativeHttpClient;
76
import cn.jiguang.common.connection.NettyHttpClient;
@@ -26,10 +25,7 @@
2625

2726
import java.net.URI;
2827
import java.net.URISyntaxException;
29-
import java.util.Collection;
30-
import java.util.HashMap;
31-
import java.util.LinkedList;
32-
import java.util.Map;
28+
import java.util.*;
3329

3430
public class PushExample {
3531
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
@@ -49,6 +45,7 @@ public class PushExample {
4945
private static long sendTotalTime = 0;
5046

5147
public static void main(String[] args) {
48+
5249
testBatchSend();
5350
// testSendPushWithCustomConfig();
5451
// testSendIosAlert();
@@ -532,47 +529,51 @@ public static void testBatchSend() {
532529
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
533530
try {
534531
{
535-
BatchPushPayload batchPayload = new BatchPushPayload();
536-
SinglePayload payload1 = new SinglePayload();
537-
payload1.setMessage(Message.content("content1 by alias"))
532+
List<PushPayload> pushPayloadList = new ArrayList<>();
533+
PushPayload.Builder builder1 = PushPayload.newBuilder();
534+
builder1.setMessage(Message.content("content1 by alias"))
538535
.setNotification(Notification.alert(ALERT))
539-
.setPlatform(DeviceType.Android.toString())
536+
.setPlatform(Platform.all())
537+
.setAudience(Audience.all())
540538
.setOptions(Options.sendno())
541539
.setTarget("1507ffd3f79456757de");
542-
batchPayload.setPushList("1507ffd3f79456757de", payload1);
540+
pushPayloadList.add(builder1.build());
543541

544-
SinglePayload payload2 = new SinglePayload();
545-
payload2.setMessage(Message.content("content2 by alias"))
542+
PushPayload.Builder builder2 = PushPayload.newBuilder();
543+
builder2.setMessage(Message.content("content2 by alias"))
546544
.setNotification(Notification.alert(ALERT))
547-
.setPlatform("all")
545+
.setPlatform(Platform.android())
546+
.setAudience(Audience.all())
548547
.setOptions(Options.sendno())
549548
.setTarget("1507ffd3f79456757de");
550-
batchPayload.setPushList("1507efd3f49623582c", payload2);
549+
pushPayloadList.add(builder2.build());
551550

552-
PushResult result = jPushClient.batchSendPushByAlias(batchPayload);
553-
LOG.info("batchSendPushByAlias param: {}, result: {}", batchPayload, result);
551+
PushResult result = jPushClient.batchSendPushByAlias(pushPayloadList);
552+
LOG.info("batchSendPushByAlias param: {}, result: {}", pushPayloadList, result);
554553
}
555554

556555
{
557-
BatchPushPayload batchPayload = new BatchPushPayload();
558-
SinglePayload payload1 = new SinglePayload();
559-
payload1.setMessage(Message.content("content1 by regId"))
556+
List<PushPayload> pushPayloadList = new ArrayList<>();
557+
PushPayload.Builder builder1 = PushPayload.newBuilder();
558+
builder1.setMessage(Message.content("content1 by regId"))
560559
.setNotification(Notification.alert(ALERT))
561-
.setPlatform(DeviceType.IOS.toString())
560+
.setPlatform(Platform.android())
561+
.setAudience(Audience.all())
562562
.setOptions(Options.sendno())
563563
.setTarget("1507ffd3f79456757de");
564-
batchPayload.setPushList("1507ffd3f79456757de", payload1);
564+
pushPayloadList.add(builder1.build());
565565

566-
SinglePayload payload2 = new SinglePayload();
567-
payload2.setMessage(Message.content("content2 by regId"))
566+
PushPayload.Builder builder2 = PushPayload.newBuilder();
567+
builder2.setMessage(Message.content("content2 by regId"))
568568
.setNotification(Notification.alert(ALERT))
569-
.setPlatform("all")
569+
.setAudience(Audience.all())
570+
.setPlatform(Platform.ios())
570571
.setOptions(Options.sendno())
571572
.setTarget("1507ffd3f79456757de");
572-
batchPayload.setPushList("1507efd3f49623582c", payload2);
573+
pushPayloadList.add(builder2.build());
573574

574-
PushResult result = jPushClient.batchSendPushByRegId(batchPayload);
575-
LOG.info("batchSendPushByRegId param: {}, result: {}", batchPayload, result);
575+
PushResult result = jPushClient.batchSendPushByRegId(pushPayloadList);
576+
LOG.info("batchSendPushByRegId param: {}, result: {}", pushPayloadList, result);
576577
}
577578

578579
} catch (APIConnectionException e) {

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api;
22

3+
import java.util.List;
34
import java.util.Map;
45
import java.util.Set;
56

@@ -220,12 +221,12 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
220221
return _pushClient.sendPushValidate(payloadString);
221222
}
222223

223-
public PushResult batchSendPushByRegId(BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
224-
return _pushClient.batchSendPushByRegId(pushList);
224+
public PushResult batchSendPushByRegId(List<PushPayload> pushPayloadList) throws APIConnectionException, APIRequestException {
225+
return _pushClient.batchSendPushByRegId(pushPayloadList);
225226
}
226227

227-
public PushResult batchSendPushByAlias(BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
228-
return _pushClient.batchSendPushByAlias(pushList);
228+
public PushResult batchSendPushByAlias(List<PushPayload> pushPayloadList) throws APIConnectionException, APIRequestException {
229+
return _pushClient.batchSendPushByAlias(pushPayloadList);
229230
}
230231

231232
/**

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

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import cn.jpush.api.push.model.audience.Audience;
1616
import com.google.gson.*;
1717

18+
import java.util.List;
1819
import java.util.Map;
1920

2021
/**
@@ -220,32 +221,37 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
220221
return BaseResult.fromResponse(response, PushResult.class);
221222
}
222223

223-
public PushResult batchSendPushByRegId(BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
224-
return batchSendPush(_baseUrl + batchRegidPushPath, pushList);
224+
public PushResult batchSendPushByRegId(List<PushPayload> pushPayloadList) throws APIConnectionException, APIRequestException {
225+
return batchSendPush(_baseUrl + batchRegidPushPath, pushPayloadList);
225226
}
226227

227-
public PushResult batchSendPushByAlias(BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
228-
return batchSendPush(_baseUrl + batchAliasPushPath, pushList);
228+
public PushResult batchSendPushByAlias(List<PushPayload> pushPayloadList) throws APIConnectionException, APIRequestException {
229+
return batchSendPush(_baseUrl + batchAliasPushPath, pushPayloadList);
229230
}
230231

231-
public PushResult batchSendPush(String url, BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
232+
public PushResult batchSendPush(String url, List<PushPayload> pushPayloadList) throws APIConnectionException, APIRequestException {
232233

233-
Preconditions.checkArgument((null != pushList), "param should not be null");
234-
Preconditions.checkArgument((null != pushList.getPushList()), "pushList should not be null");
234+
Preconditions.checkArgument((null != pushPayloadList), "param should not be null");
235+
Preconditions.checkArgument((!pushPayloadList.isEmpty()), "pushPayloadList should not be empty");
235236

236-
for (SinglePayload payload : pushList.getPushList().values()) {
237-
if (_timeToLive >= 0) {
238-
payload.resetOptionsTimeToLive(_timeToLive);
239-
}
240-
if (_apnsProduction > 0) {
241-
payload.resetOptionsApnsProduction(true);
242-
} else if(_apnsProduction == 0) {
243-
payload.resetOptionsApnsProduction(false);
237+
Gson gson = new Gson();
238+
239+
JsonObject json = new JsonObject();
240+
241+
CIDResult cidResult = getCidList(pushPayloadList.size(), "push");
242+
int i = 0;
243+
JsonObject pushPayLoadList = new JsonObject();
244+
// setting cid
245+
for (PushPayload payload : pushPayloadList) {
246+
if (payload.getCid() != null && !payload.getCid().trim().isEmpty()) {
247+
pushPayLoadList.add(payload.getCid(), payload.toJSON());
248+
continue;
244249
}
250+
pushPayLoadList.add(cidResult.cidlist.get(i++), payload.toJSON());
245251
}
252+
json.add("pushlist", pushPayLoadList);
246253

247-
Gson gson = new Gson();
248-
ResponseWrapper response = _httpClient.sendPost(url, getEncryptData(gson.toJson(pushList)));
254+
ResponseWrapper response = _httpClient.sendPost(url, getEncryptData(gson.toJson(pushPayLoadList)));
249255

250256
return BaseResult.fromResponse(response, PushResult.class);
251257
}

src/main/java/cn/jpush/api/push/model/BatchPushPayload.java

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

src/main/java/cn/jpush/api/push/model/PushPayload.java

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.push.model;
22

3+
import cn.jiguang.common.utils.StringUtils;
34
import com.google.gson.Gson;
45
import com.google.gson.GsonBuilder;
56
import com.google.gson.JsonElement;
@@ -23,16 +24,25 @@
2324
*
2425
*/
2526
public class PushPayload implements PushModel {
27+
2628
private static final String PLATFORM = "platform";
2729
private static final String AUDIENCE = "audience";
2830
private static final String NOTIFICATION = "notification";
2931
private static final String MESSAGE = "message";
3032
private static final String OPTIONS = "options";
3133
private static final String SMS = "sms_message";
3234
private static final String CID = "cid";
35+
private static final String TARGET = "target";
3336

34-
private static final int MAX_GLOBAL_ENTITY_LENGTH = 4000; // Definition acording to JPush Docs
35-
private static final int MAX_IOS_PAYLOAD_LENGTH = 2000; // Definition acording to JPush Docs
37+
/**
38+
* Definition acording to JPush Docs
39+
*/
40+
private static final int MAX_GLOBAL_ENTITY_LENGTH = 4000;
41+
42+
/**
43+
* Definition acording to JPush Docs
44+
*/
45+
private static final int MAX_IOS_PAYLOAD_LENGTH = 2000;
3646

3747
private static Gson _gson = new GsonBuilder().disableHtmlEscaping().create();
3848

@@ -43,9 +53,10 @@ public class PushPayload implements PushModel {
4353
private Options options;
4454
private SMS sms;
4555
private String cid;
56+
private String target;
4657

4758
private PushPayload(Platform platform, Audience audience,
48-
Notification notification, Message message, Options options, SMS sms, String cid) {
59+
Notification notification, Message message, Options options, SMS sms, String cid, String target) {
4960
this.platform = platform;
5061
this.audience = audience;
5162
this.notification = notification;
@@ -55,6 +66,18 @@ private PushPayload(Platform platform, Audience audience,
5566
this.cid = cid;
5667
}
5768

69+
public Platform getPlatform() {
70+
return platform;
71+
}
72+
73+
public String getTarget() {
74+
return target;
75+
}
76+
77+
public String getCid() {
78+
return cid;
79+
}
80+
5881
/**
5982
* The entrance for building a PushPayload object.
6083
* @return PushPayload builder
@@ -160,6 +183,9 @@ public JsonElement toJSON() {
160183
if (null != cid) {
161184
json.addProperty(CID, cid);
162185
}
186+
if (null != target) {
187+
json.addProperty(TARGET, target);
188+
}
163189

164190
return json;
165191
}
@@ -219,6 +245,12 @@ public static class Builder {
219245
private Options options = null;
220246
private SMS sms = null;
221247
private String cid;
248+
private String target;
249+
250+
public Builder setTarget(String target) {
251+
this.target = target;
252+
return this;
253+
}
222254

223255
public Builder setPlatform(Platform platform) {
224256
this.platform = platform;
@@ -256,15 +288,25 @@ public Builder setCid(String cid) {
256288
}
257289

258290
public PushPayload build() {
259-
Preconditions.checkArgument(! (null == audience || null == platform), "audience and platform both should be set.");
260-
Preconditions.checkArgument(! (null == notification && null == message), "notification or message should be set at least one.");
291+
292+
if (StringUtils.isTrimedEmpty(target)) {
293+
Preconditions.checkArgument(!(null == audience || null == platform),
294+
"audience and platform both should be set.");
295+
}
296+
if (!StringUtils.isTrimedEmpty(target)) {
297+
Preconditions.checkArgument(!StringUtils.isTrimedEmpty(target) && null != platform,
298+
"target and platform should be set.");
299+
}
300+
301+
Preconditions.checkArgument(! (null == notification && null == message),
302+
"notification or message should be set at least one.");
261303

262304
// if options is not set, a sendno will be generated for tracing easily
263305
if (null == options) {
264306
options = Options.sendno();
265307
}
266308

267-
return new PushPayload(platform, audience, notification, message, options, sms, cid);
309+
return new PushPayload(platform, audience, notification, message, options, sms, cid, target);
268310
}
269311
}
270312
}

0 commit comments

Comments
 (0)