1
- package cn .jpush .api .examples ;
2
-
3
- import java .net .URI ;
4
- import java .net .URISyntaxException ;
5
- import java .util .Collection ;
6
- import java .util .HashMap ;
7
- import java .util .LinkedList ;
8
- import java .util .Map ;
1
+ package main .java .cn .jpush .api .examples ;
9
2
3
+ import cn .jiguang .common .ClientConfig ;
10
4
import cn .jiguang .common .ServiceHelper ;
11
5
import cn .jiguang .common .connection .NativeHttpClient ;
12
6
import cn .jiguang .common .connection .NettyHttpClient ;
7
+ import cn .jiguang .common .resp .APIConnectionException ;
8
+ import cn .jiguang .common .resp .APIRequestException ;
13
9
import cn .jiguang .common .resp .ResponseWrapper ;
10
+ import cn .jpush .api .JPushClient ;
14
11
import cn .jpush .api .push .CIDResult ;
15
12
import cn .jpush .api .push .GroupPushClient ;
13
+ import cn .jpush .api .push .PushResult ;
14
+ import cn .jpush .api .push .model .*;
15
+ import cn .jpush .api .push .model .audience .Audience ;
16
+ import cn .jpush .api .push .model .audience .AudienceTarget ;
16
17
import cn .jpush .api .push .model .notification .*;
17
- import com .google .gson .*;
18
+ import com .google .gson .Gson ;
19
+ import com .google .gson .GsonBuilder ;
20
+ import com .google .gson .JsonObject ;
21
+ import com .google .gson .JsonPrimitive ;
18
22
import io .netty .handler .codec .http .HttpMethod ;
19
23
import org .slf4j .Logger ;
20
24
import org .slf4j .LoggerFactory ;
21
25
22
- import cn .jiguang .common .ClientConfig ;
23
- import cn .jiguang .common .resp .APIConnectionException ;
24
- import cn .jiguang .common .resp .APIRequestException ;
25
- import cn .jpush .api .JPushClient ;
26
- import cn .jpush .api .push .PushResult ;
27
- import cn .jpush .api .push .model .Message ;
28
- import cn .jpush .api .push .model .Options ;
29
- import cn .jpush .api .push .model .Platform ;
30
- import cn .jpush .api .push .model .PushPayload ;
31
- import cn .jpush .api .push .model .SMS ;
32
- import cn .jpush .api .push .model .audience .Audience ;
33
- import cn .jpush .api .push .model .audience .AudienceTarget ;
26
+ import java .net .URI ;
27
+ import java .net .URISyntaxException ;
28
+ import java .util .Collection ;
29
+ import java .util .HashMap ;
30
+ import java .util .LinkedList ;
31
+ import java .util .Map ;
34
32
35
33
public class PushExample {
36
34
protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
@@ -52,10 +50,10 @@ public class PushExample {
52
50
public static void main (String [] args ) {
53
51
// testSendPushWithCustomConfig();
54
52
// testSendIosAlert();
55
- testSendPush ();
53
+ // testSendPush();
56
54
// testGetCidList();
57
55
// testSendPushes();
58
- // testSendPush_fromJSON();
56
+ testSendPush_fromJSON ();
59
57
// testSendPushWithCallback();
60
58
// testSendPushWithCid();
61
59
}
@@ -88,6 +86,42 @@ public static void testSendPush() {
88
86
// Call setHttpClient to set httpClient,
89
87
// If you don't invoke this method, default httpClient will use NativeHttpClient.
90
88
89
+ // ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
90
+ // NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
91
+ // jpushClient.getPushClient().setHttpClient(httpClient);
92
+ final PushPayload payload = buildPushObject_android_and_ios ();
93
+ // // For push, all you need do is to build PushPayload object.
94
+ // PushPayload payload = buildPushObject_all_alias_alert();
95
+ try {
96
+ PushResult result = jpushClient .sendPush (payload );
97
+ LOG .info ("Got result - " + result );
98
+ System .out .println (result );
99
+ // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
100
+ // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
101
+ // jpushClient.close();
102
+ } catch (APIConnectionException e ) {
103
+ LOG .error ("Connection error. Should retry later. " , e );
104
+ LOG .error ("Sendno: " + payload .getSendno ());
105
+
106
+ } catch (APIRequestException e ) {
107
+ LOG .error ("Error response from JPush server. Should review and fix it. " , e );
108
+ LOG .info ("HTTP Status: " + e .getStatus ());
109
+ LOG .info ("Error Code: " + e .getErrorCode ());
110
+ LOG .info ("Error Message: " + e .getErrorMessage ());
111
+ LOG .info ("Msg ID: " + e .getMsgId ());
112
+ LOG .error ("Sendno: " + payload .getSendno ());
113
+ }
114
+ }
115
+
116
+ public static void testSendPushWithEncrypt () {
117
+ ClientConfig clientConfig = ClientConfig .getInstance ();
118
+ clientConfig .setEncryptType (EncryptKeys .ENCRYPT_SMS2_TYPE );
119
+ final JPushClient jpushClient = new JPushClient (MASTER_SECRET , APP_KEY , null , clientConfig );
120
+ // String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
121
+ // Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
122
+ // Call setHttpClient to set httpClient,
123
+ // If you don't invoke this method, default httpClient will use NativeHttpClient.
124
+
91
125
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
92
126
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
93
127
// jpushClient.getPushClient().setHttpClient(httpClient);
@@ -123,23 +157,23 @@ public static void testSendPush_fromJSON() {
123
157
.registerTypeAdapter (PlatformNotification .class , new InterfaceAdapter <PlatformNotification >())
124
158
.create ();
125
159
// Since the type of DeviceType is enum, thus the value should be uppercase, same with the AudienceType.
126
- 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}}" ;
160
+ String payloadString = "{\" platform\" :{\" all\" :false,\" deviceTypes\" :[\" IOS\" ]},\" audience\" :{\" all\" :true ,\" 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}}" ;
127
161
PushPayload payload = gson .fromJson (payloadString , PushPayload .class );
128
162
try {
129
- PushResult result = jpushClient .sendPush (payload );
163
+ PushResult result = jpushClient .sendPush (payloadString );
130
164
LOG .info ("Got result - " + result );
131
165
132
166
} catch (APIConnectionException e ) {
133
167
LOG .error ("Connection error. Should retry later. " , e );
134
- LOG .error ("Sendno: " + payload .getSendno ());
168
+ // LOG.error("Sendno: " + payload.getSendno());
135
169
136
170
} catch (APIRequestException e ) {
137
171
LOG .error ("Error response from JPush server. Should review and fix it. " , e );
138
172
LOG .info ("HTTP Status: " + e .getStatus ());
139
173
LOG .info ("Error Code: " + e .getErrorCode ());
140
174
LOG .info ("Error Message: " + e .getErrorMessage ());
141
175
LOG .info ("Msg ID: " + e .getMsgId ());
142
- LOG .error ("Sendno: " + payload .getSendno ());
176
+ // LOG.error("Sendno: " + payload.getSendno());
143
177
}
144
178
}
145
179
0 commit comments