1111import cn .jiguang .common .connection .NativeHttpClient ;
1212import cn .jiguang .common .connection .NettyHttpClient ;
1313import cn .jiguang .common .resp .ResponseWrapper ;
14+ import cn .jpush .api .push .CIDResult ;
15+ import cn .jpush .api .push .GroupPushClient ;
1416import cn .jpush .api .push .model .notification .*;
1517import com .google .gson .*;
1618import 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
0 commit comments