11
11
import cn .jiguang .common .connection .NativeHttpClient ;
12
12
import cn .jiguang .common .connection .NettyHttpClient ;
13
13
import cn .jiguang .common .resp .ResponseWrapper ;
14
+ import cn .jpush .api .push .CIDResult ;
15
+ import cn .jpush .api .push .GroupPushClient ;
14
16
import cn .jpush .api .push .model .notification .*;
15
17
import com .google .gson .*;
16
18
import io .netty .handler .codec .http .HttpMethod ;
@@ -34,11 +36,10 @@ public class PushExample {
34
36
protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
35
37
36
38
// demo App defined in resources/jpush-api.conf
37
- private static final String appKey ="dd1066407b044738b6479275" ;
38
- private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7" ;
39
-
40
39
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" ;
42
43
43
44
public static final String TITLE = "Test from API example" ;
44
45
public static final String ALERT = "Test from API Example - alert" ;
@@ -85,9 +86,74 @@ public static void testSendPush() {
85
86
NativeHttpClient httpClient = new NativeHttpClient (authCode , null , clientConfig );
86
87
// Call setHttpClient to set httpClient,
87
88
// 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.
88
154
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
89
155
jpushClient .getPushClient ().setHttpClient (httpClient );
90
- final PushPayload payload = buildPushObject_android_newly_support ();
156
+ final PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage ();
91
157
for (int i =0 ;i <10 ;i ++) {
92
158
Thread thread = new Thread () {
93
159
public void run () {
@@ -116,41 +182,13 @@ public void run() {
116
182
};
117
183
thread .start ();
118
184
}
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
- // }
140
185
}
141
186
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 ();
152
190
try {
153
- PushResult result = jpushClient . sendPush (payload );
191
+ PushResult result = groupPushClient . sendGroupPush (payload );
154
192
LOG .info ("Got result - " + result );
155
193
156
194
} catch (APIConnectionException e ) {
@@ -165,10 +203,6 @@ public static void testSendPush_fromJSON() {
165
203
LOG .info ("Msg ID: " + e .getMsgId ());
166
204
LOG .error ("Sendno: " + payload .getSendno ());
167
205
}
168
- }
169
-
170
- public static void testSendPushes () {
171
-
172
206
}
173
207
174
208
public static PushPayload buildPushObject_all_all_alert () {
@@ -277,9 +311,12 @@ public static PushPayload buildPushObject_android_newly_support() {
277
311
.build ();
278
312
return PushPayload .newBuilder ()
279
313
.setPlatform (Platform .all ())
280
- .setAudience (Audience .registrationId ( "18071adc030dcba91c0" ))
314
+ .setAudience (Audience .all ( ))
281
315
.setNotification (notification )
282
- .setOptions (Options .sendno ())
316
+ .setOptions (Options .newBuilder ()
317
+ .setApnsProduction (true )
318
+ .setSendno (ServiceHelper .generateSendno ())
319
+ .build ())
283
320
.build ();
284
321
}
285
322
@@ -305,12 +342,21 @@ public static PushPayload buildPushObject_all_tag_not() {
305
342
.build ();
306
343
}
307
344
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
+
308
354
public static void testSendPushWithCustomConfig () {
309
355
ClientConfig config = ClientConfig .getInstance ();
310
356
// Setup the custom hostname
311
357
config .setPushHostName ("https://api.jpush.cn" );
312
358
313
- JPushClient jpushClient = new JPushClient (masterSecret , appKey , null , config );
359
+ JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , config );
314
360
315
361
// For push, all you need do is to build PushPayload object.
316
362
PushPayload payload = buildPushObject_all_all_alert ();
@@ -332,7 +378,7 @@ public static void testSendPushWithCustomConfig() {
332
378
}
333
379
334
380
public static void testSendIosAlert () {
335
- JPushClient jpushClient = new JPushClient (masterSecret , appKey );
381
+ JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY );
336
382
337
383
IosAlert alert = IosAlert .newBuilder ()
338
384
.setTitleAndBody ("test alert" , "subtitle" , "test ios alert json" )
@@ -352,7 +398,7 @@ public static void testSendIosAlert() {
352
398
}
353
399
354
400
public static void testSendWithSMS () {
355
- JPushClient jpushClient = new JPushClient (masterSecret , appKey );
401
+ JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY );
356
402
try {
357
403
SMS sms = SMS .content ("Test SMS" , 10 );
358
404
PushResult result = jpushClient .sendAndroidMessageWithAlias ("Test SMS" , "test sms" , sms , "alias1" );
@@ -367,5 +413,36 @@ public static void testSendWithSMS() {
367
413
}
368
414
}
369
415
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
+
370
447
}
371
448
0 commit comments