diff --git a/example/main/java/cn/jpush/api/examples/PushExample.java b/example/main/java/cn/jpush/api/examples/PushExample.java index f452ccd9..a18dbb09 100644 --- a/example/main/java/cn/jpush/api/examples/PushExample.java +++ b/example/main/java/cn/jpush/api/examples/PushExample.java @@ -46,6 +46,8 @@ public class PushExample { public static void main(String[] args) { + // 回调参数可参考下面方法 + testSendPushWithCustom(); testSendPushWithCustomField(); // testBatchSend(); testSendPushWithCustomConfig(); @@ -630,5 +632,51 @@ public static void testSendPushWithCustomField() { } } + /** + * 回调参数示例 + */ + public static void testSendPushWithCustom() { + + ClientConfig config = ClientConfig.getInstance(); + // Setup the custom hostname + config.setPushHostName("https://api.jpush.cn"); + + JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, config); + + Notification notification = Notification.newBuilder() + .addPlatformNotification(AndroidNotification.newBuilder() + .setAlert(ALERT) + .setTitle("Alert test") + .build()) + .build(); + + JsonObject callback = new JsonObject(); + callback.addProperty("url", "https://www.jiguagn.cn/callback"); + JsonObject params = new JsonObject(); + params.addProperty("name", "joe"); + params.addProperty("age", 26); + callback.add("params", params); + callback.addProperty("type", 3); + + PushPayload.Builder payloadBuilder = new PushPayload.Builder() + .setPlatform(Platform.all()) + .setAudience(Audience.all()) + .setNotification(notification) + .addCustom("callback", callback); + + try { + PushResult result = jpushClient.sendPush(payloadBuilder.build()); + LOG.info("Got result - " + result); + } catch (APIConnectionException e) { + LOG.error("Connection error. Should retry later. ", e); + } catch (APIRequestException e) { + LOG.error("Error response from JPush server. Should review and fix it. ", e); + LOG.info("HTTP Status: " + e.getStatus()); + LOG.info("Error Code: " + e.getErrorCode()); + LOG.info("Error Message: " + e.getErrorMessage()); + LOG.info("Msg ID: " + e.getMsgId()); + } + } + } diff --git a/pom.xml b/pom.xml index ff26a476..463c9c25 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,7 @@ cn.jpush.api jpush-client - 3.4.3-SNAPSHOT + 3.4.3 jar https://github.com/jpush/jpush-api-java-client JPush API Java Client diff --git a/src/main/java/cn/jpush/api/push/model/BatchPushResult.java b/src/main/java/cn/jpush/api/push/model/BatchPushResult.java index da762cad..8a4777cd 100644 --- a/src/main/java/cn/jpush/api/push/model/BatchPushResult.java +++ b/src/main/java/cn/jpush/api/push/model/BatchPushResult.java @@ -12,11 +12,6 @@ import java.util.List; import java.util.Map; -/** - * @author xudanxia - * @Desc - * @date 2019-08-22. - */ public class BatchPushResult extends BaseResult { private static final Type RESULT_TYPE = new TypeToken>() {}.getType(); 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 4b48818b..575a38b0 100644 --- a/src/main/java/cn/jpush/api/push/model/PushPayload.java +++ b/src/main/java/cn/jpush/api/push/model/PushPayload.java @@ -14,6 +14,9 @@ import cn.jpush.api.push.model.notification.Notification; import cn.jpush.api.push.model.notification.PlatformNotification; +import java.util.LinkedHashMap; +import java.util.Map; + /** * The object you should build for sending a push. * @@ -54,9 +57,10 @@ public class PushPayload implements PushModel { private SMS sms; private String cid; private String target; + protected Map custom; private PushPayload(Platform platform, Audience audience, - Notification notification, Message message, Options options, SMS sms, String cid, String target) { + Notification notification, Message message, Options options, SMS sms, String cid, String target, Map custom) { this.platform = platform; this.audience = audience; this.notification = notification; @@ -65,6 +69,7 @@ private PushPayload(Platform platform, Audience audience, this.sms = sms; this.cid = cid; this.target = target; + this.custom = custom; } public PushPayload setCid(String cid) { @@ -192,6 +197,11 @@ public JsonElement toJSON() { if (null != target) { json.addProperty(TARGET, target); } + if (null != custom) { + for (Map.Entry entry : custom.entrySet()) { + json.add(entry.getKey(), entry.getValue()); + } + } return json; } @@ -252,6 +262,11 @@ public static class Builder { private SMS sms = null; private String cid; private String target; + private Map custom; + + public Builder() { + this.custom = new LinkedHashMap<>(); + } public Builder setTarget(String target) { this.target = target; @@ -293,6 +308,11 @@ public Builder setCid(String cid) { return this; } + public Builder addCustom(String field, JsonObject jsonObject) { + this.custom.put(field, jsonObject); + return this; + } + public PushPayload build() { if (StringUtils.isTrimedEmpty(target)) { @@ -312,7 +332,7 @@ public PushPayload build() { options = Options.sendno(); } - return new PushPayload(platform, audience, notification, message, options, sms, cid, target); + return new PushPayload(platform, audience, notification, message, options, sms, cid, target, custom); } } } diff --git a/src/main/java/cn/jpush/api/report/MessageDetailResult.java b/src/main/java/cn/jpush/api/report/MessageDetailResult.java index f8065c18..df06f65e 100644 --- a/src/main/java/cn/jpush/api/report/MessageDetailResult.java +++ b/src/main/java/cn/jpush/api/report/MessageDetailResult.java @@ -11,11 +11,6 @@ import java.util.ArrayList; import java.util.List; -/** - * @author xdx - * @Desc - * @date 2019-07-29. - */ public class MessageDetailResult extends BaseResult { private static final Type RECEIVED_TYPE = new TypeToken>() {}.getType();