1414import cn .jpush .api .push .model .*;
1515import cn .jpush .api .push .model .audience .Audience ;
1616import com .google .gson .*;
17+ import com .google .gson .reflect .TypeToken ;
1718
19+ import java .lang .reflect .Type ;
20+ import java .util .HashMap ;
1821import java .util .List ;
1922import java .util .Map ;
2023
2124/**
2225 * Entrance for sending Push.
23- *
24- * For the following parameters, you can set them by instance creation.
26+ * <p>
27+ * For the following parameters, you can set them by instance creation.
2528 * This action will override setting in PushPayload Optional.
2629 * * apnsProduction If not present, the default is true.
2730 * * timeToLive If not present, the default is 86400(s) (one day).
28- *
31+ * <p>
2932 * Can be used directly.
3033 */
3134public class PushClient {
@@ -41,7 +44,7 @@ public class PushClient {
4144
4245 // If not present, true by default.
4346 private int _apnsProduction ;
44-
47+
4548 // If not present, the default value is 86400(s) (one day)
4649 private long _timeToLive ;
4750
@@ -50,36 +53,37 @@ public class PushClient {
5053
5154 /**
5255 * Create a Push Client.
53- *
56+ *
5457 * @param masterSecret API access secret of the appKey.
55- * @param appKey The KEY of one application on JPush.
58+ * @param appKey The KEY of one application on JPush.
5659 */
57- public PushClient (String masterSecret , String appKey ) {
58- this (masterSecret , appKey , null , ClientConfig .getInstance ());
59- }
60+ public PushClient (String masterSecret , String appKey ) {
61+ this (masterSecret , appKey , null , ClientConfig .getInstance ());
62+ }
6063
6164 /**
6265 * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig} instead of this constructor.
63- * @param masterSecret API access secret of the appKey.
64- * @param appKey The KEY of one application on JPush.
66+ *
67+ * @param masterSecret API access secret of the appKey.
68+ * @param appKey The KEY of one application on JPush.
6569 * @param maxRetryTimes The max retry times.
6670 */
6771 @ Deprecated
68- public PushClient (String masterSecret , String appKey , int maxRetryTimes ) {
69- this (masterSecret , appKey , maxRetryTimes , null );
70- }
71-
72- /**
73- * Create a Push Client with max retry times.
74- * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig} instead of this constructor.
72+ public PushClient (String masterSecret , String appKey , int maxRetryTimes ) {
73+ this (masterSecret , appKey , maxRetryTimes , null );
74+ }
75+
76+ /**
77+ * Create a Push Client with max retry times.
78+ * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig} instead of this constructor.
7579 *
76- * @param masterSecret API access secret of the appKey.
77- * @param appKey The KEY of one application on JPush.
78- * @param maxRetryTimes max retry times
79- * @param proxy The max retry times.
80- */
80+ * @param masterSecret API access secret of the appKey.
81+ * @param appKey The KEY of one application on JPush.
82+ * @param maxRetryTimes max retry times
83+ * @param proxy The max retry times.
84+ */
8185 @ Deprecated
82- public PushClient (String masterSecret , String appKey , int maxRetryTimes , HttpProxy proxy ) {
86+ public PushClient (String masterSecret , String appKey , int maxRetryTimes , HttpProxy proxy ) {
8387 ServiceHelper .checkBasic (appKey , masterSecret );
8488
8589 ClientConfig conf = ClientConfig .getInstance ();
@@ -94,7 +98,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
9498
9599 String authCode = ServiceHelper .getBasicAuthorization (appKey , masterSecret );
96100 this ._httpClient = new NativeHttpClient (authCode , proxy , conf );
97- }
101+ }
98102
99103 public PushClient (String masterSecret , String appKey , HttpProxy proxy , ClientConfig conf ) {
100104 ServiceHelper .checkBasic (appKey , masterSecret );
@@ -115,21 +119,21 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
115119
116120 }
117121
118- /**
122+ /**
119123 * Create a Push Client with global settings.
120- *
124+ * <p>
121125 * If you want different settings from default globally, this constructor is what you needed.
122126 * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig} instead of this constructor.
123127 *
124- * @param masterSecret API access secret of the appKey.
125- * @param appKey The KEY of one application on JPush.
128+ * @param masterSecret API access secret of the appKey.
129+ * @param appKey The KEY of one application on JPush.
126130 * @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
127- * @param timeToLive Global time_to_live setting. It will override PushPayload Options.
131+ * @param timeToLive Global time_to_live setting. It will override PushPayload Options.
128132 */
129133 @ Deprecated
130134 public PushClient (String masterSecret , String appKey , boolean apnsProduction , long timeToLive ) {
131135 this (masterSecret , appKey );
132- if (apnsProduction ) {
136+ if (apnsProduction ) {
133137 _apnsProduction = 1 ;
134138 } else {
135139 _apnsProduction = 0 ;
@@ -141,28 +145,28 @@ public PushClient(String masterSecret, String appKey, boolean apnsProduction, lo
141145 * This will be removed in the future. Please use ClientConfig{jiguang-common cn.jiguang.common.ClientConfig#setGlobalPushSetting} instead of this method.
142146 *
143147 * @param apnsProduction Global APNs environment setting. It will override PushPayload Options.
144- * @param timeToLive Global time_to_live setting. It will override PushPayload Options.
148+ * @param timeToLive Global time_to_live setting. It will override PushPayload Options.
145149 */
146150 @ Deprecated
147151 public void setDefaults (boolean apnsProduction , long timeToLive ) {
148- if (apnsProduction ) {
152+ if (apnsProduction ) {
149153 _apnsProduction = 1 ;
150154 } else {
151155 _apnsProduction = 0 ;
152156 }
153157 this ._timeToLive = timeToLive ;
154158 }
155-
159+
156160 public void setBaseUrl (String baseUrl ) {
157161 this ._baseUrl = baseUrl ;
158162 }
159-
163+
160164 public PushResult sendPush (PushPayload pushPayload ) throws APIConnectionException , APIRequestException {
161- Preconditions .checkArgument (! (null == pushPayload ), "pushPayload should not be null" );
162-
165+ Preconditions .checkArgument (!(null == pushPayload ), "pushPayload should not be null" );
166+
163167 if (_apnsProduction > 0 ) {
164168 pushPayload .resetOptionsApnsProduction (true );
165- } else if (_apnsProduction == 0 ) {
169+ } else if (_apnsProduction == 0 ) {
166170 pushPayload .resetOptionsApnsProduction (false );
167171 }
168172
@@ -171,65 +175,65 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
171175 }
172176
173177 ResponseWrapper response = _httpClient .sendPost (_baseUrl + _pushPath , getEncryptData (pushPayload ));
174-
178+
175179 return BaseResult .fromResponse (response , PushResult .class );
176180 }
177-
181+
178182 public PushResult sendPushValidate (PushPayload pushPayload ) throws APIConnectionException , APIRequestException {
179- Preconditions .checkArgument (! (null == pushPayload ), "pushPayload should not be null" );
180-
183+ Preconditions .checkArgument (!(null == pushPayload ), "pushPayload should not be null" );
184+
181185 if (_apnsProduction > 0 ) {
182186 pushPayload .resetOptionsApnsProduction (true );
183- } else if (_apnsProduction == 0 ) {
187+ } else if (_apnsProduction == 0 ) {
184188 pushPayload .resetOptionsApnsProduction (false );
185189 }
186190
187191 if (_timeToLive >= 0 ) {
188192 pushPayload .resetOptionsTimeToLive (_timeToLive );
189193 }
190-
194+
191195 ResponseWrapper response = _httpClient .sendPost (_baseUrl + _pushValidatePath , getEncryptData (pushPayload ));
192-
196+
193197 return BaseResult .fromResponse (response , PushResult .class );
194198 }
195-
199+
196200 public PushResult sendPush (String payloadString ) throws APIConnectionException , APIRequestException {
197201 Preconditions .checkArgument (StringUtils .isNotEmpty (payloadString ), "pushPayload should not be empty" );
198-
202+
199203 try {
200204 _jsonParser .parse (payloadString );
201205 } catch (JsonParseException e ) {
202206 Preconditions .checkArgument (false , "payloadString should be a valid JSON string." );
203207 }
204-
208+
205209 ResponseWrapper response = _httpClient .sendPost (_baseUrl + _pushPath , getEncryptData (payloadString ));
206-
210+
207211 return BaseResult .fromResponse (response , PushResult .class );
208212 }
209-
213+
210214 public PushResult sendPushValidate (String payloadString ) throws APIConnectionException , APIRequestException {
211215 Preconditions .checkArgument (StringUtils .isNotEmpty (payloadString ), "pushPayload should not be empty" );
212-
216+
213217 try {
214218 _jsonParser .parse (payloadString );
215219 } catch (JsonParseException e ) {
216220 Preconditions .checkArgument (false , "payloadString should be a valid JSON string." );
217221 }
218-
222+
219223 ResponseWrapper response = _httpClient .sendPost (_baseUrl + _pushValidatePath , getEncryptData (payloadString ));
220-
224+
221225 return BaseResult .fromResponse (response , PushResult .class );
222226 }
223227
224- public PushResult batchSendPushByRegId (List <PushPayload > pushPayloadList ) throws APIConnectionException , APIRequestException {
228+ public BatchPushResult batchSendPushByRegId (List <PushPayload > pushPayloadList ) throws APIConnectionException , APIRequestException {
225229 return batchSendPush (_baseUrl + batchRegidPushPath , pushPayloadList );
226230 }
227231
228- public PushResult batchSendPushByAlias (List <PushPayload > pushPayloadList ) throws APIConnectionException , APIRequestException {
232+ public BatchPushResult batchSendPushByAlias (List <PushPayload > pushPayloadList ) throws APIConnectionException , APIRequestException {
229233 return batchSendPush (_baseUrl + batchAliasPushPath , pushPayloadList );
230234 }
231235
232- public PushResult batchSendPush (String url , List <PushPayload > pushPayloadList ) throws APIConnectionException , APIRequestException {
236+ public BatchPushResult batchSendPush (String url , List <PushPayload > pushPayloadList ) throws APIConnectionException , APIRequestException {
233237
234238 Preconditions .checkArgument ((null != pushPayloadList ), "param should not be null" );
235239 Preconditions .checkArgument ((!pushPayloadList .isEmpty ()), "pushPayloadList should not be empty" );
@@ -251,18 +255,20 @@ public PushResult batchSendPush(String url, List<PushPayload> pushPayloadList) t
251255 }
252256 json .add ("pushlist" , pushPayLoadList );
253257
254- ResponseWrapper response = _httpClient .sendPost (url , getEncryptData (gson .toJson (pushPayLoadList )));
258+ ResponseWrapper response = _httpClient .sendPost (url , getEncryptData (gson .toJson (json )));
259+
260+ return BatchPushResult .fromResponse (response );
255261
256- return BaseResult .fromResponse (response , PushResult .class );
257262 }
258263
259264 /**
260265 * Get cid list, the data form of cid is appKey-uuid.
266+ *
261267 * @param count the count of cid list, from 1 to 1000. default is 1.
262- * @param type default is "push", option: "schedule"
268+ * @param type default is "push", option: "schedule"
263269 * @return CIDResult, an array of cid
264270 * @throws APIConnectionException connect exception
265- * @throws APIRequestException request exception
271+ * @throws APIRequestException request exception
266272 */
267273 public CIDResult getCidList (int count , String type ) throws APIConnectionException , APIRequestException {
268274 Preconditions .checkArgument (count >= 1 && count <= 1000 , "count should not less than 1 or larger than 1000" );
@@ -273,7 +279,7 @@ public CIDResult getCidList(int count, String type) throws APIConnectionExceptio
273279 } else {
274280 responseWrapper = _httpClient .sendGet (_baseUrl + _pushPath + "/cid?count=" + count );
275281 }
276- return BaseResult .fromResponse (responseWrapper , CIDResult .class );
282+ return BaseResult .fromResponse (responseWrapper , CIDResult .class );
277283 }
278284
279285 public void setHttpClient (IHttpClient client ) {
@@ -291,6 +297,7 @@ public void close() {
291297
292298 /**
293299 * 获取加密的payload数据
300+ *
294301 * @param payloadData
295302 * @return
296303 */
@@ -303,6 +310,7 @@ private String getEncryptData(String payloadData) {
303310
304311 /**
305312 * 获取加密的payload数据
313+ *
306314 * @param pushPayload
307315 * @return
308316 */
@@ -326,6 +334,7 @@ private String getEncryptData(PushPayload pushPayload) {
326334
327335 /**
328336 * 获取加密的payload数据
337+ *
329338 * @param pushPayload
330339 * @return
331340 */
0 commit comments