diff --git a/src/main/java/cn/jpush/api/push/model/Callback.java b/src/main/java/cn/jpush/api/push/model/Callback.java new file mode 100644 index 00000000..00f41d6d --- /dev/null +++ b/src/main/java/cn/jpush/api/push/model/Callback.java @@ -0,0 +1,97 @@ +package cn.jpush.api.push.model; + +import cn.jiguang.common.utils.Preconditions; +import com.google.gson.JsonElement; +import com.google.gson.JsonObject; +import com.google.gson.JsonPrimitive; + +import java.util.LinkedHashMap; +import java.util.Map; + +/** + * 回执功能支持 + */ +public class Callback implements PushModel { + /** + * 回执地址 + */ + private String url; + /** + * 回执数据类型: 1-送达回执, 2-点击回执,3-送达和点击回执 + */ + private int type; + /** + * 自定义参数 + */ + private Map params; + + private Callback(String url, int type, Map params) { + this.url = url; + this.type = type; + this.params = params; + } + + public static Builder newBuilder() { + return new Builder(); + } + + @Override + public JsonElement toJSON() { + JsonObject json = new JsonObject(); + if (type > 0) { + json.add("type", new JsonPrimitive(type)); + } + if (null != url) { + json.add("url", new JsonPrimitive(url)); + } + + if (null != params && params.size() > 0) { + JsonObject paramObj = new JsonObject(); + for (Map.Entry entry : params.entrySet()) { + paramObj.add(entry.getKey(), new JsonPrimitive(entry.getValue())); + } + json.add("params", paramObj); + } + + return json; + } + + + public static class Builder { + private String url; + private int type; + private Map params; + + public Callback.Builder setUrl(String url) { + this.url = url; + return this; + } + + public Callback.Builder setType(int type) { + this.type = type; + return this; + } + + public Callback.Builder addParam(String key, String value) { + Preconditions.checkArgument(!(null == key), "Key should not be null."); + if (null == params) { + params = new LinkedHashMap<>(); + } + this.params.put(key, value); + return this; + } + + public Callback.Builder addParams(Map params) { + if (null == this.params) { + this.params = new LinkedHashMap<>(); + } + this.params.putAll(params); + return this; + } + + public Callback build() { + Preconditions.checkArgument(type >= 0, "type should be greater than 0."); + return new Callback(url, type, params); + } + } +} 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..9aec7b27 100644 --- a/src/main/java/cn/jpush/api/push/model/PushPayload.java +++ b/src/main/java/cn/jpush/api/push/model/PushPayload.java @@ -33,6 +33,7 @@ public class PushPayload implements PushModel { private static final String SMS = "sms_message"; private static final String CID = "cid"; private static final String TARGET = "target"; + private static final String CALLBACK = "callback"; /** * Definition acording to JPush Docs @@ -54,9 +55,10 @@ public class PushPayload implements PushModel { private SMS sms; private String cid; private String target; + private Callback callback; 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, Callback callback) { this.platform = platform; this.audience = audience; this.notification = notification; @@ -65,6 +67,7 @@ private PushPayload(Platform platform, Audience audience, this.sms = sms; this.cid = cid; this.target = target; + this.callback = callback; } public PushPayload setCid(String cid) { @@ -192,6 +195,9 @@ public JsonElement toJSON() { if (null != target) { json.addProperty(TARGET, target); } + if (null != callback) { + json.add(CALLBACK, callback.toJSON()); + } return json; } @@ -252,6 +258,7 @@ public static class Builder { private SMS sms = null; private String cid; private String target; + private Callback callback; public Builder setTarget(String target) { this.target = target; @@ -293,6 +300,11 @@ public Builder setCid(String cid) { return this; } + public Builder setCallback(Callback callback) { + this.callback = callback; + return this; + } + public PushPayload build() { if (StringUtils.isTrimedEmpty(target)) { @@ -312,7 +324,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, callback); } } }