Skip to content

Commit 2532940

Browse files
authored
Merge pull request #167 from yutaipu/master
解决厂商推送只能传String问题,GroupPush解析返回参数报错问题
2 parents b0a064e + 2446a7b commit 2532940

File tree

9 files changed

+193
-37
lines changed

9 files changed

+193
-37
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
## 安装
1919

2020
### maven 方式
21-
将下边的依赖条件放到你项目的 maven pom.xml 文件里。v3.3.10 为例,最新版本请看[Release页面](https://github.com/jpush/jpush-api-java-client/releases)
21+
将下边的依赖条件放到你项目的 maven pom.xml 文件里。v3.4.6 为例,最新版本请看[Release页面](https://github.com/jpush/jpush-api-java-client/releases)
2222

2323
```Java
2424
<dependency>
2525
<groupId>cn.jpush.api</groupId>
2626
<artifactId>jpush-client</artifactId>
27-
<version>3.3.10</version>
27+
<version>3.4.6</version>
2828
</dependency>
2929
```
3030
### jar 包方式
@@ -45,7 +45,7 @@
4545
<dependency>
4646
<groupId>cn.jpush.api</groupId>
4747
<artifactId>jiguang-common</artifactId>
48-
<version>1.1.4</version>
48+
<version>1.1.8</version>
4949
</dependency>
5050
<dependency>
5151
<groupId>io.netty</groupId>

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import cn.jpush.api.JPushClient;
1111
import cn.jpush.api.push.CIDResult;
1212
import cn.jpush.api.push.GroupPushClient;
13+
import cn.jpush.api.push.GroupPushResult;
1314
import cn.jpush.api.push.PushResult;
1415
import cn.jpush.api.push.model.*;
1516
import cn.jpush.api.push.model.audience.Audience;
@@ -227,7 +228,8 @@ public void testSendGroupPush() {
227228
GroupPushClient groupPushClient = new GroupPushClient(GROUP_MASTER_SECRET, GROUP_PUSH_KEY);
228229
final PushPayload payload = buildPushObject_android_and_ios();
229230
try {
230-
Map<String, PushResult> result = groupPushClient.sendGroupPush(payload);
231+
GroupPushResult groupPushResult = groupPushClient.sendGroupPush(payload);
232+
Map<String, PushResult> result = groupPushResult.getAppResultMap();
231233
for (Map.Entry<String, PushResult> entry : result.entrySet()) {
232234
PushResult pushResult = entry.getValue();
233235
PushResult.Error error = pushResult.error;

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.4.6-SNAPSHOT</version>
6+
<version>3.4.7-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@
1515
import cn.jpush.api.push.model.EncryptPushPayload;
1616
import cn.jpush.api.push.model.PushPayload;
1717
import com.google.gson.Gson;
18+
import com.google.gson.JsonElement;
19+
import com.google.gson.JsonObject;
20+
import com.google.gson.JsonParser;
1821
import com.google.gson.reflect.TypeToken;
1922

2023
import java.util.Map;
@@ -29,6 +32,8 @@ public class GroupPushClient {
2932
private String _groupPushPath;
3033
private String _encryptType;
3134
private Gson _gson = new Gson();
35+
private JsonParser _jsonParser = new JsonParser();
36+
3237

3338
public GroupPushClient(String groupMasterSecret, String groupKey) {
3439
this(groupMasterSecret, groupKey, null, ClientConfig.getInstance());
@@ -43,11 +48,17 @@ public GroupPushClient(String groupMasterSecret, String groupKey, HttpProxy prox
4348
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
4449
}
4550

46-
public Map<String, PushResult> sendGroupPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
51+
public GroupPushResult sendGroupPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
4752
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
4853
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _groupPushPath, getEncryptData(pushPayload));
49-
return _gson.fromJson(response.responseContent,
50-
new TypeToken<Map<String, PushResult>>(){}.getType());
54+
// 2020-8-6 兼容分组推送新返回的group_msgid结构
55+
JsonObject responseJson = _jsonParser.parse(response.responseContent).getAsJsonObject();
56+
String groupMsgIdKey = "group_msgid";
57+
JsonElement _groupMsgId = responseJson.get(groupMsgIdKey);
58+
String groupMsgId = null != _groupMsgId ? _groupMsgId.getAsString() : "";
59+
responseJson.remove(groupMsgIdKey);
60+
Map<String, PushResult> result = _gson.fromJson(responseJson, new TypeToken<Map<String, PushResult>>(){}.getType());
61+
return new GroupPushResult(groupMsgId, result);
5162
}
5263

5364
/**
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package cn.jpush.api.push;
2+
3+
import java.util.Map;
4+
5+
/**
6+
* @author tp
7+
* @since 2020/8/6
8+
*/
9+
public class GroupPushResult {
10+
11+
private Map<String, PushResult> appResultMap;
12+
13+
private String groupMsgId;
14+
15+
public GroupPushResult() {
16+
}
17+
18+
public GroupPushResult(String groupMsgId, Map<String, PushResult> appResultMap) {
19+
this.groupMsgId = groupMsgId;
20+
this.appResultMap = appResultMap;
21+
}
22+
23+
public Map<String, PushResult> getAppResultMap() {
24+
return appResultMap;
25+
}
26+
27+
public void setAppResultMap(Map<String, PushResult> appResultMap) {
28+
this.appResultMap = appResultMap;
29+
}
30+
31+
public String getGroupMsgId() {
32+
return groupMsgId;
33+
}
34+
35+
public void setGroupMsgId(String groupMsgId) {
36+
this.groupMsgId = groupMsgId;
37+
}
38+
}

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

Lines changed: 69 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@
66
import cn.jiguang.common.ServiceHelper;
77
import cn.jiguang.common.utils.Preconditions;
88

9+
import java.util.HashMap;
910
import java.util.LinkedHashMap;
1011
import java.util.Map;
12+
import java.util.Set;
1113

1214
public class Options implements PushModel {
1315

@@ -32,37 +34,42 @@ public class Options implements PushModel {
3234

3335

3436
/**
35-
* example
36-
* "third_party_channel": {
37-
* "xiaomi": {
38-
* "distribution": "ospush"
39-
* },
40-
* "huawei": {
41-
* "distribution": "jpush"
42-
* },
43-
* "meizu": {
44-
* "distribution": "jpush"
45-
* },
46-
* "fcm": {
47-
* "distribution": "ospush"
48-
* },
49-
* "oppo": {
50-
* "distribution": "ospush"
51-
* },
52-
* "vivo": {
53-
* "distribution": "ospush"
37+
* {
38+
* "third_party_channel":{
39+
* "xiaomi":{
40+
* "distribution":"ospush",
41+
* "channel_id":"*******"
42+
* },
43+
* "huawei":{
44+
* "distribution":"jpush"
45+
* },
46+
* "meizu":{
47+
* "distribution":"jpush"
48+
* },
49+
* "fcm":{
50+
* "distribution":"ospush"
51+
* },
52+
* "oppo":{
53+
* "distribution":"ospush",
54+
* "channel_id":"*******"
55+
* },
56+
* "vivo":{
57+
* "distribution":"ospush",
58+
* "classification":0 // 2020/06 新增,和vivo官方字段含义一致 0 代表运营消息,1 代表系统消息,不填vivo官方默认为0
59+
* // 使用此字段时,需使用setThirdPartyChannelV2方法,因为此值只能为整数形式
60+
* }
5461
* }
5562
* }
5663
*/
57-
private Map<String, Map<String, String>> thirdPartyChannel;
64+
private Map<String, JsonObject> thirdPartyChannel;
5865

5966
private Options(int sendno,
6067
long overrideMsgId,
6168
long timeToLive,
6269
boolean apnsProduction,
6370
int bigPushDuration,
6471
String apnsCollapseId,
65-
Map<String, Map<String, String>> thirdPartyChannel,
72+
Map<String, JsonObject> thirdPartyChannel,
6673
Map<String, JsonPrimitive> customData) {
6774
this.sendno = sendno;
6875
this.overrideMsgId = overrideMsgId;
@@ -127,11 +134,8 @@ public JsonElement toJSON() {
127134

128135
if (null != thirdPartyChannel && thirdPartyChannel.size() > 0) {
129136
JsonObject partyChannel = new JsonObject();
130-
for (Map.Entry<String, Map<String, String>> entry : thirdPartyChannel.entrySet()) {
131-
JsonObject channel = new JsonObject();
132-
for (Map.Entry<String, String> stringEntry : entry.getValue().entrySet()) {
133-
channel.addProperty(stringEntry.getKey(), stringEntry.getValue());
134-
}
137+
for (Map.Entry<String, JsonObject> entry : thirdPartyChannel.entrySet()) {
138+
JsonObject channel = entry.getValue();
135139
partyChannel.add(entry.getKey(), channel);
136140
}
137141
json.add(THIRD_PARTH_CHANNEl, partyChannel);
@@ -154,7 +158,7 @@ public static class Builder {
154158
private boolean apnsProduction = false;
155159
private int bigPushDuration = 0;
156160
private String apnsCollapseId;
157-
private Map<String, Map<String, String>> thirdPartyChannel;
161+
private Map<String, JsonObject> thirdPartyChannel;
158162
private Map<String, JsonPrimitive> customData;
159163

160164
public Builder setSendno(int sendno) {
@@ -187,11 +191,48 @@ public Builder setBigPushDuration(int bigPushDuration) {
187191
return this;
188192
}
189193

194+
@Deprecated
190195
public Map<String, Map<String, String>> getThirdPartyChannel() {
191-
return thirdPartyChannel;
196+
if (null != thirdPartyChannel) {
197+
Map<String, Map<String, String>> thirdPartyChannelRsp = new HashMap<>();
198+
Set<Map.Entry<String, JsonObject>> entrySet = thirdPartyChannel.entrySet();
199+
for (Map.Entry<String, JsonObject> entry : entrySet) {
200+
JsonObject entryValue = entry.getValue();
201+
Set<Map.Entry<String, JsonElement>> valueEntrySet = entryValue.entrySet();
202+
Map<String, String> valueMap = new HashMap<>();
203+
for (Map.Entry<String, JsonElement> valueEntry : valueEntrySet) {
204+
valueMap.put(valueEntry.getKey(), null == valueEntry.getValue() ? null : valueEntry.getValue().getAsString());
205+
}
206+
thirdPartyChannelRsp.put(entry.getKey(), valueMap);
207+
}
208+
return thirdPartyChannelRsp;
209+
}
210+
return null;
192211
}
193212

213+
@Deprecated
194214
public Builder setThirdPartyChannel(Map<String, Map<String, String>> thirdPartyChannel) {
215+
this.thirdPartyChannel = new HashMap<>();
216+
if (null != thirdPartyChannel) {
217+
Set<Map.Entry<String, Map<String, String>>> entrySet = thirdPartyChannel.entrySet();
218+
for (Map.Entry<String, Map<String, String>> entry : entrySet) {
219+
String key = entry.getKey();
220+
Map<String, String> valueMap = entry.getValue();
221+
JsonObject valueObj = new JsonObject();
222+
if (null != valueMap) {
223+
Set<Map.Entry<String, String>> valueEntrySet = valueMap.entrySet();
224+
for (Map.Entry<String, String> valueEntry : valueEntrySet) {
225+
valueObj.addProperty(valueEntry.getKey(), valueEntry.getValue());
226+
}
227+
this.thirdPartyChannel.put(key, valueObj);
228+
}
229+
}
230+
231+
}
232+
return this;
233+
}
234+
235+
public Builder setThirdPartyChannelV2(Map<String, JsonObject> thirdPartyChannel) {
195236
this.thirdPartyChannel = thirdPartyChannel;
196237
return this;
197238
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,10 @@ public int getSendno() {
169169
public Audience getAudience() {
170170
return audience;
171171
}
172+
173+
public Options getOptions() {
174+
return options;
175+
}
172176

173177
@Override
174178
public JsonElement toJSON() {

src/test/java/cn/jpush/api/push/GroupPushClientTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public void testSendGroupPush() {
2424
GroupPushClient groupPushClient = new GroupPushClient(GROUP_MASTER_SECRET, GROUP_PUSH_KEY);
2525
final PushPayload payload = buildPushObject_android();
2626
try {
27-
Map<String, PushResult> result = groupPushClient.sendGroupPush(payload);
27+
GroupPushResult groupPushresult = groupPushClient.sendGroupPush(payload);
28+
Map<String, PushResult> result = groupPushresult.getAppResultMap();
2829
for (Map.Entry<String, PushResult> entry : result.entrySet()) {
2930
PushResult pushResult = entry.getValue();
3031
PushResult.Error error = pushResult.error;

src/test/java/cn/jpush/api/push/model/OptionsTest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313
import cn.jiguang.common.ServiceHelper;
1414
import cn.jpush.api.FastTests;
1515

16+
import java.util.HashMap;
17+
import java.util.Map;
18+
1619
@Category(FastTests.class)
1720
public class OptionsTest {
1821

@@ -128,5 +131,61 @@ public void testBigPushDuration() {
128131

129132
assertThat(options.toJSON(), is((JsonElement) json));
130133
}
134+
135+
@Test
136+
public void testThirdPartyChannel() {
137+
int sendno = ServiceHelper.generateSendno();
138+
139+
Map<String, Map<String, String>> thirdMap = new HashMap<>();
140+
Map<String, String> huaweiMap = new HashMap<>();
141+
huaweiMap.put("distribution", "jpush");
142+
thirdMap.put("huawei", huaweiMap);
143+
144+
Options options = Options.newBuilder()
145+
.setSendno(sendno)
146+
.setThirdPartyChannel(thirdMap)
147+
.build();
148+
System.out.println("json string: " + options.toJSON());
149+
150+
JsonObject json = new JsonObject();
151+
JsonObject thirdPartyChannel = new JsonObject();
152+
JsonObject huawei = new JsonObject();
153+
huawei.addProperty("distribution", "jpush");
154+
thirdPartyChannel.add("huawei", huawei);
155+
json.add("sendno", new JsonPrimitive(sendno));
156+
json.add("apns_production", new JsonPrimitive(false));
157+
json.add("third_party_channel", thirdPartyChannel);
158+
159+
assertThat(options.toJSON(), is((JsonElement) json));
160+
}
161+
162+
@Test
163+
public void testThirdPartyChannelV2() {
164+
int sendno = ServiceHelper.generateSendno();
165+
166+
Map<String, JsonObject> thirdMap = new HashMap<>();
167+
JsonObject vivoJsonObj = new JsonObject();
168+
vivoJsonObj.addProperty("distribution", "ospush");
169+
vivoJsonObj.addProperty("classification", 1);
170+
thirdMap.put("vivo", vivoJsonObj);
171+
172+
Options options = Options.newBuilder()
173+
.setSendno(sendno)
174+
.setThirdPartyChannelV2(thirdMap)
175+
.build();
176+
System.out.println("json string: " + options.toJSON());
177+
178+
JsonObject json = new JsonObject();
179+
JsonObject thirdPartyChannel = new JsonObject();
180+
JsonObject vivo = new JsonObject();
181+
vivo.addProperty("distribution", "ospush");
182+
vivo.addProperty("classification", 1);
183+
thirdPartyChannel.add("vivo", vivo);
184+
json.add("sendno", new JsonPrimitive(sendno));
185+
json.add("apns_production", new JsonPrimitive(false));
186+
json.add("third_party_channel", thirdPartyChannel);
187+
188+
assertThat(options.toJSON(), is((JsonElement) json));
189+
}
131190
}
132191

0 commit comments

Comments
 (0)