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 ;
92
3+ import cn .jiguang .common .ClientConfig ;
104import cn .jiguang .common .ServiceHelper ;
115import cn .jiguang .common .connection .NativeHttpClient ;
126import cn .jiguang .common .connection .NettyHttpClient ;
7+ import cn .jiguang .common .resp .APIConnectionException ;
8+ import cn .jiguang .common .resp .APIRequestException ;
139import cn .jiguang .common .resp .ResponseWrapper ;
10+ import cn .jpush .api .JPushClient ;
1411import cn .jpush .api .push .CIDResult ;
1512import 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 ;
1617import 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 ;
1822import io .netty .handler .codec .http .HttpMethod ;
1923import org .slf4j .Logger ;
2024import org .slf4j .LoggerFactory ;
2125
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 ;
3432
3533public class PushExample {
3634 protected static final Logger LOG = LoggerFactory .getLogger (PushExample .class );
@@ -52,10 +50,10 @@ public class PushExample {
5250 public static void main (String [] args ) {
5351// testSendPushWithCustomConfig();
5452// testSendIosAlert();
55- testSendPush ();
53+ // testSendPush();
5654// testGetCidList();
5755// testSendPushes();
58- // testSendPush_fromJSON();
56+ testSendPush_fromJSON ();
5957// testSendPushWithCallback();
6058// testSendPushWithCid();
6159 }
@@ -88,6 +86,42 @@ public static void testSendPush() {
8886 // Call setHttpClient to set httpClient,
8987 // If you don't invoke this method, default httpClient will use NativeHttpClient.
9088
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+
91125// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
92126// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
93127// jpushClient.getPushClient().setHttpClient(httpClient);
@@ -123,23 +157,23 @@ public static void testSendPush_fromJSON() {
123157 .registerTypeAdapter (PlatformNotification .class , new InterfaceAdapter <PlatformNotification >())
124158 .create ();
125159 // 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}}" ;
127161 PushPayload payload = gson .fromJson (payloadString , PushPayload .class );
128162 try {
129- PushResult result = jpushClient .sendPush (payload );
163+ PushResult result = jpushClient .sendPush (payloadString );
130164 LOG .info ("Got result - " + result );
131165
132166 } catch (APIConnectionException e ) {
133167 LOG .error ("Connection error. Should retry later. " , e );
134- LOG .error ("Sendno: " + payload .getSendno ());
168+ // LOG.error("Sendno: " + payload.getSendno());
135169
136170 } catch (APIRequestException e ) {
137171 LOG .error ("Error response from JPush server. Should review and fix it. " , e );
138172 LOG .info ("HTTP Status: " + e .getStatus ());
139173 LOG .info ("Error Code: " + e .getErrorCode ());
140174 LOG .info ("Error Message: " + e .getErrorMessage ());
141175 LOG .info ("Msg ID: " + e .getMsgId ());
142- LOG .error ("Sendno: " + payload .getSendno ());
176+ // LOG.error("Sendno: " + payload.getSendno());
143177 }
144178 }
145179
0 commit comments