diff --git a/README.md b/README.md index 2c8608df..78bde550 100644 --- a/README.md +++ b/README.md @@ -18,13 +18,13 @@ ## 安装 ### maven 方式 -将下边的依赖条件放到你项目的 maven pom.xml 文件里。v3.3.10 为例,最新版本请看[Release页面](https://github.com/jpush/jpush-api-java-client/releases) +将下边的依赖条件放到你项目的 maven pom.xml 文件里。v3.4.6 为例,最新版本请看[Release页面](https://github.com/jpush/jpush-api-java-client/releases) ```Java cn.jpush.api jpush-client - 3.3.10 + 3.4.6 ``` ### jar 包方式 @@ -45,7 +45,7 @@ cn.jpush.api jiguang-common - 1.1.4 + 1.1.8 io.netty diff --git a/example/main/java/cn/jpush/api/examples/PushExample.java b/example/main/java/cn/jpush/api/examples/PushExample.java index a18dbb09..73b7fcad 100644 --- a/example/main/java/cn/jpush/api/examples/PushExample.java +++ b/example/main/java/cn/jpush/api/examples/PushExample.java @@ -10,6 +10,7 @@ import cn.jpush.api.JPushClient; import cn.jpush.api.push.CIDResult; import cn.jpush.api.push.GroupPushClient; +import cn.jpush.api.push.GroupPushResult; import cn.jpush.api.push.PushResult; import cn.jpush.api.push.model.*; import cn.jpush.api.push.model.audience.Audience; @@ -227,7 +228,8 @@ public void testSendGroupPush() { GroupPushClient groupPushClient = new GroupPushClient(GROUP_MASTER_SECRET, GROUP_PUSH_KEY); final PushPayload payload = buildPushObject_android_and_ios(); try { - Map result = groupPushClient.sendGroupPush(payload); + GroupPushResult groupPushResult = groupPushClient.sendGroupPush(payload); + Map result = groupPushResult.getAppResultMap(); for (Map.Entry entry : result.entrySet()) { PushResult pushResult = entry.getValue(); PushResult.Error error = pushResult.error; diff --git a/pom.xml b/pom.xml index a1a8c99e..5ca7840e 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.4.6-SNAPSHOT + 3.4.7-SNAPSHOT jar https://github.com/jpush/jpush-api-java-client JPush API Java Client diff --git a/src/main/java/cn/jpush/api/push/GroupPushClient.java b/src/main/java/cn/jpush/api/push/GroupPushClient.java index 39103db9..d1f0adc4 100644 --- a/src/main/java/cn/jpush/api/push/GroupPushClient.java +++ b/src/main/java/cn/jpush/api/push/GroupPushClient.java @@ -15,6 +15,9 @@ import cn.jpush.api.push.model.EncryptPushPayload; import cn.jpush.api.push.model.PushPayload; import com.google.gson.Gson; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; import com.google.gson.reflect.TypeToken; import java.util.Map; @@ -29,6 +32,8 @@ public class GroupPushClient { private String _groupPushPath; private String _encryptType; private Gson _gson = new Gson(); + private JsonParser _jsonParser = new JsonParser(); + public GroupPushClient(String groupMasterSecret, String groupKey) { this(groupMasterSecret, groupKey, null, ClientConfig.getInstance()); @@ -43,11 +48,17 @@ public GroupPushClient(String groupMasterSecret, String groupKey, HttpProxy prox this._httpClient = new NativeHttpClient(authCode, proxy, conf); } - public Map sendGroupPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException { + public GroupPushResult sendGroupPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException { Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null"); ResponseWrapper response = _httpClient.sendPost(_baseUrl + _groupPushPath, getEncryptData(pushPayload)); - return _gson.fromJson(response.responseContent, - new TypeToken>(){}.getType()); + // 2020-8-6 兼容分组推送新返回的group_msgid结构 + JsonObject responseJson = _jsonParser.parse(response.responseContent).getAsJsonObject(); + String groupMsgIdKey = "group_msgid"; + JsonElement _groupMsgId = responseJson.get(groupMsgIdKey); + String groupMsgId = null != _groupMsgId ? _groupMsgId.getAsString() : ""; + responseJson.remove(groupMsgIdKey); + Map result = _gson.fromJson(responseJson, new TypeToken>(){}.getType()); + return new GroupPushResult(groupMsgId, result); } /** diff --git a/src/main/java/cn/jpush/api/push/GroupPushResult.java b/src/main/java/cn/jpush/api/push/GroupPushResult.java new file mode 100644 index 00000000..3a49abe2 --- /dev/null +++ b/src/main/java/cn/jpush/api/push/GroupPushResult.java @@ -0,0 +1,38 @@ +package cn.jpush.api.push; + +import java.util.Map; + +/** + * @author tp + * @since 2020/8/6 + */ +public class GroupPushResult { + + private Map appResultMap; + + private String groupMsgId; + + public GroupPushResult() { + } + + public GroupPushResult(String groupMsgId, Map appResultMap) { + this.groupMsgId = groupMsgId; + this.appResultMap = appResultMap; + } + + public Map getAppResultMap() { + return appResultMap; + } + + public void setAppResultMap(Map appResultMap) { + this.appResultMap = appResultMap; + } + + public String getGroupMsgId() { + return groupMsgId; + } + + public void setGroupMsgId(String groupMsgId) { + this.groupMsgId = groupMsgId; + } +} diff --git a/src/main/java/cn/jpush/api/push/model/Options.java b/src/main/java/cn/jpush/api/push/model/Options.java index 6f4b6a1b..0e0f07e6 100644 --- a/src/main/java/cn/jpush/api/push/model/Options.java +++ b/src/main/java/cn/jpush/api/push/model/Options.java @@ -6,8 +6,10 @@ import cn.jiguang.common.ServiceHelper; import cn.jiguang.common.utils.Preconditions; +import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; public class Options implements PushModel { @@ -32,29 +34,34 @@ public class Options implements PushModel { /** - * example - * "third_party_channel": { - * "xiaomi": { - * "distribution": "ospush" - * }, - * "huawei": { - * "distribution": "jpush" - * }, - * "meizu": { - * "distribution": "jpush" - * }, - * "fcm": { - * "distribution": "ospush" - * }, - * "oppo": { - * "distribution": "ospush" - * }, - * "vivo": { - * "distribution": "ospush" + * { + * "third_party_channel":{ + * "xiaomi":{ + * "distribution":"ospush", + * "channel_id":"*******" + * }, + * "huawei":{ + * "distribution":"jpush" + * }, + * "meizu":{ + * "distribution":"jpush" + * }, + * "fcm":{ + * "distribution":"ospush" + * }, + * "oppo":{ + * "distribution":"ospush", + * "channel_id":"*******" + * }, + * "vivo":{ + * "distribution":"ospush", + * "classification":0 // 2020/06 新增,和vivo官方字段含义一致 0 代表运营消息,1 代表系统消息,不填vivo官方默认为0 + * // 使用此字段时,需使用setThirdPartyChannelV2方法,因为此值只能为整数形式 + * } * } * } */ - private Map> thirdPartyChannel; + private Map thirdPartyChannel; private Options(int sendno, long overrideMsgId, @@ -62,7 +69,7 @@ private Options(int sendno, boolean apnsProduction, int bigPushDuration, String apnsCollapseId, - Map> thirdPartyChannel, + Map thirdPartyChannel, Map customData) { this.sendno = sendno; this.overrideMsgId = overrideMsgId; @@ -127,11 +134,8 @@ public JsonElement toJSON() { if (null != thirdPartyChannel && thirdPartyChannel.size() > 0) { JsonObject partyChannel = new JsonObject(); - for (Map.Entry> entry : thirdPartyChannel.entrySet()) { - JsonObject channel = new JsonObject(); - for (Map.Entry stringEntry : entry.getValue().entrySet()) { - channel.addProperty(stringEntry.getKey(), stringEntry.getValue()); - } + for (Map.Entry entry : thirdPartyChannel.entrySet()) { + JsonObject channel = entry.getValue(); partyChannel.add(entry.getKey(), channel); } json.add(THIRD_PARTH_CHANNEl, partyChannel); @@ -154,7 +158,7 @@ public static class Builder { private boolean apnsProduction = false; private int bigPushDuration = 0; private String apnsCollapseId; - private Map> thirdPartyChannel; + private Map thirdPartyChannel; private Map customData; public Builder setSendno(int sendno) { @@ -187,11 +191,48 @@ public Builder setBigPushDuration(int bigPushDuration) { return this; } + @Deprecated public Map> getThirdPartyChannel() { - return thirdPartyChannel; + if (null != thirdPartyChannel) { + Map> thirdPartyChannelRsp = new HashMap<>(); + Set> entrySet = thirdPartyChannel.entrySet(); + for (Map.Entry entry : entrySet) { + JsonObject entryValue = entry.getValue(); + Set> valueEntrySet = entryValue.entrySet(); + Map valueMap = new HashMap<>(); + for (Map.Entry valueEntry : valueEntrySet) { + valueMap.put(valueEntry.getKey(), null == valueEntry.getValue() ? null : valueEntry.getValue().getAsString()); + } + thirdPartyChannelRsp.put(entry.getKey(), valueMap); + } + return thirdPartyChannelRsp; + } + return null; } + @Deprecated public Builder setThirdPartyChannel(Map> thirdPartyChannel) { + this.thirdPartyChannel = new HashMap<>(); + if (null != thirdPartyChannel) { + Set>> entrySet = thirdPartyChannel.entrySet(); + for (Map.Entry> entry : entrySet) { + String key = entry.getKey(); + Map valueMap = entry.getValue(); + JsonObject valueObj = new JsonObject(); + if (null != valueMap) { + Set> valueEntrySet = valueMap.entrySet(); + for (Map.Entry valueEntry : valueEntrySet) { + valueObj.addProperty(valueEntry.getKey(), valueEntry.getValue()); + } + this.thirdPartyChannel.put(key, valueObj); + } + } + + } + return this; + } + + public Builder setThirdPartyChannelV2(Map thirdPartyChannel) { this.thirdPartyChannel = thirdPartyChannel; return this; } diff --git a/src/main/java/cn/jpush/api/push/model/PushPayload.java b/src/main/java/cn/jpush/api/push/model/PushPayload.java index 575a38b0..6338d86e 100644 --- a/src/main/java/cn/jpush/api/push/model/PushPayload.java +++ b/src/main/java/cn/jpush/api/push/model/PushPayload.java @@ -169,6 +169,10 @@ public int getSendno() { public Audience getAudience() { return audience; } + + public Options getOptions() { + return options; + } @Override public JsonElement toJSON() { diff --git a/src/test/java/cn/jpush/api/push/GroupPushClientTest.java b/src/test/java/cn/jpush/api/push/GroupPushClientTest.java index 2a476c1a..574871b1 100644 --- a/src/test/java/cn/jpush/api/push/GroupPushClientTest.java +++ b/src/test/java/cn/jpush/api/push/GroupPushClientTest.java @@ -24,7 +24,8 @@ public void testSendGroupPush() { GroupPushClient groupPushClient = new GroupPushClient(GROUP_MASTER_SECRET, GROUP_PUSH_KEY); final PushPayload payload = buildPushObject_android(); try { - Map result = groupPushClient.sendGroupPush(payload); + GroupPushResult groupPushresult = groupPushClient.sendGroupPush(payload); + Map result = groupPushresult.getAppResultMap(); for (Map.Entry entry : result.entrySet()) { PushResult pushResult = entry.getValue(); PushResult.Error error = pushResult.error; diff --git a/src/test/java/cn/jpush/api/push/model/OptionsTest.java b/src/test/java/cn/jpush/api/push/model/OptionsTest.java index eb67e519..09356610 100644 --- a/src/test/java/cn/jpush/api/push/model/OptionsTest.java +++ b/src/test/java/cn/jpush/api/push/model/OptionsTest.java @@ -13,6 +13,9 @@ import cn.jiguang.common.ServiceHelper; import cn.jpush.api.FastTests; +import java.util.HashMap; +import java.util.Map; + @Category(FastTests.class) public class OptionsTest { @@ -128,5 +131,61 @@ public void testBigPushDuration() { assertThat(options.toJSON(), is((JsonElement) json)); } + + @Test + public void testThirdPartyChannel() { + int sendno = ServiceHelper.generateSendno(); + + Map> thirdMap = new HashMap<>(); + Map huaweiMap = new HashMap<>(); + huaweiMap.put("distribution", "jpush"); + thirdMap.put("huawei", huaweiMap); + + Options options = Options.newBuilder() + .setSendno(sendno) + .setThirdPartyChannel(thirdMap) + .build(); + System.out.println("json string: " + options.toJSON()); + + JsonObject json = new JsonObject(); + JsonObject thirdPartyChannel = new JsonObject(); + JsonObject huawei = new JsonObject(); + huawei.addProperty("distribution", "jpush"); + thirdPartyChannel.add("huawei", huawei); + json.add("sendno", new JsonPrimitive(sendno)); + json.add("apns_production", new JsonPrimitive(false)); + json.add("third_party_channel", thirdPartyChannel); + + assertThat(options.toJSON(), is((JsonElement) json)); + } + + @Test + public void testThirdPartyChannelV2() { + int sendno = ServiceHelper.generateSendno(); + + Map thirdMap = new HashMap<>(); + JsonObject vivoJsonObj = new JsonObject(); + vivoJsonObj.addProperty("distribution", "ospush"); + vivoJsonObj.addProperty("classification", 1); + thirdMap.put("vivo", vivoJsonObj); + + Options options = Options.newBuilder() + .setSendno(sendno) + .setThirdPartyChannelV2(thirdMap) + .build(); + System.out.println("json string: " + options.toJSON()); + + JsonObject json = new JsonObject(); + JsonObject thirdPartyChannel = new JsonObject(); + JsonObject vivo = new JsonObject(); + vivo.addProperty("distribution", "ospush"); + vivo.addProperty("classification", 1); + thirdPartyChannel.add("vivo", vivo); + json.add("sendno", new JsonPrimitive(sendno)); + json.add("apns_production", new JsonPrimitive(false)); + json.add("third_party_channel", thirdPartyChannel); + + assertThat(options.toJSON(), is((JsonElement) json)); + } }