Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev 191121 #152

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
97 changes: 97 additions & 0 deletions src/main/java/cn/jpush/api/push/model/Callback.java
Original file line number Diff line number Diff line change
@@ -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<String, String> params;

private Callback(String url, int type, Map<String, String> 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<String, String> 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<String, String> 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<String, String> 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);
}
}
}
16 changes: 14 additions & 2 deletions src/main/java/cn/jpush/api/push/model/PushPayload.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -192,6 +195,9 @@ public JsonElement toJSON() {
if (null != target) {
json.addProperty(TARGET, target);
}
if (null != callback) {
json.add(CALLBACK, callback.toJSON());
}

return json;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
}
}
Expand Down