88import cn .jpush .api .common .ResponseWrapper ;
99import cn .jpush .api .common .ServiceHelper ;
1010import cn .jpush .api .push .model .PushPayload ;
11+ import cn .jpush .api .utils .StringUtils ;
12+
13+ import com .google .common .base .Preconditions ;
14+ import com .google .gson .JsonParseException ;
15+ import com .google .gson .JsonParser ;
1116
1217/**
1318 * Entrance for sending Push.
@@ -24,7 +29,8 @@ public class PushClient {
2429 public static final String PUSH_PATH = "/v3/push" ;
2530
2631 private final NativeHttpClient _httpClient ;
27-
32+ private JsonParser _jsonParser = new JsonParser ();
33+
2834 // If not present, true by default.
2935 private boolean _apnsProduction = true ;
3036
@@ -78,6 +84,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
7884 */
7985 public PushClient (String masterSecret , String appKey , boolean apnsProduction , long timeToLive ) {
8086 this (masterSecret , appKey );
87+
8188 this ._apnsProduction = apnsProduction ;
8289 this ._timeToLive = timeToLive ;
8390 this ._globalSettingEnabled = true ;
@@ -88,6 +95,8 @@ public void setBaseUrl(String baseUrl) {
8895 }
8996
9097 public PushResult sendPush (PushPayload pushPayload ) throws APIConnectionException , APIRequestException {
98+ Preconditions .checkArgument (! (null == pushPayload ), "pushPayload should not be null" );
99+
91100 if (_globalSettingEnabled ) {
92101 pushPayload .resetOptionsTimeToLive (_timeToLive );
93102 pushPayload .resetOptionsApnsProduction (_apnsProduction );
@@ -99,6 +108,15 @@ public PushResult sendPush(PushPayload pushPayload) throws APIConnectionExceptio
99108 }
100109
101110 public PushResult sendPush (String payloadString ) throws APIConnectionException , APIRequestException {
111+ Preconditions .checkArgument (StringUtils .isNotEmpty (payloadString ), "pushPayload should not be empty" );
112+
113+ try {
114+ _jsonParser .parse (payloadString );
115+ } catch (JsonParseException e ) {
116+ e .printStackTrace ();
117+ Preconditions .checkArgument (false , "payloadString should be a valid JSON string." );
118+ }
119+
102120 ResponseWrapper response = _httpClient .sendPost (_baseUrl , payloadString );
103121
104122 return PushResult .fromResponse (response );
0 commit comments