11package cn .jpush .api .push .model ;
22
3+ import java .util .HashMap ;
4+ import java .util .Map ;
5+
36import com .google .gson .JsonElement ;
7+ import com .google .gson .JsonNull ;
48import com .google .gson .JsonObject ;
9+ import com .google .gson .JsonPrimitive ;
510
611import cn .jiguang .common .utils .Preconditions ;
7- import cn .jiguang .common .utils .StringUtils ;
812
913public class SMS implements PushModel {
1014
1115 private final String content ;
1216 private final int delay_time ;
17+ private final long temp_id ;
18+ private final Map <String , String > extras ;
19+ private final Map <String , Number > numberExtras ;
20+ private final Map <String , Boolean > booleanExtras ;
21+ private final Map <String , JsonObject > jsonExtras ;
1322
14- private SMS (String content , int delay_time ) {
23+ private SMS (String content , int delay_time , long temp_id ,
24+ Map <String , String > extras ,
25+ Map <String , Number > numberExtras ,
26+ Map <String , Boolean > booleanExtras ,
27+ Map <String , JsonObject > jsonExtras ) {
1528 this .content = content ;
1629 this .delay_time = delay_time ;
30+ this .temp_id = temp_id ;
31+ this .extras = extras ;
32+ this .numberExtras = numberExtras ;
33+ this .booleanExtras = booleanExtras ;
34+ this .jsonExtras = jsonExtras ;
35+
1736 }
1837
1938 public static Builder newBuilder () {
2039 return new Builder ();
2140 }
2241
2342 /**
43+ * This will be removed in the future. Please use content(long tempId, int delayTime) this constructor.
2444 * Create a SMS content with a delay time.
2545 * JPush will send a SMS if the message doesn't received within the delay time. If the delay time is 0, the SMS will be sent immediately.
2646 * Please note the delay time only works on Android.
@@ -30,24 +50,81 @@ public static Builder newBuilder() {
3050 * @param delayTime The seconds you want to delay, should be greater than or equal to 0.
3151 * @return SMS payload.
3252 */
53+ @ Deprecated
3354 public static SMS content (String content , int delayTime ) {
3455 return new Builder ()
3556 .setContent (content )
3657 .setDelayTime (delayTime )
3758 .build ();
3859 }
60+
61+ public static SMS content (long tempId , int delayTime ) {
62+ return new Builder ()
63+ .setTempID (tempId )
64+ .setDelayTime (delayTime )
65+ .build ();
66+ }
67+
3968
4069 @ Override
4170 public JsonElement toJSON () {
4271 JsonObject json = new JsonObject ();
43- json . addProperty ( "content" , content );
72+
4473 json .addProperty ("delay_time" , delay_time );
74+
75+ if (temp_id > 0 ) {
76+ json .addProperty ("temp_id" , temp_id );
77+ }
78+
79+ if (null != content ) {
80+ json .addProperty ("content" , content );
81+ }
82+
83+
84+ JsonObject extrasObject = null ;
85+ if (null != extras || null != numberExtras || null != booleanExtras ) {
86+ extrasObject = new JsonObject ();
87+ }
88+
89+ if (null != extras ) {
90+ for (String key : extras .keySet ()) {
91+ if (extras .get (key ) != null ) {
92+ extrasObject .add (key , new JsonPrimitive (extras .get (key )));
93+ } else {
94+ extrasObject .add (key , JsonNull .INSTANCE );
95+ }
96+ }
97+ }
98+ if (null != numberExtras ) {
99+ for (String key : numberExtras .keySet ()) {
100+ extrasObject .add (key , new JsonPrimitive (numberExtras .get (key )));
101+ }
102+ }
103+ if (null != booleanExtras ) {
104+ for (String key : booleanExtras .keySet ()) {
105+ extrasObject .add (key , new JsonPrimitive (booleanExtras .get (key )));
106+ }
107+ }
108+ if (null != jsonExtras ) {
109+ for (String key : jsonExtras .keySet ()) {
110+ extrasObject .add (key , jsonExtras .get (key ));
111+ }
112+ }
113+
114+ if (null != extras || null != numberExtras || null != booleanExtras ) {
115+ json .add ("temp_para" , extrasObject );
116+ }
45117 return json ;
46118 }
47119
48120 public static class Builder {
49121 private String content ;
50122 private int delay_time ;
123+ private long temp_id ;
124+ private Map <String , String > extrasBuilder ;
125+ private Map <String , Number > numberExtrasBuilder ;
126+ private Map <String , Boolean > booleanExtrasBuilder ;
127+ protected Map <String , JsonObject > jsonExtrasBuilder ;
51128
52129 public Builder setContent (String content ) {
53130 this .content = content ;
@@ -58,12 +135,64 @@ public Builder setDelayTime(int delayTime) {
58135 this .delay_time = delayTime ;
59136 return this ;
60137 }
138+
139+ public Builder setTempID (long tempID ) {
140+ this .temp_id = tempID ;
141+ return this ;
142+ }
143+
144+ public Builder addPara (String key , String value ) {
145+ Preconditions .checkArgument (! (null == key || null == value ), "Key/Value should not be null." );
146+ if (null == extrasBuilder ) {
147+ extrasBuilder = new HashMap <String , String >();
148+ }
149+ extrasBuilder .put (key , value );
150+ return this ;
151+ }
152+
153+ public Builder addParas (Map <String , String > extras ) {
154+ Preconditions .checkArgument (! (null == extras ), "extras should not be null." );
155+ if (null == extrasBuilder ) {
156+ extrasBuilder = new HashMap <String , String >();
157+ }
158+ for (String key : extras .keySet ()) {
159+ extrasBuilder .put (key , extras .get (key ));
160+ }
161+ return this ;
162+ }
163+
164+ public Builder addPara (String key , Number value ) {
165+ Preconditions .checkArgument (! (null == key || null == value ), "Key/Value should not be null." );
166+ if (null == numberExtrasBuilder ) {
167+ numberExtrasBuilder = new HashMap <String , Number >();
168+ }
169+ numberExtrasBuilder .put (key , value );
170+ return this ;
171+ }
172+
173+ public Builder addPara (String key , Boolean value ) {
174+ Preconditions .checkArgument (! (null == key || null == value ), "Key/Value should not be null." );
175+ if (null == booleanExtrasBuilder ) {
176+ booleanExtrasBuilder = new HashMap <String , Boolean >();
177+ }
178+ booleanExtrasBuilder .put (key , value );
179+ return this ;
180+ }
181+
182+ public Builder addPara (String key , JsonObject value ) {
183+ Preconditions .checkArgument (! (null == key || null == value ), "Key/Value should not be null." );
184+ if (null == jsonExtrasBuilder ) {
185+ jsonExtrasBuilder = new HashMap <String , JsonObject >();
186+ }
187+ jsonExtrasBuilder .put (key , value );
188+ return this ;
189+ }
61190
62191 public SMS build () {
63- Preconditions .checkArgument (StringUtils .isNotEmpty (content ), "The content must not be empty." );
64192 Preconditions .checkArgument (delay_time >= 0 , "The delay time must be greater than or equal to 0" );
65193
66- return new SMS (content , delay_time );
194+ return new SMS (content , delay_time , temp_id ,
195+ extrasBuilder , numberExtrasBuilder , booleanExtrasBuilder ,jsonExtrasBuilder );
67196 }
68197
69198 }
0 commit comments