1- package main . java . cn .jpush .api .examples ;
1+ package cn .jpush .api .examples ;
22
33import cn .jiguang .common .ClientConfig ;
44import cn .jiguang .common .ServiceHelper ;
2525
2626import java .net .URI ;
2727import 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 .*;
3229
3330public class PushExample {
3431 protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
3532
3633 // demo App defined in resources/jpush-api.conf
37- protected static final String APP_KEY ="7b4b94cca0d185d611e53cca" ;
34+ protected static final String APP_KEY = "7b4b94cca0d185d611e53cca" ;
3835 protected static final String MASTER_SECRET = "860803cf613ed54aa3b941a8" ;
3936 protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c" ;
4037 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" ;
4340 public static final String ALERT = "Test from API Example - alert" ;
4441 public static final String MSG_CONTENT = "Test from API Example - msgContent" ;
4542 public static final String REGISTRATION_ID = "0900e8d85ef" ;
4643 public static final String TAG = "tag_api" ;
4744 public static long sendCount = 0 ;
4845 private static long sendTotalTime = 0 ;
4946
50- public static void main (String [] args ) {
47+ public static void main (String [] args ) {
48+
49+ testBatchSend ();
5150// testSendPushWithCustomConfig();
5251// testSendIosAlert();
5352// testSendPush();
@@ -56,9 +55,9 @@ public static void main(String[] args) {
5655 testSendPush_fromJSON ();
5756// testSendPushWithCallback();
5857// testSendPushWithCid();
59- }
58+ }
6059
61- // 使用 NettyHttpClient 异步接口发送请求
60+ // 使用 NettyHttpClient 异步接口发送请求
6261 public static void testSendPushWithCallback () {
6362 ClientConfig clientConfig = ClientConfig .getInstance ();
6463 String host = (String ) clientConfig .get (ClientConfig .PUSH_HOST_NAME );
@@ -78,14 +77,14 @@ public void onSucceed(ResponseWrapper responseWrapper) {
7877 }
7978 }
8079
81- public static void testSendPush () {
82- ClientConfig clientConfig = ClientConfig .getInstance ();
80+ public static void testSendPush () {
81+ ClientConfig clientConfig = ClientConfig .getInstance ();
8382 final JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
8483// String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
8584 // Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
8685 // Call setHttpClient to set httpClient,
8786 // If you don't invoke this method, default httpClient will use NativeHttpClient.
88-
87+
8988// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
9089// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
9190// jpushClient.getPushClient().setHttpClient(httpClient);
@@ -149,7 +148,7 @@ public static void testSendPushWithEncrypt() {
149148 }
150149 }
151150
152- //use String to build PushPayload instance
151+ //use String to build PushPayload instance
153152 public static void testSendPush_fromJSON () {
154153 ClientConfig clientConfig = ClientConfig .getInstance ();
155154 JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
@@ -165,7 +164,7 @@ public static void testSendPush_fromJSON() {
165164
166165 } catch (APIConnectionException e ) {
167166 LOG .error ("Connection error. Should retry later. " , e );
168- // LOG.error("Sendno: " + payload.getSendno());
167+ // LOG.error("Sendno: " + payload.getSendno());
169168
170169 } catch (APIRequestException e ) {
171170 LOG .error ("Error response from JPush server. Should review and fix it. " , e );
@@ -191,7 +190,7 @@ public static void testSendPushes() {
191190// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
192191 jpushClient .getPushClient ().setHttpClient (httpClient );
193192 final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage ();
194- for (int i = 0 ; i < 10 ;i ++) {
193+ for (int i = 0 ; i < 10 ; i ++) {
195194 Thread thread = new Thread () {
196195 public void run () {
197196 for (int j = 0 ; j < 200 ; j ++) {
@@ -249,42 +248,42 @@ public void testSendGroupPush() {
249248 LOG .error ("Sendno: " + payload .getSendno ());
250249 }
251250 }
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+
257256 public static PushPayload buildPushObject_all_alias_alert () {
258257 return PushPayload .newBuilder ()
259258 .setPlatform (Platform .all ())
260259 .setAudience (Audience .alias ("alias1" ))
261260 .setNotification (Notification .alert (ALERT ))
262261 .build ();
263262 }
264-
263+
265264 public static PushPayload buildPushObject_android_tag_alertWithTitle () {
266265 return PushPayload .newBuilder ()
267266 .setPlatform (Platform .android ())
268267 .setAudience (Audience .tag ("tag1" ))
269268 .setNotification (Notification .android (ALERT , TITLE , null ))
270269 .build ();
271270 }
272-
271+
273272 public static PushPayload buildPushObject_android_and_ios () {
274273 Map <String , String > extras = new HashMap <String , String >();
275274 extras .put ("test" , "https://community.jiguang.cn/push" );
276275 return PushPayload .newBuilder ()
277276 .setPlatform (Platform .android_ios ())
278277 .setAudience (Audience .all ())
279278 .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" )
283282 .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 ())
288287 .build ();
289288 }
290289
@@ -318,12 +317,12 @@ public static void buildPushObject_with_extra() {
318317
319318 System .out .println (payload .toJSON ());
320319 }
321-
320+
322321 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 ));
327326 return PushPayload .newBuilder ()
328327 .setPlatform (Platform .ios ())
329328 .setAudience (Audience .tag_and ("tag1" , "tag_all" ))
@@ -337,24 +336,24 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage()
337336 .addExtra ("from" , "JPush" )
338337 .build ())
339338 .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 ();
345344 }
346345
347346 public static PushPayload buildPushObject_android_newly_support () {
348-
347+
349348 JsonObject inbox = new JsonObject ();
350349 inbox .add ("line1" , new JsonPrimitive ("line1 string" ));
351350 inbox .add ("line2" , new JsonPrimitive ("line2 string" ));
352351 inbox .add ("contentTitle" , new JsonPrimitive ("title string" ));
353352 inbox .add ("summaryText" , new JsonPrimitive ("+3 more" ));
354-
353+
355354 JsonObject intent = new JsonObject ();
356355 intent .add ("url" , new JsonPrimitive ("intent:#Intent;component=com.jiguang.push/com.example.jpushdemo.SettingActivity;end" ));
357-
356+
358357 Notification notification = Notification .newBuilder ()
359358 .addPlatformNotification (AndroidNotification .newBuilder ()
360359 .setAlert (ALERT )
@@ -380,7 +379,7 @@ public static PushPayload buildPushObject_android_newly_support() {
380379 .build ())
381380 .build ();
382381 }
383-
382+
384383 public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras () {
385384 return PushPayload .newBuilder ()
386385 .setPlatform (Platform .android_ios ())
@@ -404,16 +403,16 @@ public static PushPayload buildPushObject_all_tag_not() {
404403 }
405404
406405 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+
417416 return PushPayload .newBuilder ()
418417 .setPlatform (Platform .android ())
419418// .setAudience(Audience.registrationId("1507bfd3f79558957de"))
@@ -474,10 +473,10 @@ public static void testSendWithSMS() {
474473 try {
475474// SMS sms = SMS.content(1, 10);
476475 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 ();
481480 PushResult result = jpushClient .sendAndroidMessageWithAlias ("Test SMS" , "test sms" , sms , "alias1" );
482481 LOG .info ("Got result - " + result );
483482 } catch (APIConnectionException e ) {
@@ -521,5 +520,71 @@ public static void testSendPushWithCid() {
521520 }
522521 }
523522
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+
524589}
525590
0 commit comments