Skip to content

Commit f1eecfa

Browse files
author
许丹侠
committed
支持自定义参数
1 parent c2e1fc9 commit f1eecfa

File tree

6 files changed

+175
-22
lines changed

6 files changed

+175
-22
lines changed

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)