Skip to content

Commit ce2db2c

Browse files
authored
Merge pull request #146 from xdx54321/master
支持自定义参数
2 parents 93e487c + ff4dfe9 commit ce2db2c

File tree

10 files changed

+249
-28
lines changed

10 files changed

+249
-28
lines changed

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ public class PushExample {
3131
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
3232

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

@@ -46,8 +46,9 @@ public class PushExample {
4646

4747
public static void main(String[] args) {
4848

49-
testBatchSend();
50-
// testSendPushWithCustomConfig();
49+
testSendPushWithCustomField();
50+
// testBatchSend();
51+
testSendPushWithCustomConfig();
5152
// testSendIosAlert();
5253
// testSendPush();
5354
// testGetCidList();
@@ -586,5 +587,48 @@ public static void testBatchSend() {
586587
}
587588
}
588589

590+
/**
591+
* 自定义发送参数名称, 华为客户可参考该方法
592+
*/
593+
public static void testSendPushWithCustomField() {
594+
595+
ClientConfig config = ClientConfig.getInstance();
596+
// Setup the custom hostname
597+
config.setPushHostName("https://api.jpush.cn");
598+
599+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, config);
600+
601+
Notification notification = Notification.newBuilder()
602+
.addPlatformNotification(AndroidNotification.newBuilder()
603+
.setAlert(ALERT)
604+
.setTitle("Alert test")
605+
.setLargeIcon("http://www.jiguang.cn/largeIcon.jpg")
606+
.addCustom("uri_activity", "uri_activity")
607+
.addCustom("uri_flag", "uri_flag")
608+
.addCustom("uri_action", "uri_action")
609+
.build())
610+
.build();
611+
612+
PushPayload.Builder payloadBuilder = new PushPayload.Builder()
613+
.setPlatform(Platform.all())
614+
.setAudience(Audience.all())
615+
.setNotification(notification);
616+
617+
try {
618+
PushResult result = jpushClient.sendPush(payloadBuilder.build());
619+
LOG.info("Got result - " + result);
620+
621+
} catch (APIConnectionException e) {
622+
LOG.error("Connection error. Should retry later. ", e);
623+
624+
} catch (APIRequestException e) {
625+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
626+
LOG.info("HTTP Status: " + e.getStatus());
627+
LOG.info("Error Code: " + e.getErrorCode());
628+
LOG.info("Error Message: " + e.getErrorMessage());
629+
LOG.info("Msg ID: " + e.getMsgId());
630+
}
631+
}
632+
589633
}
590634

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.3.14-SNAPSHOT</version>
6+
<version>3.4.3-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>
@@ -35,7 +35,7 @@
3535
<url>https://github.com/jpush/jpush-api-java-client</url>
3636
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
3737
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
38-
<tag>v3.3.5</tag>
38+
<tag>v3.3.12</tag>
3939
</scm>
4040

4141
<dependencies>

src/main/java/cn/jpush/api/push/model/Message.java

Lines changed: 51 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cn.jpush.api.push.model;
22

33
import java.util.HashMap;
4+
import java.util.LinkedHashMap;
45
import java.util.Map;
56

67
import com.google.gson.JsonElement;
@@ -23,19 +24,22 @@ public class Message implements PushModel {
2324
private final Map<String, Number> numberExtras;
2425
private final Map<String, Boolean> booleanExtras;
2526
private final Map<String, JsonObject> jsonExtras;
26-
27+
private final Map<String, JsonPrimitive> customData;
28+
2729
private Message(String title, String msgContent, String contentType,
2830
Map<String, String> extras,
2931
Map<String, Number> numberExtras,
3032
Map<String, Boolean> booleanExtras,
31-
Map<String, JsonObject> jsonExtras) {
33+
Map<String, JsonObject> jsonExtras,
34+
Map<String, JsonPrimitive> customData) {
3235
this.title = title;
3336
this.msgContent = msgContent;
3437
this.contentType = contentType;
3538
this.extras = extras;
3639
this.numberExtras = numberExtras;
3740
this.booleanExtras = booleanExtras;
3841
this.jsonExtras = jsonExtras;
42+
this.customData = customData;
3943
}
4044

4145
public static Builder newBuilder() {
@@ -92,6 +96,12 @@ public JsonElement toJSON() {
9296
if (null != extras || null != numberExtras || null != booleanExtras) {
9397
json.add(EXTRAS, extrasObject);
9498
}
99+
100+
if (null != customData) {
101+
for (Map.Entry<String, JsonPrimitive> entry : customData.entrySet()) {
102+
json.add(entry.getKey(), entry.getValue());
103+
}
104+
}
95105

96106
return json;
97107
}
@@ -104,6 +114,7 @@ public static class Builder {
104114
private Map<String, Number> numberExtrasBuilder;
105115
private Map<String, Boolean> booleanExtrasBuilder;
106116
protected Map<String, JsonObject> jsonExtrasBuilder;
117+
private Map<String, JsonPrimitive> customData;
107118

108119
public Builder setTitle(String title) {
109120
this.title = title;
@@ -166,12 +177,49 @@ public Builder addExtra(String key, JsonObject value) {
166177
jsonExtrasBuilder.put(key, value);
167178
return this;
168179
}
180+
181+
public Builder addCustom(Map<String, String> extras) {
182+
if (customData == null) {
183+
customData = new LinkedHashMap<>();
184+
}
185+
for (Map.Entry<String, String> entry : extras.entrySet()) {
186+
customData.put(entry.getKey(), new JsonPrimitive(entry.getValue()));
187+
}
188+
return this;
189+
}
190+
191+
public Builder addCustom(String key, Number value) {
192+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
193+
if (customData == null) {
194+
customData = new LinkedHashMap<>();
195+
}
196+
customData.put(key, new JsonPrimitive(value));
197+
return this;
198+
}
199+
200+
public Builder addCustom(String key, String value) {
201+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
202+
if (customData == null) {
203+
customData = new LinkedHashMap<>();
204+
}
205+
customData.put(key, new JsonPrimitive(value));
206+
return this;
207+
}
208+
209+
public Builder addCustom(String key, Boolean value) {
210+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
211+
if (customData == null) {
212+
customData = new LinkedHashMap<>();
213+
}
214+
customData.put(key, new JsonPrimitive(value));
215+
return this;
216+
}
169217

170218
public Message build() {
171219
Preconditions.checkArgument(! (null == msgContent),
172220
"msgContent should be set");
173221
return new Message(title, msgContent, contentType,
174-
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder);
222+
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder,jsonExtrasBuilder, customData);
175223
}
176224
}
177225
}

src/main/java/cn/jpush/api/push/model/Options.java

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import cn.jiguang.common.ServiceHelper;
77
import cn.jiguang.common.utils.Preconditions;
88

9+
import java.util.LinkedHashMap;
910
import java.util.Map;
1011

1112
public class Options implements PushModel {
@@ -27,6 +28,8 @@ public class Options implements PushModel {
2728
// minutes
2829
private int bigPushDuration;
2930
private String apnsCollapseId;
31+
private final Map<String, JsonPrimitive> customData;
32+
3033

3134
/**
3235
* example
@@ -59,14 +62,16 @@ private Options(int sendno,
5962
boolean apnsProduction,
6063
int bigPushDuration,
6164
String apnsCollapseId,
62-
Map<String, Map<String, String>> thirdPartyChannel) {
65+
Map<String, Map<String, String>> thirdPartyChannel,
66+
Map<String, JsonPrimitive> customData) {
6367
this.sendno = sendno;
6468
this.overrideMsgId = overrideMsgId;
6569
this.timeToLive = timeToLive;
6670
this.apnsProduction = apnsProduction;
6771
this.bigPushDuration = bigPushDuration;
6872
this.apnsCollapseId = apnsCollapseId;
6973
this.thirdPartyChannel = thirdPartyChannel;
74+
this.customData = customData;
7075
}
7176

7277
public static Builder newBuilder() {
@@ -132,6 +137,12 @@ public JsonElement toJSON() {
132137
json.add(THIRD_PARTH_CHANNEl, partyChannel);
133138
}
134139

140+
if (null != customData) {
141+
for (Map.Entry<String, JsonPrimitive> entry : customData.entrySet()) {
142+
json.add(entry.getKey(), entry.getValue());
143+
}
144+
}
145+
135146
return json;
136147
}
137148

@@ -144,6 +155,7 @@ public static class Builder {
144155
private int bigPushDuration = 0;
145156
private String apnsCollapseId;
146157
private Map<String, Map<String, String>> thirdPartyChannel;
158+
private Map<String, JsonPrimitive> customData;
147159

148160
public Builder setSendno(int sendno) {
149161
this.sendno = sendno;
@@ -184,6 +196,43 @@ public Builder setThirdPartyChannel(Map<String, Map<String, String>> thirdPartyC
184196
return this;
185197
}
186198

199+
public Builder addCustom(Map<String, String> extras) {
200+
if (customData == null) {
201+
customData = new LinkedHashMap<>();
202+
}
203+
for (Map.Entry<String, String> entry : extras.entrySet()) {
204+
customData.put(entry.getKey(), new JsonPrimitive(entry.getValue()));
205+
}
206+
return this;
207+
}
208+
209+
public Builder addCustom(String key, Number value) {
210+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
211+
if (customData == null) {
212+
customData = new LinkedHashMap<>();
213+
}
214+
customData.put(key, new JsonPrimitive(value));
215+
return this;
216+
}
217+
218+
public Builder addCustom(String key, String value) {
219+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
220+
if (customData == null) {
221+
customData = new LinkedHashMap<>();
222+
}
223+
customData.put(key, new JsonPrimitive(value));
224+
return this;
225+
}
226+
227+
public Builder addCustom(String key, Boolean value) {
228+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
229+
if (customData == null) {
230+
customData = new LinkedHashMap<>();
231+
}
232+
customData.put(key, new JsonPrimitive(value));
233+
return this;
234+
}
235+
187236
public Options build() {
188237
Preconditions.checkArgument(sendno >= 0, "sendno should be greater than 0.");
189238
Preconditions.checkArgument(overrideMsgId >= 0, "override_msg_id should be greater than 0.");
@@ -194,7 +243,7 @@ public Options build() {
194243
sendno = ServiceHelper.generateSendno();
195244
}
196245

197-
return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId, thirdPartyChannel);
246+
return new Options(sendno, overrideMsgId, timeToLive, apnsProduction, bigPushDuration, apnsCollapseId, thirdPartyChannel, customData);
198247
}
199248
}
200249

src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import lombok.*;
77
import lombok.experimental.Accessors;
88

9+
import java.util.LinkedHashMap;
910
import java.util.Map;
1011

1112
public class AndroidNotification extends PlatformNotification {
@@ -55,8 +56,9 @@ private AndroidNotification(Object alert,
5556
Map<String, String> extras,
5657
Map<String, Number> numberExtras,
5758
Map<String, Boolean> booleanExtras,
58-
Map<String, JsonObject> jsonExtras) {
59-
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
59+
Map<String, JsonObject> jsonExtras,
60+
Map<String, JsonPrimitive> customData) {
61+
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
6062

6163
this.title = title;
6264
this.builderId = builderId;
@@ -80,7 +82,6 @@ public static AndroidNotification alert(String alert) {
8082
return newBuilder().setAlert(alert).build();
8183
}
8284

83-
8485
@Override
8586
public String getPlatform() {
8687
return NOTIFICATION_ANDROID;
@@ -268,7 +269,8 @@ public AndroidNotification build() {
268269
extrasBuilder,
269270
numberExtrasBuilder,
270271
booleanExtrasBuilder,
271-
jsonExtrasBuilder
272+
jsonExtrasBuilder,
273+
super.customData
272274
);
273275
}
274276
}

src/main/java/cn/jpush/api/push/model/notification/IosNotification.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,15 +55,15 @@ public class IosNotification extends PlatformNotification {
5555
private final boolean mutableContent;
5656
private final String threadId;
5757

58-
5958
private IosNotification(Object alert, Object sound, String badge,
6059
boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
6160
String category, boolean mutableContent,String threadId,
6261
Map<String, String> extras,
6362
Map<String, Number> numberExtras,
6463
Map<String, Boolean> booleanExtras,
65-
Map<String, JsonObject> jsonExtras) {
66-
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
64+
Map<String, JsonObject> jsonExtras,
65+
Map<String, JsonPrimitive> customData) {
66+
super(alert, extras, numberExtras, booleanExtras, jsonExtras, customData);
6767

6868
this.sound = sound;
6969
this.badge = badge;
@@ -139,6 +139,7 @@ public static class Builder extends PlatformNotification.Builder<IosNotification
139139
private boolean mutableContent;
140140
private String threadId;
141141

142+
@Override
142143
protected Builder getThis() {
143144
return this;
144145
}
@@ -203,6 +204,7 @@ public Builder setCategory(String category) {
203204
return this;
204205
}
205206

207+
@Override
206208
public Builder setAlert(Object alert) {
207209
this.alert = alert;
208210
return this;
@@ -222,7 +224,7 @@ public Builder setThreadId(String threadId) {
222224
public IosNotification build() {
223225
return new IosNotification(alert, sound, badge, contentAvailable,
224226
soundDisabled, badgeDisabled, category, mutableContent, threadId,
225-
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder);
227+
extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder, super.customData);
226228
}
227229
}
228230
}

0 commit comments

Comments
 (0)