Skip to content

Commit 83d22a5

Browse files
authored
Merge pull request #154 from xdx54321/master
支持最外层自定义参数
2 parents ce2db2c + 1bbb34d commit 83d22a5

File tree

5 files changed

+71
-13
lines changed

5 files changed

+71
-13
lines changed

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

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public class PushExample {
4646

4747
public static void main(String[] args) {
4848

49+
// 回调参数可参考下面方法
50+
testSendPushWithCustom();
4951
testSendPushWithCustomField();
5052
// testBatchSend();
5153
testSendPushWithCustomConfig();
@@ -630,5 +632,51 @@ public static void testSendPushWithCustomField() {
630632
}
631633
}
632634

635+
/**
636+
* 回调参数示例
637+
*/
638+
public static void testSendPushWithCustom() {
639+
640+
ClientConfig config = ClientConfig.getInstance();
641+
// Setup the custom hostname
642+
config.setPushHostName("https://api.jpush.cn");
643+
644+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, config);
645+
646+
Notification notification = Notification.newBuilder()
647+
.addPlatformNotification(AndroidNotification.newBuilder()
648+
.setAlert(ALERT)
649+
.setTitle("Alert test")
650+
.build())
651+
.build();
652+
653+
JsonObject callback = new JsonObject();
654+
callback.addProperty("url", "https://www.jiguagn.cn/callback");
655+
JsonObject params = new JsonObject();
656+
params.addProperty("name", "joe");
657+
params.addProperty("age", 26);
658+
callback.add("params", params);
659+
callback.addProperty("type", 3);
660+
661+
PushPayload.Builder payloadBuilder = new PushPayload.Builder()
662+
.setPlatform(Platform.all())
663+
.setAudience(Audience.all())
664+
.setNotification(notification)
665+
.addCustom("callback", callback);
666+
667+
try {
668+
PushResult result = jpushClient.sendPush(payloadBuilder.build());
669+
LOG.info("Got result - " + result);
670+
} catch (APIConnectionException e) {
671+
LOG.error("Connection error. Should retry later. ", e);
672+
} catch (APIRequestException e) {
673+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
674+
LOG.info("HTTP Status: " + e.getStatus());
675+
LOG.info("Error Code: " + e.getErrorCode());
676+
LOG.info("Error Message: " + e.getErrorMessage());
677+
LOG.info("Msg ID: " + e.getMsgId());
678+
}
679+
}
680+
633681
}
634682

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.3-SNAPSHOT</version>
6+
<version>3.4.3</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/model/BatchPushResult.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@
1212
import java.util.List;
1313
import java.util.Map;
1414

15-
/**
16-
* @author xudanxia
17-
* @Desc
18-
* @date 2019-08-22.
19-
*/
2015
public class BatchPushResult extends BaseResult {
2116

2217
private static final Type RESULT_TYPE = new TypeToken<Map<String, PushResult>>() {}.getType();

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

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import cn.jpush.api.push.model.notification.Notification;
1515
import cn.jpush.api.push.model.notification.PlatformNotification;
1616

17+
import java.util.LinkedHashMap;
18+
import java.util.Map;
19+
1720
/**
1821
* The object you should build for sending a push.
1922
*
@@ -54,9 +57,10 @@ public class PushPayload implements PushModel {
5457
private SMS sms;
5558
private String cid;
5659
private String target;
60+
protected Map<String, JsonObject> custom;
5761

5862
private PushPayload(Platform platform, Audience audience,
59-
Notification notification, Message message, Options options, SMS sms, String cid, String target) {
63+
Notification notification, Message message, Options options, SMS sms, String cid, String target, Map<String, JsonObject> custom) {
6064
this.platform = platform;
6165
this.audience = audience;
6266
this.notification = notification;
@@ -65,6 +69,7 @@ private PushPayload(Platform platform, Audience audience,
6569
this.sms = sms;
6670
this.cid = cid;
6771
this.target = target;
72+
this.custom = custom;
6873
}
6974

7075
public PushPayload setCid(String cid) {
@@ -192,6 +197,11 @@ public JsonElement toJSON() {
192197
if (null != target) {
193198
json.addProperty(TARGET, target);
194199
}
200+
if (null != custom) {
201+
for (Map.Entry<String, JsonObject> entry : custom.entrySet()) {
202+
json.add(entry.getKey(), entry.getValue());
203+
}
204+
}
195205

196206
return json;
197207
}
@@ -252,6 +262,11 @@ public static class Builder {
252262
private SMS sms = null;
253263
private String cid;
254264
private String target;
265+
private Map<String, JsonObject> custom;
266+
267+
public Builder() {
268+
this.custom = new LinkedHashMap<>();
269+
}
255270

256271
public Builder setTarget(String target) {
257272
this.target = target;
@@ -293,6 +308,11 @@ public Builder setCid(String cid) {
293308
return this;
294309
}
295310

311+
public Builder addCustom(String field, JsonObject jsonObject) {
312+
this.custom.put(field, jsonObject);
313+
return this;
314+
}
315+
296316
public PushPayload build() {
297317

298318
if (StringUtils.isTrimedEmpty(target)) {
@@ -312,7 +332,7 @@ public PushPayload build() {
312332
options = Options.sendno();
313333
}
314334

315-
return new PushPayload(platform, audience, notification, message, options, sms, cid, target);
335+
return new PushPayload(platform, audience, notification, message, options, sms, cid, target, custom);
316336
}
317337
}
318338
}

src/main/java/cn/jpush/api/report/MessageDetailResult.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,6 @@
1111
import java.util.ArrayList;
1212
import java.util.List;
1313

14-
/**
15-
* @author xdx
16-
* @Desc
17-
* @date 2019-07-29.
18-
*/
1914
public class MessageDetailResult extends BaseResult {
2015

2116
private static final Type RECEIVED_TYPE = new TypeToken<List<MessageDetailResult.Received>>() {}.getType();

0 commit comments

Comments
 (0)