Skip to content

Commit 675eebf

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

File tree

10 files changed

+283
-85
lines changed

10 files changed

+283
-85
lines changed

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

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

33
import cn.jiguang.common.ClientConfig;
4+
import cn.jiguang.common.DeviceType;
45
import cn.jiguang.common.ServiceHelper;
56
import cn.jiguang.common.connection.NativeHttpClient;
67
import cn.jiguang.common.connection.NettyHttpClient;
@@ -34,20 +35,21 @@ public class PushExample {
3435
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
3536

3637
// demo App defined in resources/jpush-api.conf
37-
protected static final String APP_KEY ="7b4b94cca0d185d611e53cca";
38+
protected static final String APP_KEY = "7b4b94cca0d185d611e53cca";
3839
protected static final String MASTER_SECRET = "860803cf613ed54aa3b941a8";
3940
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
4041
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";
41-
42-
public static final String TITLE = "Test from API example";
42+
43+
public static final String TITLE = "Test from API example";
4344
public static final String ALERT = "Test from API Example - alert";
4445
public static final String MSG_CONTENT = "Test from API Example - msgContent";
4546
public static final String REGISTRATION_ID = "0900e8d85ef";
4647
public static final String TAG = "tag_api";
4748
public static long sendCount = 0;
4849
private static long sendTotalTime = 0;
4950

50-
public static void main(String[] args) {
51+
public static void main(String[] args) {
52+
testBatchSend();
5153
// testSendPushWithCustomConfig();
5254
// testSendIosAlert();
5355
// testSendPush();
@@ -56,9 +58,9 @@ public static void main(String[] args) {
5658
testSendPush_fromJSON();
5759
// testSendPushWithCallback();
5860
// testSendPushWithCid();
59-
}
61+
}
6062

61-
// 使用 NettyHttpClient 异步接口发送请求
63+
// 使用 NettyHttpClient 异步接口发送请求
6264
public static void testSendPushWithCallback() {
6365
ClientConfig clientConfig = ClientConfig.getInstance();
6466
String host = (String) clientConfig.get(ClientConfig.PUSH_HOST_NAME);
@@ -78,14 +80,14 @@ public void onSucceed(ResponseWrapper responseWrapper) {
7880
}
7981
}
8082

81-
public static void testSendPush() {
82-
ClientConfig clientConfig = ClientConfig.getInstance();
83+
public static void testSendPush() {
84+
ClientConfig clientConfig = ClientConfig.getInstance();
8385
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
8486
// String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
8587
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
8688
// Call setHttpClient to set httpClient,
8789
// If you don't invoke this method, default httpClient will use NativeHttpClient.
88-
90+
8991
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
9092
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
9193
// jpushClient.getPushClient().setHttpClient(httpClient);
@@ -149,7 +151,7 @@ public static void testSendPushWithEncrypt() {
149151
}
150152
}
151153

152-
//use String to build PushPayload instance
154+
//use String to build PushPayload instance
153155
public static void testSendPush_fromJSON() {
154156
ClientConfig clientConfig = ClientConfig.getInstance();
155157
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
@@ -165,7 +167,7 @@ public static void testSendPush_fromJSON() {
165167

166168
} catch (APIConnectionException e) {
167169
LOG.error("Connection error. Should retry later. ", e);
168-
// LOG.error("Sendno: " + payload.getSendno());
170+
// LOG.error("Sendno: " + payload.getSendno());
169171

170172
} catch (APIRequestException e) {
171173
LOG.error("Error response from JPush server. Should review and fix it. ", e);
@@ -191,7 +193,7 @@ public static void testSendPushes() {
191193
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
192194
jpushClient.getPushClient().setHttpClient(httpClient);
193195
final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
194-
for(int i=0;i<10;i++) {
196+
for (int i = 0; i < 10; i++) {
195197
Thread thread = new Thread() {
196198
public void run() {
197199
for (int j = 0; j < 200; j++) {
@@ -249,42 +251,42 @@ public void testSendGroupPush() {
249251
LOG.error("Sendno: " + payload.getSendno());
250252
}
251253
}
252-
253-
public static PushPayload buildPushObject_all_all_alert() {
254-
return PushPayload.alertAll(ALERT);
255-
}
256-
254+
255+
public static PushPayload buildPushObject_all_all_alert() {
256+
return PushPayload.alertAll(ALERT);
257+
}
258+
257259
public static PushPayload buildPushObject_all_alias_alert() {
258260
return PushPayload.newBuilder()
259261
.setPlatform(Platform.all())
260262
.setAudience(Audience.alias("alias1"))
261263
.setNotification(Notification.alert(ALERT))
262264
.build();
263265
}
264-
266+
265267
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
266268
return PushPayload.newBuilder()
267269
.setPlatform(Platform.android())
268270
.setAudience(Audience.tag("tag1"))
269271
.setNotification(Notification.android(ALERT, TITLE, null))
270272
.build();
271273
}
272-
274+
273275
public static PushPayload buildPushObject_android_and_ios() {
274276
Map<String, String> extras = new HashMap<String, String>();
275277
extras.put("test", "https://community.jiguang.cn/push");
276278
return PushPayload.newBuilder()
277279
.setPlatform(Platform.android_ios())
278280
.setAudience(Audience.all())
279281
.setNotification(Notification.newBuilder()
280-
.setAlert("alert content")
281-
.addPlatformNotification(AndroidNotification.newBuilder()
282-
.setTitle("Android Title")
282+
.setAlert("alert content")
283+
.addPlatformNotification(AndroidNotification.newBuilder()
284+
.setTitle("Android Title")
283285
.addExtras(extras).build())
284-
.addPlatformNotification(IosNotification.newBuilder()
285-
.incrBadge(1)
286-
.addExtra("extra_key", "extra_value").build())
287-
.build())
286+
.addPlatformNotification(IosNotification.newBuilder()
287+
.incrBadge(1)
288+
.addExtra("extra_key", "extra_value").build())
289+
.build())
288290
.build();
289291
}
290292

@@ -318,12 +320,12 @@ public static void buildPushObject_with_extra() {
318320

319321
System.out.println(payload.toJSON());
320322
}
321-
323+
322324
public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
323-
JsonObject sound = new JsonObject();
324-
sound.add("critical", new JsonPrimitive(1));
325-
sound.add("name", new JsonPrimitive("default"));
326-
sound.add("volume", new JsonPrimitive(0.2));
325+
JsonObject sound = new JsonObject();
326+
sound.add("critical", new JsonPrimitive(1));
327+
sound.add("name", new JsonPrimitive("default"));
328+
sound.add("volume", new JsonPrimitive(0.2));
327329
return PushPayload.newBuilder()
328330
.setPlatform(Platform.ios())
329331
.setAudience(Audience.tag_and("tag1", "tag_all"))
@@ -337,24 +339,24 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage()
337339
.addExtra("from", "JPush")
338340
.build())
339341
.build())
340-
.setMessage(Message.content(MSG_CONTENT))
341-
.setOptions(Options.newBuilder()
342-
.setApnsProduction(true)
343-
.build())
344-
.build();
342+
.setMessage(Message.content(MSG_CONTENT))
343+
.setOptions(Options.newBuilder()
344+
.setApnsProduction(true)
345+
.build())
346+
.build();
345347
}
346348

347349
public static PushPayload buildPushObject_android_newly_support() {
348-
350+
349351
JsonObject inbox = new JsonObject();
350352
inbox.add("line1", new JsonPrimitive("line1 string"));
351353
inbox.add("line2", new JsonPrimitive("line2 string"));
352354
inbox.add("contentTitle", new JsonPrimitive("title string"));
353355
inbox.add("summaryText", new JsonPrimitive("+3 more"));
354-
356+
355357
JsonObject intent = new JsonObject();
356358
intent.add("url", new JsonPrimitive("intent:#Intent;component=com.jiguang.push/com.example.jpushdemo.SettingActivity;end"));
357-
359+
358360
Notification notification = Notification.newBuilder()
359361
.addPlatformNotification(AndroidNotification.newBuilder()
360362
.setAlert(ALERT)
@@ -380,7 +382,7 @@ public static PushPayload buildPushObject_android_newly_support() {
380382
.build())
381383
.build();
382384
}
383-
385+
384386
public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
385387
return PushPayload.newBuilder()
386388
.setPlatform(Platform.android_ios())
@@ -404,16 +406,16 @@ public static PushPayload buildPushObject_all_tag_not() {
404406
}
405407

406408
public static PushPayload buildPushObject_android_cid() {
407-
Collection<String> list =new LinkedList<String>();
408-
list.add("1507bfd3f79558957de");
409-
list.add("1507bfd3f79554957de");
410-
list.add("1507bfd3f79555957de");
411-
list.add("1507bfd3f79556957de");
412-
list.add("1507ffd3f79545957de");
413-
list.add("1507ffd3f79457957de");
414-
list.add("1507ffd3f79456757de");
415-
416-
409+
Collection<String> list = new LinkedList<String>();
410+
list.add("1507bfd3f79558957de");
411+
list.add("1507bfd3f79554957de");
412+
list.add("1507bfd3f79555957de");
413+
list.add("1507bfd3f79556957de");
414+
list.add("1507ffd3f79545957de");
415+
list.add("1507ffd3f79457957de");
416+
list.add("1507ffd3f79456757de");
417+
418+
417419
return PushPayload.newBuilder()
418420
.setPlatform(Platform.android())
419421
// .setAudience(Audience.registrationId("1507bfd3f79558957de"))
@@ -474,10 +476,10 @@ public static void testSendWithSMS() {
474476
try {
475477
// SMS sms = SMS.content(1, 10);
476478
SMS sms = SMS.newBuilder()
477-
.setDelayTime(1000)
478-
.setTempID(2000)
479-
.addPara("Test", 1)
480-
.build();
479+
.setDelayTime(1000)
480+
.setTempID(2000)
481+
.addPara("Test", 1)
482+
.build();
481483
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "alias1");
482484
LOG.info("Got result - " + result);
483485
} catch (APIConnectionException e) {
@@ -521,5 +523,67 @@ public static void testSendPushWithCid() {
521523
}
522524
}
523525

526+
/**
527+
* 批量单推接口
528+
* https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
529+
*/
530+
public static void testBatchSend() {
531+
532+
JPushClient jPushClient = new JPushClient(MASTER_SECRET, APP_KEY);
533+
try {
534+
{
535+
BatchPushPayload batchPayload = new BatchPushPayload();
536+
SinglePayload payload1 = new SinglePayload();
537+
payload1.setMessage(Message.content("content1 by alias"))
538+
.setNotification(Notification.alert(ALERT))
539+
.setPlatform(DeviceType.Android.toString())
540+
.setOptions(Options.sendno())
541+
.setTarget("1507ffd3f79456757de");
542+
batchPayload.setPushList("1507ffd3f79456757de", payload1);
543+
544+
SinglePayload payload2 = new SinglePayload();
545+
payload2.setMessage(Message.content("content2 by alias"))
546+
.setNotification(Notification.alert(ALERT))
547+
.setPlatform("all")
548+
.setOptions(Options.sendno())
549+
.setTarget("1507ffd3f79456757de");
550+
batchPayload.setPushList("1507efd3f49623582c", payload2);
551+
552+
PushResult result = jPushClient.batchSendPushByAlias(batchPayload);
553+
LOG.info("batchSendPushByAlias param: {}, result: {}", batchPayload, result);
554+
}
555+
556+
{
557+
BatchPushPayload batchPayload = new BatchPushPayload();
558+
SinglePayload payload1 = new SinglePayload();
559+
payload1.setMessage(Message.content("content1 by regId"))
560+
.setNotification(Notification.alert(ALERT))
561+
.setPlatform(DeviceType.IOS.toString())
562+
.setOptions(Options.sendno())
563+
.setTarget("1507ffd3f79456757de");
564+
batchPayload.setPushList("1507ffd3f79456757de", payload1);
565+
566+
SinglePayload payload2 = new SinglePayload();
567+
payload2.setMessage(Message.content("content2 by regId"))
568+
.setNotification(Notification.alert(ALERT))
569+
.setPlatform("all")
570+
.setOptions(Options.sendno())
571+
.setTarget("1507ffd3f79456757de");
572+
batchPayload.setPushList("1507efd3f49623582c", payload2);
573+
574+
PushResult result = jPushClient.batchSendPushByRegId(batchPayload);
575+
LOG.info("batchSendPushByRegId param: {}, result: {}", batchPayload, result);
576+
}
577+
578+
} catch (APIConnectionException e) {
579+
LOG.error("Connection error. Should retry later. ", e);
580+
} catch (APIRequestException e) {
581+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
582+
LOG.info("HTTP Status: " + e.getStatus());
583+
LOG.info("Error Code: " + e.getErrorCode());
584+
LOG.info("Error Message: " + e.getErrorMessage());
585+
}
586+
}
587+
524588
}
525589

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

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

3-
import cn.jpush.api.report.MessageStatus;
3+
import cn.jpush.api.report.*;
44
import cn.jpush.api.report.model.CheckMessagePayload;
55
import org.slf4j.Logger;
66
import org.slf4j.LoggerFactory;
@@ -9,9 +9,6 @@
99
import cn.jiguang.common.resp.APIConnectionException;
1010
import cn.jiguang.common.resp.APIRequestException;
1111
import cn.jpush.api.JPushClient;
12-
import cn.jpush.api.report.MessagesResult;
13-
import cn.jpush.api.report.ReceivedsResult;
14-
import cn.jpush.api.report.UsersResult;
1512

1613
import java.util.Map;
1714

@@ -26,6 +23,8 @@ public class ReportsExample {
2623
public static final String REGISTRATION_ID3 = "18071adc030dcba91c0";
2724

2825
public static void main(String[] args) {
26+
testGetReceivedsDetail();
27+
testGetMessagesDetail();
2928
// testGetReport();
3029
// testGetMessages();
3130
// testGetUsers();
@@ -106,5 +105,48 @@ public static void testGetMessageStatus() {
106105
}
107106
}
108107

108+
/**
109+
* 消息统计
110+
* https://docs.jiguang.cn/jpush/server/push/rest_api_v3_report/#vip
111+
*/
112+
public static void testGetMessagesDetail() {
113+
114+
JPushClient jPushClient = new JPushClient(masterSecret, appKey);
115+
String msgIds = "3993287034,3993287035,3993287036";
116+
try {
117+
MessageDetailResult result = jPushClient.getMessagesDetail(msgIds);
118+
LOG.info("msgIds: {}, MessageDetail: {}", msgIds, result);
119+
} catch (APIConnectionException e) {
120+
LOG.error("Connection error. Should retry later. ", e);
121+
} catch (APIRequestException e) {
122+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
123+
LOG.info("HTTP Status: " + e.getStatus());
124+
LOG.info("Error Code: " + e.getErrorCode());
125+
LOG.info("Error Message: " + e.getErrorMessage());
126+
}
127+
128+
}
129+
130+
/**
131+
* 送达详情
132+
* https://docs.jiguang.cn/jpush/server/push/rest_api_v3_report/#_7
133+
*/
134+
public static void testGetReceivedsDetail() {
135+
136+
JPushClient jPushClient = new JPushClient(masterSecret, appKey);
137+
String msgIds = "3993287034,3993287035,3993287036";
138+
try {
139+
ReceivedsResult result = jPushClient.getReceivedsDetail(msgIds);
140+
LOG.info("msgIds: {}, ReceivedsDetail: {}", msgIds, result);
141+
} catch (APIConnectionException e) {
142+
LOG.error("Connection error. Should retry later. ", e);
143+
} catch (APIRequestException e) {
144+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
145+
LOG.info("HTTP Status: " + e.getStatus());
146+
LOG.info("Error Code: " + e.getErrorCode());
147+
LOG.info("Error Message: " + e.getErrorMessage());
148+
}
149+
}
150+
109151
}
110152

0 commit comments

Comments
 (0)