Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.10</version>
<version>3.4.6</version>
</dependency>
```
### jar 包方式
Expand All @@ -45,7 +45,7 @@
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.4</version>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
Expand Down
4 changes: 3 additions & 1 deletion example/main/java/cn/jpush/api/examples/PushExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, PushResult> result = groupPushClient.sendGroupPush(payload);
GroupPushResult groupPushResult = groupPushClient.sendGroupPush(payload);
Map<String, PushResult> result = groupPushResult.getAppResultMap();
for (Map.Entry<String, PushResult> entry : result.entrySet()) {
PushResult pushResult = entry.getValue();
PushResult.Error error = pushResult.error;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.6-SNAPSHOT</version>
<version>3.4.7-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/cn/jpush/api/push/GroupPushClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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());
Expand All @@ -43,11 +48,17 @@ public GroupPushClient(String groupMasterSecret, String groupKey, HttpProxy prox
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
}

public Map<String, PushResult> 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<Map<String, PushResult>>(){}.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<String, PushResult> result = _gson.fromJson(responseJson, new TypeToken<Map<String, PushResult>>(){}.getType());
return new GroupPushResult(groupMsgId, result);
}

/**
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/cn/jpush/api/push/GroupPushResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package cn.jpush.api.push;

import java.util.Map;

/**
* @author tp
* @since 2020/8/6
*/
public class GroupPushResult {

private Map<String, PushResult> appResultMap;

private String groupMsgId;

public GroupPushResult() {
}

public GroupPushResult(String groupMsgId, Map<String, PushResult> appResultMap) {
this.groupMsgId = groupMsgId;
this.appResultMap = appResultMap;
}

public Map<String, PushResult> getAppResultMap() {
return appResultMap;
}

public void setAppResultMap(Map<String, PushResult> appResultMap) {
this.appResultMap = appResultMap;
}

public String getGroupMsgId() {
return groupMsgId;
}

public void setGroupMsgId(String groupMsgId) {
this.groupMsgId = groupMsgId;
}
}
97 changes: 69 additions & 28 deletions src/main/java/cn/jpush/api/push/model/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand All @@ -32,37 +34,42 @@ 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<String, Map<String, String>> thirdPartyChannel;
private Map<String, JsonObject> thirdPartyChannel;

private Options(int sendno,
long overrideMsgId,
long timeToLive,
boolean apnsProduction,
int bigPushDuration,
String apnsCollapseId,
Map<String, Map<String, String>> thirdPartyChannel,
Map<String, JsonObject> thirdPartyChannel,
Map<String, JsonPrimitive> customData) {
this.sendno = sendno;
this.overrideMsgId = overrideMsgId;
Expand Down Expand Up @@ -127,11 +134,8 @@ public JsonElement toJSON() {

if (null != thirdPartyChannel && thirdPartyChannel.size() > 0) {
JsonObject partyChannel = new JsonObject();
for (Map.Entry<String, Map<String, String>> entry : thirdPartyChannel.entrySet()) {
JsonObject channel = new JsonObject();
for (Map.Entry<String, String> stringEntry : entry.getValue().entrySet()) {
channel.addProperty(stringEntry.getKey(), stringEntry.getValue());
}
for (Map.Entry<String, JsonObject> entry : thirdPartyChannel.entrySet()) {
JsonObject channel = entry.getValue();
partyChannel.add(entry.getKey(), channel);
}
json.add(THIRD_PARTH_CHANNEl, partyChannel);
Expand All @@ -154,7 +158,7 @@ public static class Builder {
private boolean apnsProduction = false;
private int bigPushDuration = 0;
private String apnsCollapseId;
private Map<String, Map<String, String>> thirdPartyChannel;
private Map<String, JsonObject> thirdPartyChannel;
private Map<String, JsonPrimitive> customData;

public Builder setSendno(int sendno) {
Expand Down Expand Up @@ -187,11 +191,48 @@ public Builder setBigPushDuration(int bigPushDuration) {
return this;
}

@Deprecated
public Map<String, Map<String, String>> getThirdPartyChannel() {
return thirdPartyChannel;
if (null != thirdPartyChannel) {
Map<String, Map<String, String>> thirdPartyChannelRsp = new HashMap<>();
Set<Map.Entry<String, JsonObject>> entrySet = thirdPartyChannel.entrySet();
for (Map.Entry<String, JsonObject> entry : entrySet) {
JsonObject entryValue = entry.getValue();
Set<Map.Entry<String, JsonElement>> valueEntrySet = entryValue.entrySet();
Map<String, String> valueMap = new HashMap<>();
for (Map.Entry<String, JsonElement> 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<String, Map<String, String>> thirdPartyChannel) {
this.thirdPartyChannel = new HashMap<>();
if (null != thirdPartyChannel) {
Set<Map.Entry<String, Map<String, String>>> entrySet = thirdPartyChannel.entrySet();
for (Map.Entry<String, Map<String, String>> entry : entrySet) {
String key = entry.getKey();
Map<String, String> valueMap = entry.getValue();
JsonObject valueObj = new JsonObject();
if (null != valueMap) {
Set<Map.Entry<String, String>> valueEntrySet = valueMap.entrySet();
for (Map.Entry<String, String> valueEntry : valueEntrySet) {
valueObj.addProperty(valueEntry.getKey(), valueEntry.getValue());
}
this.thirdPartyChannel.put(key, valueObj);
}
}

}
return this;
}

public Builder setThirdPartyChannelV2(Map<String, JsonObject> thirdPartyChannel) {
this.thirdPartyChannel = thirdPartyChannel;
return this;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/cn/jpush/api/push/model/PushPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ public int getSendno() {
public Audience getAudience() {
return audience;
}

public Options getOptions() {
return options;
}

@Override
public JsonElement toJSON() {
Expand Down
3 changes: 2 additions & 1 deletion src/test/java/cn/jpush/api/push/GroupPushClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public void testSendGroupPush() {
GroupPushClient groupPushClient = new GroupPushClient(GROUP_MASTER_SECRET, GROUP_PUSH_KEY);
final PushPayload payload = buildPushObject_android();
try {
Map<String, PushResult> result = groupPushClient.sendGroupPush(payload);
GroupPushResult groupPushresult = groupPushClient.sendGroupPush(payload);
Map<String, PushResult> result = groupPushresult.getAppResultMap();
for (Map.Entry<String, PushResult> entry : result.entrySet()) {
PushResult pushResult = entry.getValue();
PushResult.Error error = pushResult.error;
Expand Down
59 changes: 59 additions & 0 deletions src/test/java/cn/jpush/api/push/model/OptionsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -128,5 +131,61 @@ public void testBigPushDuration() {

assertThat(options.toJSON(), is((JsonElement) json));
}

@Test
public void testThirdPartyChannel() {
int sendno = ServiceHelper.generateSendno();

Map<String, Map<String, String>> thirdMap = new HashMap<>();
Map<String, String> 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<String, JsonObject> 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));
}
}