Skip to content

Commit

Permalink
Merge pull request #146 from xdx54321/master
Browse files Browse the repository at this point in the history
支持自定义参数
  • Loading branch information
xdx54321 committed Sep 25, 2019
2 parents 93e487c + ff4dfe9 commit ce2db2c
Show file tree
Hide file tree
Showing 10 changed files with 249 additions and 28 deletions.
52 changes: 48 additions & 4 deletions example/main/java/cn/jpush/api/examples/PushExample.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ public class PushExample {
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);

// demo App defined in resources/jpush-api.conf
protected static final String APP_KEY = "7b4b94cca0d185d611e53cca";
protected static final String MASTER_SECRET = "860803cf613ed54aa3b941a8";
protected static final String APP_KEY = "e4ceeaf7a53ad745dd4728f2";
protected static final String MASTER_SECRET = "1582b986adeaf48ceec1e354";
protected static final String GROUP_PUSH_KEY = "2c88a01e073a0fe4fc7b167c";
protected static final String GROUP_MASTER_SECRET = "b11314807507e2bcfdeebe2e";

Expand All @@ -46,8 +46,9 @@ public class PushExample {

public static void main(String[] args) {

testBatchSend();
// testSendPushWithCustomConfig();
testSendPushWithCustomField();
// testBatchSend();
testSendPushWithCustomConfig();
// testSendIosAlert();
// testSendPush();
// testGetCidList();
Expand Down Expand Up @@ -586,5 +587,48 @@ public static void testBatchSend() {
}
}

/**
* 自定义发送参数名称, 华为客户可参考该方法
*/
public static void testSendPushWithCustomField() {

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")
.setLargeIcon("http://www.jiguang.cn/largeIcon.jpg")
.addCustom("uri_activity", "uri_activity")
.addCustom("uri_flag", "uri_flag")
.addCustom("uri_action", "uri_action")
.build())
.build();

PushPayload.Builder payloadBuilder = new PushPayload.Builder()
.setPlatform(Platform.all())
.setAudience(Audience.all())
.setNotification(notification);

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());
}
}

}

4 changes: 2 additions & 2 deletions 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.3.14-SNAPSHOT</version>
<version>3.4.3-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down Expand Up @@ -35,7 +35,7 @@
<url>https://github.com/jpush/jpush-api-java-client</url>
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
<tag>v3.3.5</tag>
<tag>v3.3.12</tag>
</scm>

<dependencies>
Expand Down
54 changes: 51 additions & 3 deletions src/main/java/cn/jpush/api/push/model/Message.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.jpush.api.push.model;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

import com.google.gson.JsonElement;
Expand All @@ -23,19 +24,22 @@ public class Message implements PushModel {
private final Map<String, Number> numberExtras;
private final Map<String, Boolean> booleanExtras;
private final Map<String, JsonObject> jsonExtras;

private final Map<String, JsonPrimitive> customData;

private Message(String title, String msgContent, String contentType,
Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras) {
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
this.title = title;
this.msgContent = msgContent;
this.contentType = contentType;
this.extras = extras;
this.numberExtras = numberExtras;
this.booleanExtras = booleanExtras;
this.jsonExtras = jsonExtras;
this.customData = customData;
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -92,6 +96,12 @@ public JsonElement toJSON() {
if (null != extras || null != numberExtras || null != booleanExtras) {
json.add(EXTRAS, extrasObject);
}

if (null != customData) {
for (Map.Entry<String, JsonPrimitive> entry : customData.entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
}

return json;
}
Expand All @@ -104,6 +114,7 @@ public static class Builder {
private Map<String, Number> numberExtrasBuilder;
private Map<String, Boolean> booleanExtrasBuilder;
protected Map<String, JsonObject> jsonExtrasBuilder;
private Map<String, JsonPrimitive> customData;

public Builder setTitle(String title) {
this.title = title;
Expand Down Expand Up @@ -166,12 +177,49 @@ public Builder addExtra(String key, JsonObject value) {
jsonExtrasBuilder.put(key, value);
return this;
}

public Builder addCustom(Map<String, String> extras) {
if (customData == null) {
customData = new LinkedHashMap<>();
}
for (Map.Entry<String, String> entry : extras.entrySet()) {
customData.put(entry.getKey(), new JsonPrimitive(entry.getValue()));
}
return this;
}

public Builder addCustom(String key, Number value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (customData == null) {
customData = new LinkedHashMap<>();
}
customData.put(key, new JsonPrimitive(value));
return this;
}

public Builder addCustom(String key, String value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (customData == null) {
customData = new LinkedHashMap<>();
}
customData.put(key, new JsonPrimitive(value));
return this;
}

public Builder addCustom(String key, Boolean value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (customData == null) {
customData = new LinkedHashMap<>();
}
customData.put(key, new JsonPrimitive(value));
return this;
}

public Message build() {
Preconditions.checkArgument(! (null == msgContent),
"msgContent should be set");
return new Message(title, msgContent, contentType,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder);
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder, customData);
}
}
}
53 changes: 51 additions & 2 deletions src/main/java/cn/jpush/api/push/model/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import cn.jiguang.common.ServiceHelper;
import cn.jiguang.common.utils.Preconditions;

import java.util.LinkedHashMap;
import java.util.Map;

public class Options implements PushModel {
Expand All @@ -27,6 +28,8 @@ public class Options implements PushModel {
// minutes
private int bigPushDuration;
private String apnsCollapseId;
private final Map<String, JsonPrimitive> customData;


/**
* example
Expand Down Expand Up @@ -59,14 +62,16 @@ private Options(int sendno,
boolean apnsProduction,
int bigPushDuration,
String apnsCollapseId,
Map<String, Map<String, String>> thirdPartyChannel) {
Map<String, Map<String, String>> thirdPartyChannel,
Map<String, JsonPrimitive> customData) {
this.sendno = sendno;
this.overrideMsgId = overrideMsgId;
this.timeToLive = timeToLive;
this.apnsProduction = apnsProduction;
this.bigPushDuration = bigPushDuration;
this.apnsCollapseId = apnsCollapseId;
this.thirdPartyChannel = thirdPartyChannel;
this.customData = customData;
}

public static Builder newBuilder() {
Expand Down Expand Up @@ -132,6 +137,12 @@ public JsonElement toJSON() {
json.add(THIRD_PARTH_CHANNEl, partyChannel);
}

if (null != customData) {
for (Map.Entry<String, JsonPrimitive> entry : customData.entrySet()) {
json.add(entry.getKey(), entry.getValue());
}
}

return json;
}

Expand All @@ -144,6 +155,7 @@ public static class Builder {
private int bigPushDuration = 0;
private String apnsCollapseId;
private Map<String, Map<String, String>> thirdPartyChannel;
private Map<String, JsonPrimitive> customData;

public Builder setSendno(int sendno) {
this.sendno = sendno;
Expand Down Expand Up @@ -184,6 +196,43 @@ public Builder setThirdPartyChannel(Map<String, Map<String, String>> thirdPartyC
return this;
}

public Builder addCustom(Map<String, String> extras) {
if (customData == null) {
customData = new LinkedHashMap<>();
}
for (Map.Entry<String, String> entry : extras.entrySet()) {
customData.put(entry.getKey(), new JsonPrimitive(entry.getValue()));
}
return this;
}

public Builder addCustom(String key, Number value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (customData == null) {
customData = new LinkedHashMap<>();
}
customData.put(key, new JsonPrimitive(value));
return this;
}

public Builder addCustom(String key, String value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (customData == null) {
customData = new LinkedHashMap<>();
}
customData.put(key, new JsonPrimitive(value));
return this;
}

public Builder addCustom(String key, Boolean value) {
Preconditions.checkArgument(! (null == key), "Key should not be null.");
if (customData == null) {
customData = new LinkedHashMap<>();
}
customData.put(key, new JsonPrimitive(value));
return this;
}

public Options build() {
Preconditions.checkArgument(sendno >= 0, "sendno should be greater than 0.");
Preconditions.checkArgument(overrideMsgId >= 0, "override_msg_id should be greater than 0.");
Expand All @@ -194,7 +243,7 @@ public Options build() {
sendno = ServiceHelper.generateSendno();
}

return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId, thirdPartyChannel);
return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId, thirdPartyChannel, customData);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.*;
import lombok.experimental.Accessors;

import java.util.LinkedHashMap;
import java.util.Map;

public class AndroidNotification extends PlatformNotification {
Expand Down Expand Up @@ -55,8 +56,9 @@ private AndroidNotification(Object alert,
Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);

this.title = title;
this.builderId = builderId;
Expand All @@ -80,7 +82,6 @@ public static AndroidNotification alert(String alert) {
return newBuilder().setAlert(alert).build();
}


@Override
public String getPlatform() {
return NOTIFICATION_ANDROID;
Expand Down Expand Up @@ -268,7 +269,8 @@ public AndroidNotification build() {
extrasBuilder,
numberExtrasBuilder,
booleanExtrasBuilder,
jsonExtrasBuilder
jsonExtrasBuilder,
super.customData
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,15 @@ public class IosNotification extends PlatformNotification {
private final boolean mutableContent;
private final String threadId;


private IosNotification(Object alert, Object sound, String badge,
boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
String category, boolean mutableContent,String threadId,
Map<String, String> extras,
Map<String, Number> numberExtras,
Map<String, Boolean> booleanExtras,
Map<String, JsonObject> jsonExtras) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
Map<String, JsonObject> jsonExtras,
Map<String, JsonPrimitive> customData) {
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);

this.sound = sound;
this.badge = badge;
Expand Down Expand Up @@ -139,6 +139,7 @@ public static class Builder extends PlatformNotification.Builder<IosNotification
private boolean mutableContent;
private String threadId;

@Override
protected Builder getThis() {
return this;
}
Expand Down Expand Up @@ -203,6 +204,7 @@ public Builder setCategory(String category) {
return this;
}

@Override
public Builder setAlert(Object alert) {
this.alert = alert;
return this;
Expand All @@ -222,7 +224,7 @@ public Builder setThreadId(String threadId) {
public IosNotification build() {
return new IosNotification(alert, sound, badge, contentAvailable,
soundDisabled, badgeDisabled, category, mutableContent, threadId,
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder);
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData);
}
}
}
Loading

0 comments on commit ce2db2c

Please sign in to comment.