Skip to content

Commit 962b19b

Browse files
author
yinyongxiang
committed
升级版本
1 parent 724ddef commit 962b19b

File tree

7 files changed

+82
-25
lines changed

7 files changed

+82
-25
lines changed

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

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,48 @@ public void onSucceed(ResponseWrapper responseWrapper) {
8080

8181
public static void testSendPush() {
8282
ClientConfig clientConfig = ClientConfig.getInstance();
83-
clientConfig.setPushHostName("http://113.31.136.110:8800");
84-
clientConfig.setEncryptType(EncryptKeys.ENCRYPT_SMS_TYPE);
8583
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
8684
// String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
8785
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
8886
// Call setHttpClient to set httpClient,
8987
// If you don't invoke this method, default httpClient will use NativeHttpClient.
9088

89+
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
90+
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
91+
// jpushClient.getPushClient().setHttpClient(httpClient);
92+
final PushPayload payload = buildPushObject_android_and_ios();
93+
// // For push, all you need do is to build PushPayload object.
94+
// PushPayload payload = buildPushObject_all_alias_alert();
95+
try {
96+
PushResult result = jpushClient.sendPush(payload);
97+
LOG.info("Got result - " + result);
98+
System.out.println(result);
99+
// 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
100+
// If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
101+
// jpushClient.close();
102+
} catch (APIConnectionException e) {
103+
LOG.error("Connection error. Should retry later. ", e);
104+
LOG.error("Sendno: " + payload.getSendno());
105+
106+
} catch (APIRequestException e) {
107+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
108+
LOG.info("HTTP Status: " + e.getStatus());
109+
LOG.info("Error Code: " + e.getErrorCode());
110+
LOG.info("Error Message: " + e.getErrorMessage());
111+
LOG.info("Msg ID: " + e.getMsgId());
112+
LOG.error("Sendno: " + payload.getSendno());
113+
}
114+
}
115+
116+
public static void testSendPushWithEncrypt() {
117+
ClientConfig clientConfig = ClientConfig.getInstance();
118+
clientConfig.setEncryptType(EncryptKeys.ENCRYPT_SMS2_TYPE);
119+
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
120+
// String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
121+
// Here you can use NativeHttpClient or NettyHttpClient or ApacheHttpClient.
122+
// Call setHttpClient to set httpClient,
123+
// If you don't invoke this method, default httpClient will use NativeHttpClient.
124+
91125
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
92126
// NettyHttpClient httpClient =new NettyHttpClient(authCode, null, clientConfig);
93127
// jpushClient.getPushClient().setHttpClient(httpClient);
@@ -118,8 +152,6 @@ public static void testSendPush() {
118152
//use String to build PushPayload instance
119153
public static void testSendPush_fromJSON() {
120154
ClientConfig clientConfig = ClientConfig.getInstance();
121-
clientConfig.setPushHostName("http://113.31.136.110:8800");
122-
clientConfig.setEncryptType(EncryptKeys.ENCRYPT_SMS_TYPE);
123155
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
124156
Gson gson = new GsonBuilder()
125157
.registerTypeAdapter(PlatformNotification.class, new InterfaceAdapter<PlatformNotification>())

pom.xml

Lines changed: 2 additions & 2 deletions
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.3.11-SNAPSHOT</version>
6+
<version>3.3.12</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>
@@ -42,7 +42,7 @@
4242
<dependency>
4343
<groupId>cn.jpush.api</groupId>
4444
<artifactId>jiguang-common</artifactId>
45-
<version>1.1.4</version>
45+
<version>1.1.6</version>
4646
</dependency>
4747
<dependency>
4848
<groupId>org.apache.httpcomponents</groupId>

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

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
import cn.jiguang.common.resp.APIConnectionException;
99
import cn.jiguang.common.resp.APIRequestException;
1010
import cn.jiguang.common.resp.ResponseWrapper;
11+
import cn.jiguang.common.utils.Base64;
1112
import cn.jiguang.common.utils.Preconditions;
13+
import cn.jiguang.common.utils.sm2.SM2Util;
14+
import cn.jpush.api.push.model.EncryptKeys;
15+
import cn.jpush.api.push.model.EncryptPushPayload;
1216
import cn.jpush.api.push.model.PushPayload;
1317
import com.google.gson.Gson;
1418
import com.google.gson.reflect.TypeToken;
@@ -23,6 +27,7 @@ public class GroupPushClient {
2327
private IHttpClient _httpClient;
2428
private String _baseUrl;
2529
private String _groupPushPath;
30+
private String _encryptType;
2631
private Gson _gson = new Gson();
2732

2833
public GroupPushClient(String groupMasterSecret, String groupKey) {
@@ -33,16 +38,39 @@ public GroupPushClient(String groupMasterSecret, String groupKey, HttpProxy prox
3338
ServiceHelper.checkBasic(groupKey, groupMasterSecret);
3439
this._baseUrl = (String) conf.get(ClientConfig.PUSH_HOST_NAME);
3540
this._groupPushPath = (String) conf.get(ClientConfig.GROUP_PUSH_PATH);
41+
this._encryptType = (String) conf.get(ClientConfig.ENCRYPT_TYPE);
3642
String authCode = ServiceHelper.getBasicAuthorization("group-" + groupKey, groupMasterSecret);
3743
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
3844
}
3945

4046
public Map<String, PushResult> sendGroupPush(PushPayload pushPayload) throws APIConnectionException, APIRequestException {
4147
Preconditions.checkArgument(! (null == pushPayload), "pushPayload should not be null");
42-
43-
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _groupPushPath, pushPayload.toString());
48+
ResponseWrapper response = _httpClient.sendPost(_baseUrl + _groupPushPath, getEncryptData(pushPayload));
4449
return _gson.fromJson(response.responseContent,
4550
new TypeToken<Map<String, PushResult>>(){}.getType());
4651
}
4752

53+
/**
54+
* 获取加密的payload数据
55+
* @param pushPayload
56+
* @return
57+
*/
58+
private String getEncryptData(PushPayload pushPayload) {
59+
if (_encryptType.isEmpty()) {
60+
return pushPayload.toString();
61+
}
62+
if (EncryptKeys.ENCRYPT_SMS2_TYPE.equals(_encryptType)) {
63+
EncryptPushPayload encryptPushPayload = new EncryptPushPayload();
64+
try {
65+
encryptPushPayload.setPayload(String.valueOf(Base64.encode(SM2Util.encrypt(pushPayload.toString(), EncryptKeys.DEFAULT_SM2_ENCRYPT_KEY))));
66+
} catch (Exception e) {
67+
throw new RuntimeException("encrypt word exception", e);
68+
}
69+
encryptPushPayload.setAudience(pushPayload.getAudience());
70+
return encryptPushPayload.toString();
71+
}
72+
// 不支持的加密默认不加密
73+
return pushPayload.toString();
74+
}
75+
4876
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,7 +272,7 @@ private String getEncryptData(PushPayload pushPayload) {
272272
if (_encryptType.isEmpty()) {
273273
return pushPayload.toString();
274274
}
275-
if (EncryptKeys.ENCRYPT_SMS_TYPE.equals(_encryptType)) {
275+
if (EncryptKeys.ENCRYPT_SMS2_TYPE.equals(_encryptType)) {
276276
EncryptPushPayload encryptPushPayload = new EncryptPushPayload();
277277
try {
278278
encryptPushPayload.setPayload(String.valueOf(Base64.encode(SM2Util.encrypt(pushPayload.toString(), EncryptKeys.DEFAULT_SM2_ENCRYPT_KEY))));
@@ -295,7 +295,7 @@ private String getEncryptData(String pushPayload, Audience audience) {
295295
if (_encryptType.isEmpty()) {
296296
return pushPayload;
297297
}
298-
if (EncryptKeys.ENCRYPT_SMS_TYPE.equals(_encryptType)) {
298+
if (EncryptKeys.ENCRYPT_SMS2_TYPE.equals(_encryptType)) {
299299
EncryptPushPayload encryptPushPayload = new EncryptPushPayload();
300300
try {
301301
encryptPushPayload.setPayload(String.valueOf(Base64.encode(SM2Util.encrypt(pushPayload, EncryptKeys.DEFAULT_SM2_ENCRYPT_KEY))));

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

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

33
public class EncryptKeys {
44

5-
public static String ENCRYPT_SMS_TYPE = "SM2";
5+
public static String ENCRYPT_SMS2_TYPE = "SM2";
66
public static String DEFAULT_SM2_ENCRYPT_KEY = "BPj6Mj/T444gxPaHc6CDCizMRp4pEl14WI2lvIbdEK2c+5XiSqmQt2TQc8hMMZqfxcDqUNQW95puAfQx1asv3rU=";
77

88
}

src/main/java/cn/jpush/api/push/model/audience/Audience.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,18 @@ public static Audience fromJsonElement(JsonElement jsonElement) {
124124
if (jsonElement == null) {
125125
return null;
126126
}
127+
boolean all = !jsonElement.isJsonObject();
128+
if (all) {
129+
return new Audience(all, null);
130+
}
127131
JsonObject jsonObject = jsonElement.getAsJsonObject();
128-
boolean all = jsonObject.get(ALL).getAsBoolean();
129-
JsonArray jsonArray = jsonObject.getAsJsonArray("targets");
130132
Set<AudienceTarget> audienceTargetSet = new HashSet<>();
131-
if (jsonArray != null) {
132-
for (int i=0; i<jsonArray.size(); i++) {
133-
audienceTargetSet.add(AudienceTarget.fromJsonElement(jsonArray.get(i).getAsJsonObject()));
133+
for (AudienceType type : AudienceType.values()) {
134+
JsonArray jsonArray = jsonObject.getAsJsonArray(type.value());
135+
if (jsonArray == null) {
136+
continue;
134137
}
138+
audienceTargetSet.add(AudienceTarget.fromJsonElement(jsonArray, type));
135139
}
136140
return new Audience(all, audienceTargetSet);
137141
}

src/main/java/cn/jpush/api/push/model/audience/AudienceTarget.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import cn.jpush.api.push.model.PushModel;
55
import com.google.gson.JsonArray;
66
import com.google.gson.JsonElement;
7-
import com.google.gson.JsonObject;
87
import com.google.gson.JsonPrimitive;
98

109
import java.util.Collection;
@@ -99,20 +98,14 @@ public JsonElement toJSON() {
9998
return array;
10099
}
101100

102-
public static AudienceTarget fromJsonElement(JsonElement jsonElement) {
103-
if (jsonElement == null) {
104-
return null;
105-
}
106-
JsonObject jsonObject = jsonElement.getAsJsonObject();
107-
JsonArray jsonArray = jsonObject.getAsJsonArray("values");
108-
String audienceType = jsonObject.get("audienceType").getAsString();
101+
public static AudienceTarget fromJsonElement(JsonArray jsonArray, AudienceType type) {
109102
Set<String> stringSet = new HashSet<>();
110103
if (jsonArray != null) {
111104
for (int i=0; i<jsonArray.size(); i++) {
112105
stringSet.add(jsonArray.get(i).getAsString());
113106
}
114107
}
115-
return new AudienceTarget(AudienceType.valueOf(audienceType), stringSet);
108+
return new AudienceTarget(type, stringSet);
116109
}
117110

118111

0 commit comments

Comments
 (0)