1
- package main . java . cn .jpush .api .examples ;
1
+ package cn .jpush .api .examples ;
2
2
3
3
import cn .jiguang .common .ClientConfig ;
4
4
import cn .jiguang .common .ServiceHelper ;
25
25
26
26
import java .net .URI ;
27
27
import java .net .URISyntaxException ;
28
- import java .util .Collection ;
29
- import java .util .HashMap ;
30
- import java .util .LinkedList ;
31
- import java .util .Map ;
28
+ import java .util .*;
32
29
33
30
public class PushExample {
34
31
protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
35
32
36
33
// demo App defined in resources/jpush-api.conf
37
- protected static final String APP_KEY ="7b4b94cca0d185d611e53cca" ;
34
+ protected static final String APP_KEY = "7b4b94cca0d185d611e53cca" ;
38
35
protected static final String MASTER_SECRET = "860803cf613ed54aa3b941a8" ;
39
36
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c" ;
40
37
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e" ;
41
-
42
- public static final String TITLE = "Test from API example" ;
38
+
39
+ public static final String TITLE = "Test from API example" ;
43
40
public static final String ALERT = "Test from API Example - alert" ;
44
41
public static final String MSG_CONTENT = "Test from API Example - msgContent" ;
45
42
public static final String REGISTRATION_ID = "0900e8d85ef" ;
46
43
public static final String TAG = "tag_api" ;
47
44
public static long sendCount = 0 ;
48
45
private static long sendTotalTime = 0 ;
49
46
50
- public static void main (String [] args ) {
47
+ public static void main (String [] args ) {
48
+
49
+ testBatchSend ();
51
50
// testSendPushWithCustomConfig();
52
51
// testSendIosAlert();
53
52
// testSendPush();
@@ -56,9 +55,9 @@ public static void main(String[] args) {
56
55
testSendPush_fromJSON ();
57
56
// testSendPushWithCallback();
58
57
// testSendPushWithCid();
59
- }
58
+ }
60
59
61
- // 使用 NettyHttpClient 异步接口发送请求
60
+ // 使用 NettyHttpClient 异步接口发送请求
62
61
public static void testSendPushWithCallback () {
63
62
ClientConfig clientConfig = ClientConfig .getInstance ();
64
63
String host = (String ) clientConfig .get (ClientConfig .PUSH_HOST_NAME );
@@ -78,14 +77,14 @@ public void onSucceed(ResponseWrapper responseWrapper) {
78
77
}
79
78
}
80
79
81
- public static void testSendPush () {
82
- ClientConfig clientConfig = ClientConfig .getInstance ();
80
+ public static void testSendPush () {
81
+ ClientConfig clientConfig = ClientConfig .getInstance ();
83
82
final JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
84
83
// String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
85
84
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
86
85
// Call setHttpClient to set httpClient,
87
86
// If you don't invoke this method, default httpClient will use NativeHttpClient.
88
-
87
+
89
88
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
90
89
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
91
90
// jpushClient.getPushClient().setHttpClient(httpClient);
@@ -149,7 +148,7 @@ public static void testSendPushWithEncrypt() {
149
148
}
150
149
}
151
150
152
- //use String to build PushPayload instance
151
+ //use String to build PushPayload instance
153
152
public static void testSendPush_fromJSON () {
154
153
ClientConfig clientConfig = ClientConfig .getInstance ();
155
154
JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
@@ -165,7 +164,7 @@ public static void testSendPush_fromJSON() {
165
164
166
165
} catch (APIConnectionException e ) {
167
166
LOG .error ("Connection error. Should retry later. " , e );
168
- // LOG.error("Sendno: " + payload.getSendno());
167
+ // LOG.error("Sendno: " + payload.getSendno());
169
168
170
169
} catch (APIRequestException e ) {
171
170
LOG .error ("Error response from JPush server. Should review and fix it. " , e );
@@ -191,7 +190,7 @@ public static void testSendPushes() {
191
190
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
192
191
jpushClient .getPushClient ().setHttpClient (httpClient );
193
192
final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage ();
194
- for (int i = 0 ; i < 10 ;i ++) {
193
+ for (int i = 0 ; i < 10 ; i ++) {
195
194
Thread thread = new Thread () {
196
195
public void run () {
197
196
for (int j = 0 ; j < 200 ; j ++) {
@@ -249,42 +248,42 @@ public void testSendGroupPush() {
249
248
LOG .error ("Sendno: " + payload .getSendno ());
250
249
}
251
250
}
252
-
253
- public static PushPayload buildPushObject_all_all_alert () {
254
- return PushPayload .alertAll (ALERT );
255
- }
256
-
251
+
252
+ public static PushPayload buildPushObject_all_all_alert () {
253
+ return PushPayload .alertAll (ALERT );
254
+ }
255
+
257
256
public static PushPayload buildPushObject_all_alias_alert () {
258
257
return PushPayload .newBuilder ()
259
258
.setPlatform (Platform .all ())
260
259
.setAudience (Audience .alias ("alias1" ))
261
260
.setNotification (Notification .alert (ALERT ))
262
261
.build ();
263
262
}
264
-
263
+
265
264
public static PushPayload buildPushObject_android_tag_alertWithTitle () {
266
265
return PushPayload .newBuilder ()
267
266
.setPlatform (Platform .android ())
268
267
.setAudience (Audience .tag ("tag1" ))
269
268
.setNotification (Notification .android (ALERT , TITLE , null ))
270
269
.build ();
271
270
}
272
-
271
+
273
272
public static PushPayload buildPushObject_android_and_ios () {
274
273
Map <String , String > extras = new HashMap <String , String >();
275
274
extras .put ("test" , "https://community.jiguang.cn/push" );
276
275
return PushPayload .newBuilder ()
277
276
.setPlatform (Platform .android_ios ())
278
277
.setAudience (Audience .all ())
279
278
.setNotification (Notification .newBuilder ()
280
- .setAlert ("alert content" )
281
- .addPlatformNotification (AndroidNotification .newBuilder ()
282
- .setTitle ("Android Title" )
279
+ .setAlert ("alert content" )
280
+ .addPlatformNotification (AndroidNotification .newBuilder ()
281
+ .setTitle ("Android Title" )
283
282
.addExtras (extras ).build ())
284
- .addPlatformNotification (IosNotification .newBuilder ()
285
- .incrBadge (1 )
286
- .addExtra ("extra_key" , "extra_value" ).build ())
287
- .build ())
283
+ .addPlatformNotification (IosNotification .newBuilder ()
284
+ .incrBadge (1 )
285
+ .addExtra ("extra_key" , "extra_value" ).build ())
286
+ .build ())
288
287
.build ();
289
288
}
290
289
@@ -318,12 +317,12 @@ public static void buildPushObject_with_extra() {
318
317
319
318
System .out .println (payload .toJSON ());
320
319
}
321
-
320
+
322
321
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 ));
322
+ JsonObject sound = new JsonObject ();
323
+ sound .add ("critical" , new JsonPrimitive (1 ));
324
+ sound .add ("name" , new JsonPrimitive ("default" ));
325
+ sound .add ("volume" , new JsonPrimitive (0.2 ));
327
326
return PushPayload .newBuilder ()
328
327
.setPlatform (Platform .ios ())
329
328
.setAudience (Audience .tag_and ("tag1" , "tag_all" ))
@@ -337,24 +336,24 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage()
337
336
.addExtra ("from" , "JPush" )
338
337
.build ())
339
338
.build ())
340
- .setMessage (Message .content (MSG_CONTENT ))
341
- .setOptions (Options .newBuilder ()
342
- .setApnsProduction (true )
343
- .build ())
344
- .build ();
339
+ .setMessage (Message .content (MSG_CONTENT ))
340
+ .setOptions (Options .newBuilder ()
341
+ .setApnsProduction (true )
342
+ .build ())
343
+ .build ();
345
344
}
346
345
347
346
public static PushPayload buildPushObject_android_newly_support () {
348
-
347
+
349
348
JsonObject inbox = new JsonObject ();
350
349
inbox .add ("line1" , new JsonPrimitive ("line1 string" ));
351
350
inbox .add ("line2" , new JsonPrimitive ("line2 string" ));
352
351
inbox .add ("contentTitle" , new JsonPrimitive ("title string" ));
353
352
inbox .add ("summaryText" , new JsonPrimitive ("+3 more" ));
354
-
353
+
355
354
JsonObject intent = new JsonObject ();
356
355
intent .add ("url" , new JsonPrimitive ("intent:#Intent;component=com.jiguang.push/com.example.jpushdemo.SettingActivity;end" ));
357
-
356
+
358
357
Notification notification = Notification .newBuilder ()
359
358
.addPlatformNotification (AndroidNotification .newBuilder ()
360
359
.setAlert (ALERT )
@@ -380,7 +379,7 @@ public static PushPayload buildPushObject_android_newly_support() {
380
379
.build ())
381
380
.build ();
382
381
}
383
-
382
+
384
383
public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras () {
385
384
return PushPayload .newBuilder ()
386
385
.setPlatform (Platform .android_ios ())
@@ -404,16 +403,16 @@ public static PushPayload buildPushObject_all_tag_not() {
404
403
}
405
404
406
405
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
-
406
+ Collection <String > list = new LinkedList <String >();
407
+ list .add ("1507bfd3f79558957de" );
408
+ list .add ("1507bfd3f79554957de" );
409
+ list .add ("1507bfd3f79555957de" );
410
+ list .add ("1507bfd3f79556957de" );
411
+ list .add ("1507ffd3f79545957de" );
412
+ list .add ("1507ffd3f79457957de" );
413
+ list .add ("1507ffd3f79456757de" );
414
+
415
+
417
416
return PushPayload .newBuilder ()
418
417
.setPlatform (Platform .android ())
419
418
// .setAudience(Audience.registrationId("1507bfd3f79558957de"))
@@ -474,10 +473,10 @@ public static void testSendWithSMS() {
474
473
try {
475
474
// SMS sms = SMS.content(1, 10);
476
475
SMS sms = SMS .newBuilder ()
477
- .setDelayTime (1000 )
478
- .setTempID (2000 )
479
- .addPara ("Test" , 1 )
480
- .build ();
476
+ .setDelayTime (1000 )
477
+ .setTempID (2000 )
478
+ .addPara ("Test" , 1 )
479
+ .build ();
481
480
PushResult result = jpushClient .sendAndroidMessageWithAlias ("Test SMS" , "test sms" , sms , "alias1" );
482
481
LOG .info ("Got result - " + result );
483
482
} catch (APIConnectionException e ) {
@@ -521,5 +520,71 @@ public static void testSendPushWithCid() {
521
520
}
522
521
}
523
522
523
+ /**
524
+ * 批量单推接口
525
+ * https://docs.jiguang.cn/jpush/server/push/rest_api_v3_push/#vip
526
+ */
527
+ public static void testBatchSend () {
528
+
529
+ JPushClient jPushClient = new JPushClient (MASTER_SECRET , APP_KEY );
530
+ try {
531
+ {
532
+ List <PushPayload > pushPayloadList = new ArrayList <>();
533
+ PushPayload .Builder builder1 = PushPayload .newBuilder ();
534
+ builder1 .setMessage (Message .content ("content1 by alias" ))
535
+ .setNotification (Notification .alert (ALERT ))
536
+ .setPlatform (Platform .all ())
537
+ .setAudience (Audience .all ())
538
+ .setOptions (Options .sendno ())
539
+ .setTarget ("1507ffd3f79456757de" );
540
+ pushPayloadList .add (builder1 .build ());
541
+
542
+ PushPayload .Builder builder2 = PushPayload .newBuilder ();
543
+ builder2 .setMessage (Message .content ("content2 by alias" ))
544
+ .setNotification (Notification .alert (ALERT ))
545
+ .setPlatform (Platform .android ())
546
+ .setAudience (Audience .all ())
547
+ .setOptions (Options .sendno ())
548
+ .setTarget ("1507ffd3f79456757de" );
549
+ pushPayloadList .add (builder2 .build ());
550
+
551
+ BatchPushResult result = jPushClient .batchSendPushByAlias (pushPayloadList );
552
+ LOG .info ("batchSendPushByAlias param: {}, result: {}" , pushPayloadList , new Gson ().toJson (result .getBatchPushResult ()));
553
+ }
554
+
555
+ {
556
+ List <PushPayload > pushPayloadList = new ArrayList <>();
557
+ PushPayload .Builder builder1 = PushPayload .newBuilder ();
558
+ builder1 .setMessage (Message .content ("content1 by regId" ))
559
+ .setNotification (Notification .alert (ALERT ))
560
+ .setPlatform (Platform .android ())
561
+ .setAudience (Audience .all ())
562
+ .setOptions (Options .sendno ())
563
+ .setTarget ("1507ffd3f79456757de" );
564
+ pushPayloadList .add (builder1 .build ());
565
+
566
+ PushPayload .Builder builder2 = PushPayload .newBuilder ();
567
+ builder2 .setMessage (Message .content ("content2 by regId" ))
568
+ .setNotification (Notification .alert (ALERT ))
569
+ .setAudience (Audience .all ())
570
+ .setPlatform (Platform .ios ())
571
+ .setOptions (Options .sendno ())
572
+ .setTarget ("1507ffd3f79456757de" );
573
+ pushPayloadList .add (builder2 .build ());
574
+
575
+ BatchPushResult result = jPushClient .batchSendPushByRegId (pushPayloadList );
576
+ LOG .info ("batchSendPushByRegId param: {}, result: {}" , pushPayloadList , new Gson ().toJson (result .getBatchPushResult ()));
577
+ }
578
+
579
+ } catch (APIConnectionException e ) {
580
+ LOG .error ("Connection error. Should retry later. " , e );
581
+ } catch (APIRequestException e ) {
582
+ LOG .error ("Error response from JPush server. Should review and fix it. " , e );
583
+ LOG .info ("HTTP Status: " + e .getStatus ());
584
+ LOG .info ("Error Code: " + e .getErrorCode ());
585
+ LOG .info ("Error Message: " + e .getErrorMessage ());
586
+ }
587
+ }
588
+
524
589
}
525
590
0 commit comments