11package cn .jpush .api .push .model ;
22
33import java .util .HashMap ;
4+ import java .util .LinkedHashMap ;
45import java .util .Map ;
56
67import 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}
0 commit comments