Skip to content

Commit a646a64

Browse files
authored
Merge pull request #140 from xdx54321/master
解决空指针的BUG
2 parents 542a3b6 + f8ce1cb commit a646a64

File tree

11 files changed

+335
-53
lines changed

11 files changed

+335
-53
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>cn.jpush.api</groupId>
4444
<artifactId>jiguang-common</artifactId>
45-
<version>1.1.6</version>
45+
<version>1.1.7</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>org.apache.httpcomponents</groupId>

src/main/java/cn/jpush/api/push/PushClient.java

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111
import cn.jiguang.common.utils.Preconditions;
1212
import cn.jiguang.common.utils.StringUtils;
1313
import cn.jiguang.common.utils.sm2.SM2Util;
14+
import cn.jpush.api.push.model.BatchPushPayload;
1415
import cn.jpush.api.push.model.EncryptKeys;
1516
import cn.jpush.api.push.model.EncryptPushPayload;
1617
import cn.jpush.api.push.model.PushPayload;
1718
import cn.jpush.api.push.model.audience.Audience;
18-
import com.google.gson.JsonElement;
19-
import com.google.gson.JsonObject;
20-
import com.google.gson.JsonParseException;
21-
import com.google.gson.JsonParser;
19+
import com.google.gson.*;
20+
21+
import java.util.Map;
2222

2323
/**
2424
* Entrance for sending Push.
@@ -36,6 +36,8 @@ public class PushClient {
3636
private String _baseUrl;
3737
private String _pushPath;
3838
private String _pushValidatePath;
39+
private String batchRegidPushPath;
40+
private String batchAliasPushPath;
3941

4042
private JsonParser _jsonParser = new JsonParser();
4143

@@ -103,6 +105,9 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
103105
this._pushPath = (String) conf.get(ClientConfig.PUSH_PATH);
104106
this._pushValidatePath = (String) conf.get(ClientConfig.PUSH_VALIDATE_PATH);
105107

108+
this.batchAliasPushPath = (String) conf.get(ClientConfig.BATCH_ALIAS_PUSH_PATH);
109+
this.batchRegidPushPath = (String) conf.get(ClientConfig.BATCH_REGID_PUSH_PATH);
110+
106111
this._apnsProduction = (Integer) conf.get(ClientConfig.APNS_PRODUCTION);
107112
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);
108113
this._encryptType = (String) conf.get(ClientConfig.ENCRYPT_TYPE);
@@ -218,6 +223,36 @@ public PushResult sendPushValidate(String payloadString) throws APIConnectionExc
218223
return BaseResult.fromResponse(response, PushResult.class);
219224
}
220225

226+
public PushResult batchSendPushByRegId(BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
227+
return batchSendPush(_baseUrl + batchRegidPushPath, pushList);
228+
}
229+
230+
public PushResult batchSendPushByAlias(BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
231+
return batchSendPush(_baseUrl + batchAliasPushPath, pushList);
232+
}
233+
234+
public PushResult batchSendPush(String url, BatchPushPayload pushList) throws APIConnectionException, APIRequestException {
235+
236+
Preconditions.checkArgument((null != pushList), "param should not be null");
237+
Preconditions.checkArgument((null != pushList.getPushlist()), "pushList should not be null");
238+
239+
for (PushPayload payload : pushList.getPushlist().values()) {
240+
if (_timeToLive >= 0) {
241+
payload.resetOptionsTimeToLive(_timeToLive);
242+
}
243+
if (_apnsProduction > 0) {
244+
payload.resetOptionsApnsProduction(true);
245+
} else if(_apnsProduction == 0) {
246+
payload.resetOptionsApnsProduction(false);
247+
}
248+
}
249+
250+
Gson gson = new Gson();
251+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _pushPath, getEncryptData(gson.toJson(pushList)));
252+
253+
return BaseResult.fromResponse(response, PushResult.class);
254+
}
255+
221256
/**
222257
* Get cid list, the data form of cid is appKey-uuid.
223258
* @param count the count of cid list, from 1 to 1000. default is 1.
@@ -269,7 +304,7 @@ private String getEncryptData(String payloadData) {
269304
* @return
270305
*/
271306
private String getEncryptData(PushPayload pushPayload) {
272-
if (_encryptType.isEmpty()) {
307+
if (StringUtils.isEmpty(_encryptType)) {
273308
return pushPayload.toString();
274309
}
275310
if (EncryptKeys.ENCRYPT_SMS2_TYPE.equals(_encryptType)) {
@@ -292,7 +327,7 @@ private String getEncryptData(PushPayload pushPayload) {
292327
* @return
293328
*/
294329
private String getEncryptData(String pushPayload, Audience audience) {
295-
if (_encryptType.isEmpty()) {
330+
if (StringUtils.isEmpty(_encryptType)) {
296331
return pushPayload;
297332
}
298333
if (EncryptKeys.ENCRYPT_SMS2_TYPE.equals(_encryptType)) {
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package cn.jpush.api.push.model;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* @author xdx
7+
* @Desc
8+
* @date 2019-07-29.
9+
*/
10+
public class BatchPushPayload {
11+
12+
private Map<String, PushPayload> pushlist;
13+
14+
public Map<String, PushPayload> getPushlist() {
15+
return pushlist;
16+
}
17+
18+
public BatchPushPayload setPushlist(Map<String, PushPayload> pushlist) {
19+
this.pushlist = pushlist;
20+
return this;
21+
}
22+
}

src/main/java/cn/jpush/api/push/model/Options.java

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,68 +1,102 @@
11
package cn.jpush.api.push.model;
22

33

4-
import com.google.gson.JsonElement;
5-
import com.google.gson.JsonObject;
6-
import com.google.gson.JsonPrimitive;
4+
import com.google.gson.*;
75

86
import cn.jiguang.common.ServiceHelper;
97
import cn.jiguang.common.utils.Preconditions;
108

9+
import java.util.Map;
10+
1111
public class Options implements PushModel {
12+
1213
private static final String SENDNO = "sendno";
1314
private static final String OVERRIDE_MSG_ID = "override_msg_id";
1415
private static final String TIME_TO_LIVE = "time_to_live";
1516
private static final String APNS_PRODUCTION = "apns_production";
1617
private static final String BIG_PUSH_DURATION = "big_push_duration";
1718
private static final String APNS_COLLAPSE_ID = "apns_collapse_id";
18-
19+
private static final String THIRD_PARTH_CHANNEl = "third_party_channel";
20+
1921
private static final long NONE_TIME_TO_LIVE = -1;
20-
22+
2123
private final int sendno;
2224
private final long overrideMsgId;
2325
private long timeToLive;
2426
private boolean apnsProduction;
25-
private int bigPushDuration; // minutes
27+
// minutes
28+
private int bigPushDuration;
2629
private String apnsCollapseId;
27-
28-
private Options(int sendno, long overrideMsgId, long timeToLive, boolean apnsProduction,
29-
int bigPushDuration, String apnsCollapseId) {
30+
31+
/**
32+
* example
33+
* "third_party_channel": {
34+
* "xiaomi": {
35+
* "distribution": "ospush"
36+
* },
37+
* "huawei": {
38+
* "distribution": "jpush"
39+
* },
40+
* "meizu": {
41+
* "distribution": "jpush"
42+
* },
43+
* "fcm": {
44+
* "distribution": "ospush"
45+
* },
46+
* "oppo": {
47+
* "distribution": "ospush"
48+
* },
49+
* "vivo": {
50+
* "distribution": "ospush"
51+
* }
52+
* }
53+
*/
54+
private Map<String, Map<String, String>> thirdPartyChannel;
55+
56+
private Options(int sendno,
57+
long overrideMsgId,
58+
long timeToLive,
59+
boolean apnsProduction,
60+
int bigPushDuration,
61+
String apnsCollapseId,
62+
Map<String, Map<String, String>> thirdPartyChannel) {
3063
this.sendno = sendno;
3164
this.overrideMsgId = overrideMsgId;
3265
this.timeToLive = timeToLive;
3366
this.apnsProduction = apnsProduction;
3467
this.bigPushDuration = bigPushDuration;
3568
this.apnsCollapseId = apnsCollapseId;
69+
this.thirdPartyChannel = thirdPartyChannel;
3670
}
37-
71+
3872
public static Builder newBuilder() {
3973
return new Builder();
4074
}
41-
75+
4276
public static Options sendno() {
4377
return newBuilder().setSendno(ServiceHelper.generateSendno()).build();
4478
}
45-
79+
4680
public static Options sendno(int sendno) {
4781
return newBuilder().setSendno(sendno).build();
4882
}
49-
83+
5084
public void setApnsProduction(boolean apnsProduction) {
5185
this.apnsProduction = apnsProduction;
5286
}
53-
87+
5488
public void setTimeToLive(long timeToLive) {
5589
this.timeToLive = timeToLive;
5690
}
57-
91+
5892
public void setBigPushDuration(int bigPushDuration) {
59-
this.bigPushDuration = bigPushDuration;
93+
this.bigPushDuration = bigPushDuration;
6094
}
61-
95+
6296
public int getSendno() {
6397
return this.sendno;
6498
}
65-
99+
66100
@Override
67101
public JsonElement toJSON() {
68102
JsonObject json = new JsonObject();
@@ -75,43 +109,57 @@ public JsonElement toJSON() {
75109
if (timeToLive >= 0) {
76110
json.add(TIME_TO_LIVE, new JsonPrimitive(timeToLive));
77111
}
78-
112+
79113
json.add(APNS_PRODUCTION, new JsonPrimitive(apnsProduction));
80-
114+
81115
if (bigPushDuration > 0) {
82-
json.add(BIG_PUSH_DURATION, new JsonPrimitive(bigPushDuration));
116+
json.add(BIG_PUSH_DURATION, new JsonPrimitive(bigPushDuration));
83117
}
84118

85119
if (apnsCollapseId != null) {
86120
json.add(APNS_COLLAPSE_ID, new JsonPrimitive(apnsCollapseId));
87121
}
88-
122+
123+
if (null != thirdPartyChannel && thirdPartyChannel.size() > 0) {
124+
JsonObject partyChannel = new JsonObject();
125+
for (Map.Entry<String, Map<String, String>> entry : thirdPartyChannel.entrySet()) {
126+
JsonObject channel = new JsonObject();
127+
for (Map.Entry<String, String> stringEntry : entry.getValue().entrySet()) {
128+
channel.addProperty(stringEntry.getKey(), stringEntry.getValue());
129+
}
130+
partyChannel.add(entry.getKey(), channel);
131+
}
132+
json.add(THIRD_PARTH_CHANNEl, partyChannel);
133+
}
134+
89135
return json;
90136
}
91-
137+
92138
public static class Builder {
139+
93140
private int sendno = 0;
94141
private long overrideMsgId = 0;
95142
private long timeToLive = NONE_TIME_TO_LIVE;
96143
private boolean apnsProduction = false;
97144
private int bigPushDuration = 0;
98145
private String apnsCollapseId;
99-
146+
private Map<String, Map<String, String>> thirdPartyChannel;
147+
100148
public Builder setSendno(int sendno) {
101149
this.sendno = sendno;
102150
return this;
103151
}
104-
152+
105153
public Builder setOverrideMsgId(long overrideMsgId) {
106154
this.overrideMsgId = overrideMsgId;
107155
return this;
108156
}
109-
157+
110158
public Builder setTimeToLive(long timeToLive) {
111159
this.timeToLive = timeToLive;
112160
return this;
113161
}
114-
162+
115163
public Builder setApnsProduction(boolean apnsProduction) {
116164
this.apnsProduction = apnsProduction;
117165
return this;
@@ -121,22 +169,32 @@ public Builder setApnsCollapseId(String id) {
121169
this.apnsCollapseId = id;
122170
return this;
123171
}
124-
172+
125173
public Builder setBigPushDuration(int bigPushDuration) {
126-
this.bigPushDuration = bigPushDuration;
127-
return this;
174+
this.bigPushDuration = bigPushDuration;
175+
return this;
176+
}
177+
178+
public Map<String, Map<String, String>> getThirdPartyChannel() {
179+
return thirdPartyChannel;
180+
}
181+
182+
public Builder setThirdPartyChannel(Map<String, Map<String, String>> thirdPartyChannel) {
183+
this.thirdPartyChannel = thirdPartyChannel;
184+
return this;
128185
}
129186

130187
public Options build() {
131188
Preconditions.checkArgument(sendno >= 0, "sendno should be greater than 0.");
132189
Preconditions.checkArgument(overrideMsgId >= 0, "override_msg_id should be greater than 0.");
133190
Preconditions.checkArgument(timeToLive >= NONE_TIME_TO_LIVE, "time_to_live should be greater than 0.");
134191
Preconditions.checkArgument(bigPushDuration >= 0, "bigPushDuration should be greater than 0.");
192+
135193
if (sendno <= 0) {
136194
sendno = ServiceHelper.generateSendno();
137195
}
138-
139-
return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId);
196+
197+
return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId, thirdPartyChannel);
140198
}
141199
}
142200

0 commit comments

Comments
 (0)