1
1
package cn .jpush .api .examples ;
2
2
3
+ import java .net .URI ;
4
+ import java .net .URISyntaxException ;
3
5
import java .util .HashMap ;
4
6
import java .util .Map ;
5
7
8
+ import cn .jiguang .common .ServiceHelper ;
9
+ import cn .jiguang .common .connection .NettyHttpClient ;
10
+ import cn .jiguang .common .resp .ResponseWrapper ;
6
11
import 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 ;
9
14
import org .slf4j .Logger ;
10
15
import org .slf4j .LoggerFactory ;
11
16
21
26
import cn .jpush .api .push .model .SMS ;
22
27
import cn .jpush .api .push .model .audience .Audience ;
23
28
import cn .jpush .api .push .model .audience .AudienceTarget ;
24
- import com .google .gson .JsonObject ;
25
29
26
30
public class PushExample {
27
31
protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
28
32
29
33
// demo App defined in resources/jpush-api.conf
30
34
private static final String appKey ="dd1066407b044738b6479275" ;
31
35
private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7" ;
36
+
37
+ protected static final String APP_KEY ="d4ee2375846bc30fa51334f5" ;
38
+ protected static final String MASTER_SECRET = "2bf52ee46fdeaadb8718fc15" ;
32
39
33
40
public static final String TITLE = "Test from API example" ;
34
41
public static final String ALERT = "Test from API Example - alert" ;
@@ -39,32 +46,109 @@ public class PushExample {
39
46
public static void main (String [] args ) {
40
47
// testSendPushWithCustomConfig();
41
48
// testSendIosAlert();
42
- // testSendPush();
43
- testSendPush_fromJSON ();
49
+ testSendPush ();
50
+ // testSendPush_fromJSON();
51
+ // testSendPushes();
52
+ // testSendPushesWithMultiCallback();
44
53
}
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
+ }
45
127
46
128
47
129
public static void testSendPush () {
48
130
// HttpProxy proxy = new HttpProxy("localhost", 3128);
49
131
// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
50
132
ClientConfig clientConfig = ClientConfig .getInstance ();
51
- JPushClient jpushClient = new JPushClient (masterSecret , appKey , null , clientConfig );
133
+ JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
52
134
53
135
// 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 ();
55
137
try {
56
138
PushResult result = jpushClient .sendPush (payload );
57
139
LOG .info ("Got result - " + result );
58
140
59
141
} catch (APIConnectionException e ) {
60
142
LOG .error ("Connection error. Should retry later. " , e );
143
+ LOG .error ("Sendno: " + payload .getSendno ());
61
144
62
145
} catch (APIRequestException e ) {
63
146
LOG .error ("Error response from JPush server. Should review and fix it. " , e );
64
147
LOG .info ("HTTP Status: " + e .getStatus ());
65
148
LOG .info ("Error Code: " + e .getErrorCode ());
66
149
LOG .info ("Error Message: " + e .getErrorMessage ());
67
150
LOG .info ("Msg ID: " + e .getMsgId ());
151
+ LOG .error ("Sendno: " + payload .getSendno ());
68
152
}
69
153
}
70
154
@@ -84,13 +168,15 @@ public static void testSendPush_fromJSON() {
84
168
85
169
} catch (APIConnectionException e ) {
86
170
LOG .error ("Connection error. Should retry later. " , e );
171
+ LOG .error ("Sendno: " + payload .getSendno ());
87
172
88
173
} catch (APIRequestException e ) {
89
174
LOG .error ("Error response from JPush server. Should review and fix it. " , e );
90
175
LOG .info ("HTTP Status: " + e .getStatus ());
91
176
LOG .info ("Error Code: " + e .getErrorCode ());
92
177
LOG .info ("Error Message: " + e .getErrorMessage ());
93
178
LOG .info ("Msg ID: " + e .getMsgId ());
179
+ LOG .error ("Sendno: " + payload .getSendno ());
94
180
}
95
181
}
96
182
@@ -178,6 +264,33 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage()
178
264
.build ())
179
265
.build ();
180
266
}
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
+ }
181
294
182
295
public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras () {
183
296
return PushPayload .newBuilder ()
0 commit comments