11package cn .jpush .api .examples ;
22
3+ import java .net .URI ;
4+ import java .net .URISyntaxException ;
35import java .util .HashMap ;
46import java .util .Map ;
57
8+ import cn .jiguang .common .ServiceHelper ;
9+ import cn .jiguang .common .connection .NettyHttpClient ;
10+ import cn .jiguang .common .resp .ResponseWrapper ;
611import cn .jpush .api .push .model .notification .*;
7- import com .google .gson .Gson ;
8- import com . google . gson . GsonBuilder ;
12+ import com .google .gson .* ;
13+ import io . netty . handler . codec . http . HttpMethod ;
914import org .slf4j .Logger ;
1015import org .slf4j .LoggerFactory ;
1116
2126import cn .jpush .api .push .model .SMS ;
2227import cn .jpush .api .push .model .audience .Audience ;
2328import cn .jpush .api .push .model .audience .AudienceTarget ;
24- import com .google .gson .JsonObject ;
2529
2630public class PushExample {
2731 protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
2832
2933 // demo App defined in resources/jpush-api.conf
3034 private static final String appKey ="dd1066407b044738b6479275" ;
3135 private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7" ;
36+
37+ protected static final String APP_KEY ="d4ee2375846bc30fa51334f5" ;
38+ protected static final String MASTER_SECRET = "2bf52ee46fdeaadb8718fc15" ;
3239
3340 public static final String TITLE = "Test from API example" ;
3441 public static final String ALERT = "Test from API Example - alert" ;
@@ -39,32 +46,109 @@ public class PushExample {
3946 public static void main (String [] args ) {
4047// testSendPushWithCustomConfig();
4148// testSendIosAlert();
42- // testSendPush();
43- testSendPush_fromJSON ();
49+ testSendPush ();
50+ // testSendPush_fromJSON();
51+ // testSendPushes();
52+ // testSendPushesWithMultiCallback();
4453 }
54+
55+ // 使用 NettyHttpClient 异步接口发送请求
56+ public static void testSendPushes () {
57+ ClientConfig clientConfig = ClientConfig .getInstance ();
58+ NettyHttpClient client = new NettyHttpClient (ServiceHelper .getBasicAuthorization (APP_KEY , MASTER_SECRET ),
59+ null , clientConfig );
60+ for (int i = 0 ; i < 4 ; i ++) {
61+ NettyHttpClient .BaseCallback callback = new NettyHttpClient .BaseCallback () {
62+ @ Override
63+ public void onSucceed (ResponseWrapper responseWrapper ) {
64+ System .out .println ("callback i" );
65+ }
66+ };
67+ MyThread thread = new MyThread (client , callback );
68+ thread .start ();
69+ }
70+ }
71+
72+ public static void testSendPushesWithMultiCallback () {
73+ NettyHttpClient client = new NettyHttpClient (ServiceHelper .getBasicAuthorization (APP_KEY , MASTER_SECRET ),
74+ null , ClientConfig .getInstance ());
75+ String host = (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_HOST_NAME );
76+ URI uri = null ;
77+ try {
78+ uri = new URI (host + (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_PATH ));
79+ PushPayload payload = PushPayload .alertAll ("test" );
80+ System .out .println (payload .toString ());
81+ NettyHttpClient .BaseCallback callback1 = new NettyHttpClient .BaseCallback () {
82+ @ Override
83+ public void onSucceed (ResponseWrapper responseWrapper ) {
84+ System .out .println ("callback1 Got result: " + responseWrapper .responseContent );
85+ }
86+ };
87+ NettyHttpClient .BaseCallback callback2 = new NettyHttpClient .BaseCallback () {
88+ @ Override
89+ public void onSucceed (ResponseWrapper responseWrapper ) {
90+ System .out .println ("callback2 Got result: " + responseWrapper .responseContent );
91+ }
92+ };
93+ MyThread thread1 = new MyThread (client , callback1 );
94+ MyThread thread2 = new MyThread (client , callback2 );
95+ thread1 .start ();
96+ thread2 .start ();
97+ } catch (URISyntaxException e ) {
98+ e .printStackTrace ();
99+ }
100+ }
101+
102+ private static class MyThread extends Thread {
103+
104+ private NettyHttpClient client ;
105+ private NettyHttpClient .BaseCallback callback ;
106+
107+ public MyThread (NettyHttpClient client , NettyHttpClient .BaseCallback callback ) {
108+ this .client = client ;
109+ this .callback = callback ;
110+ }
111+
112+ @ Override
113+ public void run () {
114+ // super.run();
115+ System .out .println ("running send push" );
116+ try {
117+ String host = (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_HOST_NAME );
118+ URI uri = new URI (host + (String ) ClientConfig .getInstance ().get (ClientConfig .PUSH_PATH ));
119+ PushPayload payload = PushPayload .alertAll ("test" );
120+ System .out .println (payload .toString ());
121+ client .sendRequest (HttpMethod .POST , payload .toString (), uri , callback );
122+ } catch (URISyntaxException e ) {
123+ e .printStackTrace ();
124+ }
125+ }
126+ }
45127
46128
47129 public static void testSendPush () {
48130 // HttpProxy proxy = new HttpProxy("localhost", 3128);
49131 // Can use this https proxy: https://github.com/Exa-Networks/exaproxy
50132 ClientConfig clientConfig = ClientConfig .getInstance ();
51- JPushClient jpushClient = new JPushClient (masterSecret , appKey , null , clientConfig );
133+ JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
52134
53135 // For push, all you need do is to build PushPayload object.
54- PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage ();
136+ PushPayload payload = buildPushObject_android_newly_support ();
55137 try {
56138 PushResult result = jpushClient .sendPush (payload );
57139 LOG .info ("Got result - " + result );
58140
59141 } catch (APIConnectionException e ) {
60142 LOG .error ("Connection error. Should retry later. " , e );
143+ LOG .error ("Sendno: " + payload .getSendno ());
61144
62145 } catch (APIRequestException e ) {
63146 LOG .error ("Error response from JPush server. Should review and fix it. " , e );
64147 LOG .info ("HTTP Status: " + e .getStatus ());
65148 LOG .info ("Error Code: " + e .getErrorCode ());
66149 LOG .info ("Error Message: " + e .getErrorMessage ());
67150 LOG .info ("Msg ID: " + e .getMsgId ());
151+ LOG .error ("Sendno: " + payload .getSendno ());
68152 }
69153 }
70154
@@ -84,13 +168,15 @@ public static void testSendPush_fromJSON() {
84168
85169 } catch (APIConnectionException e ) {
86170 LOG .error ("Connection error. Should retry later. " , e );
171+ LOG .error ("Sendno: " + payload .getSendno ());
87172
88173 } catch (APIRequestException e ) {
89174 LOG .error ("Error response from JPush server. Should review and fix it. " , e );
90175 LOG .info ("HTTP Status: " + e .getStatus ());
91176 LOG .info ("Error Code: " + e .getErrorCode ());
92177 LOG .info ("Error Message: " + e .getErrorMessage ());
93178 LOG .info ("Msg ID: " + e .getMsgId ());
179+ LOG .error ("Sendno: " + payload .getSendno ());
94180 }
95181 }
96182
@@ -178,6 +264,33 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage()
178264 .build ())
179265 .build ();
180266 }
267+
268+ public static PushPayload buildPushObject_android_newly_support () {
269+ JsonObject inbox = new JsonObject ();
270+ inbox .add ("line1" , new JsonPrimitive ("line1 string" ));
271+ inbox .add ("line2" , new JsonPrimitive ("line2 string" ));
272+ inbox .add ("contentTitle" , new JsonPrimitive ("title string" ));
273+ inbox .add ("summaryText" , new JsonPrimitive ("+3 more" ));
274+ Notification notification = Notification .newBuilder ()
275+ .addPlatformNotification (AndroidNotification .newBuilder ()
276+ .setAlert (ALERT )
277+ .setBigPicPath ("path to big picture" )
278+ .setBigText ("long text" )
279+ .setBuilderId (1 )
280+ .setCategory ("CATEGORY_SOCIAL" )
281+ .setInbox (inbox )
282+ .setStyle (1 )
283+ .setTitle ("Alert test" )
284+ .setPriority (1 )
285+ .build ())
286+ .build ();
287+ return PushPayload .newBuilder ()
288+ .setPlatform (Platform .all ())
289+ .setAudience (Audience .registrationId ("18071adc030dcba91c0" ))
290+ .setNotification (notification )
291+ .setOptions (Options .sendno ())
292+ .build ();
293+ }
181294
182295 public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras () {
183296 return PushPayload .newBuilder ()
0 commit comments