Skip to content

Commit

Permalink
Merge pull request #154 from xdx54321/master
Browse files Browse the repository at this point in the history
支持最外层自定义参数
  • Loading branch information
xdx54321 authored Dec 6, 2019
2 parents ce2db2c + 1bbb34d commit 83d22a5
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 13 deletions.
48 changes: 48 additions & 0 deletions example/main/java/cn/jpush/api/examples/PushExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class PushExample {

public static void main(String[] args) {

// 回调参数可参考下面方法
testSendPushWithCustom();
testSendPushWithCustomField();
// testBatchSend();
testSendPushWithCustomConfig();
Expand Down Expand Up @@ -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());
}
}

}

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.3-SNAPSHOT</version>
<version>3.4.3</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/cn/jpush/api/push/model/BatchPushResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map<String, PushResult>>() {}.getType();
Expand Down
24 changes: 22 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 @@ -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.
*
Expand Down Expand Up @@ -54,9 +57,10 @@ public class PushPayload implements PushModel {
private SMS sms;
private String cid;
private String target;
protected Map<String, JsonObject> 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<String, JsonObject> custom) {
this.platform = platform;
this.audience = audience;
this.notification = notification;
Expand All @@ -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) {
Expand Down Expand Up @@ -192,6 +197,11 @@ public JsonElement toJSON() {
if (null != target) {
json.addProperty(TARGET, target);
}
if (null != custom) {
for (Map.Entry<String, JsonObject> entry : custom.entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
}

return json;
}
Expand Down Expand Up @@ -252,6 +262,11 @@ public static class Builder {
private SMS sms = null;
private String cid;
private String target;
private Map<String, JsonObject> custom;

public Builder() {
this.custom = new LinkedHashMap<>();
}

public Builder setTarget(String target) {
this.target = target;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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);
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions src/main/java/cn/jpush/api/report/MessageDetailResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<MessageDetailResult.Received>>() {}.getType();
Expand Down

0 comments on commit 83d22a5

Please sign in to comment.