Skip to content

Commit d733e71

Browse files
author
Javen
committed
message to support number/boolean extra.
1 parent a879164 commit d733e71

File tree

8 files changed

+206
-25
lines changed

8 files changed

+206
-25
lines changed

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

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

3-
import java.util.Map;
4-
53
import com.google.common.base.Preconditions;
64
import com.google.common.collect.ImmutableMap;
7-
import com.google.gson.Gson;
85
import com.google.gson.JsonElement;
96
import com.google.gson.JsonObject;
107
import com.google.gson.JsonPrimitive;
@@ -19,12 +16,19 @@ public class Message implements PushModel {
1916
private final String msgContent;
2017
private final String contentType;
2118
private final ImmutableMap<String, String> extras;
19+
private final ImmutableMap<String, Number> numberExtras;
20+
private final ImmutableMap<String, Boolean> booleanExtras;
2221

23-
private Message(String title, String msgContent, String contentType, ImmutableMap<String, String> extras) {
22+
private Message(String title, String msgContent, String contentType,
23+
ImmutableMap<String, String> extras,
24+
ImmutableMap<String, Number> numberExtras,
25+
ImmutableMap<String, Boolean> booleanExtras) {
2426
this.title = title;
2527
this.msgContent = msgContent;
2628
this.contentType = contentType;
2729
this.extras = extras;
30+
this.numberExtras = numberExtras;
31+
this.booleanExtras = booleanExtras;
2832
}
2933

3034
public static Builder newBuilder() {
@@ -47,11 +51,32 @@ public JsonElement toJSON() {
4751
if (null != contentType) {
4852
json.add(CONTENT_TYPE, new JsonPrimitive(contentType));
4953
}
54+
55+
JsonObject extrasObject = null;
56+
if (null != extras || null != numberExtras || null != booleanExtras) {
57+
extrasObject = new JsonObject();
58+
}
59+
5060
if (null != extras) {
51-
Gson gson = new Gson();
52-
JsonElement extrasObject = gson.toJsonTree(extras);
61+
for (String key : extras.keySet()) {
62+
extrasObject.add(key, new JsonPrimitive(extras.get(key)));
63+
}
64+
}
65+
if (null != numberExtras) {
66+
for (String key : numberExtras.keySet()) {
67+
extrasObject.add(key, new JsonPrimitive(numberExtras.get(key)));
68+
}
69+
}
70+
if (null != booleanExtras) {
71+
for (String key : booleanExtras.keySet()) {
72+
extrasObject.add(key, new JsonPrimitive(booleanExtras.get(key)));
73+
}
74+
}
75+
76+
if (null != extras || null != numberExtras || null != booleanExtras) {
5377
json.add(EXTRAS, extrasObject);
5478
}
79+
5580
return json;
5681
}
5782

@@ -60,6 +85,8 @@ public static class Builder {
6085
private String msgContent;
6186
private String contentType;
6287
private ImmutableMap.Builder<String, String> extrasBuilder;
88+
private ImmutableMap.Builder<String, Number> numberExtrasBuilder;
89+
private ImmutableMap.Builder<String, Boolean> booleanExtrasBuilder;
6390

6491
public Builder setTitle(String title) {
6592
this.title = title;
@@ -76,28 +103,40 @@ public Builder setContentType(String contentType) {
76103
return this;
77104
}
78105

79-
public Builder addExtras(Map<String, String> extras) {
106+
public Builder addExtra(String key, String value) {
107+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
80108
if (null == extrasBuilder) {
81109
extrasBuilder = ImmutableMap.builder();
82110
}
83-
extrasBuilder.putAll(extras);
111+
extrasBuilder.put(key, value);
84112
return this;
85113
}
86114

87-
public Builder addExtra(String key, String value) {
115+
public Builder addExtra(String key, Number value) {
88116
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
89-
if (null == extrasBuilder) {
90-
extrasBuilder = ImmutableMap.builder();
117+
if (null == numberExtrasBuilder) {
118+
numberExtrasBuilder = ImmutableMap.builder();
91119
}
92-
extrasBuilder.put(key, value);
120+
numberExtrasBuilder.put(key, value);
121+
return this;
122+
}
123+
124+
public Builder addExtra(String key, Boolean value) {
125+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
126+
if (null == booleanExtrasBuilder) {
127+
booleanExtrasBuilder = ImmutableMap.builder();
128+
}
129+
booleanExtrasBuilder.put(key, value);
93130
return this;
94131
}
95132

96133
public Message build() {
97134
Preconditions.checkArgument(! (null == msgContent),
98135
"msgConent should be set");
99136
return new Message(title, msgContent, contentType,
100-
(null == extrasBuilder) ? null : extrasBuilder.build());
137+
(null == extrasBuilder) ? null : extrasBuilder.build(),
138+
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
139+
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());
101140
}
102141
}
103142
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.push.model.notification;
22

3+
import com.google.common.base.Preconditions;
34
import com.google.common.collect.ImmutableMap;
45
import com.google.gson.JsonElement;
56
import com.google.gson.JsonObject;
@@ -73,6 +74,7 @@ public Builder setAlert(String alert) {
7374
}
7475

7576
public Builder addExtra(String key, String value) {
77+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
7678
if (null == extrasBuilder) {
7779
extrasBuilder = ImmutableMap.builder();
7880
}
@@ -81,6 +83,7 @@ public Builder addExtra(String key, String value) {
8183
}
8284

8385
public Builder addExtra(String key, Number value) {
86+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
8487
if (null == numberExtrasBuilder) {
8588
numberExtrasBuilder = ImmutableMap.builder();
8689
}
@@ -89,6 +92,7 @@ public Builder addExtra(String key, Number value) {
8992
}
9093

9194
public Builder addExtra(String key, Boolean value) {
95+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
9296
if (null == booleanExtrasBuilder) {
9397
booleanExtrasBuilder = ImmutableMap.builder();
9498
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.push.model.notification;
22

3+
import com.google.common.base.Preconditions;
34
import com.google.common.collect.ImmutableMap;
45
import com.google.gson.JsonElement;
56
import com.google.gson.JsonObject;
@@ -100,6 +101,7 @@ public Builder setAlert(String alert) {
100101
}
101102

102103
public Builder addExtra(String key, String value) {
104+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
103105
if (null == extrasBuilder) {
104106
extrasBuilder = ImmutableMap.builder();
105107
}
@@ -108,6 +110,7 @@ public Builder addExtra(String key, String value) {
108110
}
109111

110112
public Builder addExtra(String key, Number value) {
113+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
111114
if (null == numberExtrasBuilder) {
112115
numberExtrasBuilder = ImmutableMap.builder();
113116
}
@@ -116,6 +119,7 @@ public Builder addExtra(String key, Number value) {
116119
}
117120

118121
public Builder addExtra(String key, Boolean value) {
122+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
119123
if (null == booleanExtrasBuilder) {
120124
booleanExtrasBuilder = ImmutableMap.builder();
121125
}

src/cn/jpush/api/push/model/notification/WinphoneNotification.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package cn.jpush.api.push.model.notification;
22

3+
import com.google.common.base.Preconditions;
34
import com.google.common.collect.ImmutableMap;
45
import com.google.gson.JsonElement;
56
import com.google.gson.JsonObject;
@@ -73,6 +74,7 @@ public Builder setAlert(String alert) {
7374
}
7475

7576
public Builder addExtra(String key, String value) {
77+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
7678
if (null == extrasBuilder) {
7779
extrasBuilder = ImmutableMap.builder();
7880
}
@@ -81,6 +83,7 @@ public Builder addExtra(String key, String value) {
8183
}
8284

8385
public Builder addExtra(String key, Number value) {
86+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
8487
if (null == numberExtrasBuilder) {
8588
numberExtrasBuilder = ImmutableMap.builder();
8689
}
@@ -89,6 +92,7 @@ public Builder addExtra(String key, Number value) {
8992
}
9093

9194
public Builder addExtra(String key, Boolean value) {
95+
Preconditions.checkArgument(! (null == key || null == value), "Key/Value should not be null.");
9296
if (null == booleanExtrasBuilder) {
9397
booleanExtrasBuilder = ImmutableMap.builder();
9498
}

test/cn/jpush/api/push/ExceptionTests.java

Lines changed: 117 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -121,15 +121,13 @@ public void invalidParams_notification_android() {
121121
}
122122

123123
@Test
124-
public void invalidParams_notification_android_empty() {
124+
public void invalidParams_notification_ios() {
125125
JsonObject payload = new JsonObject();
126126
payload.add("platform", Platform.all().toJSON());
127127
payload.add("audience", Audience.all().toJSON());
128128

129129
JsonObject notification = new JsonObject();
130-
JsonObject android = new JsonObject();
131-
132-
notification.add("android", android);
130+
notification.add("ios", new JsonPrimitive(ALERT));
133131
payload.add("notification", notification);
134132

135133
System.out.println("json string: " + payload.toString());
@@ -139,16 +137,13 @@ public void invalidParams_notification_android_empty() {
139137
}
140138

141139
@Test
142-
public void invalidParams_notification_android_noalert() {
140+
public void invalidParams_notification_winphone() {
143141
JsonObject payload = new JsonObject();
144142
payload.add("platform", Platform.all().toJSON());
145143
payload.add("audience", Audience.all().toJSON());
146144

147145
JsonObject notification = new JsonObject();
148-
JsonObject android = new JsonObject();
149-
android.add("title", new JsonPrimitive("title"));
150-
151-
notification.add("android", android);
146+
notification.add("winphone", new JsonPrimitive(ALERT));
152147
payload.add("notification", notification);
153148

154149
System.out.println("json string: " + payload.toString());
@@ -165,7 +160,7 @@ public void invalidParams_notification_android_builderidNotNumber() {
165160

166161
JsonObject notification = new JsonObject();
167162
JsonObject android = new JsonObject();
168-
android.add("alert", new JsonPrimitive(11111));
163+
android.add("builder_id", new JsonPrimitive("builder_id_string"));
169164

170165
notification.add("android", android);
171166
payload.add("notification", notification);
@@ -212,6 +207,118 @@ public void lackOfParams_messageAndNotificaiton() {
212207
assertEquals(LACK_OF_PARAMS, result.getErrorCode());
213208
}
214209

210+
@Test
211+
public void lackOfParams_notification_android_empty() {
212+
JsonObject payload = new JsonObject();
213+
payload.add("platform", Platform.all().toJSON());
214+
payload.add("audience", Audience.all().toJSON());
215+
216+
JsonObject notification = new JsonObject();
217+
JsonObject android = new JsonObject();
218+
219+
notification.add("android", android);
220+
payload.add("notification", notification);
221+
222+
System.out.println("json string: " + payload.toString());
223+
224+
PushResult result = _client.sendPush(payload.toString());
225+
assertEquals(INVALID_PARAMS, result.getErrorCode());
226+
}
227+
228+
@Test
229+
public void lackOfParams_notification_ios_empty() {
230+
JsonObject payload = new JsonObject();
231+
payload.add("platform", Platform.all().toJSON());
232+
payload.add("audience", Audience.all().toJSON());
233+
234+
JsonObject notification = new JsonObject();
235+
JsonObject ios = new JsonObject();
236+
237+
notification.add("ios", ios);
238+
payload.add("notification", notification);
239+
240+
System.out.println("json string: " + payload.toString());
241+
242+
PushResult result = _client.sendPush(payload.toString());
243+
assertEquals(INVALID_PARAMS, result.getErrorCode());
244+
}
245+
246+
@Test
247+
public void lackOfParams_notification_winphone_empty() {
248+
JsonObject payload = new JsonObject();
249+
payload.add("platform", Platform.all().toJSON());
250+
payload.add("audience", Audience.all().toJSON());
251+
252+
JsonObject notification = new JsonObject();
253+
JsonObject winphone = new JsonObject();
254+
255+
notification.add("winphone", winphone);
256+
payload.add("notification", notification);
257+
258+
System.out.println("json string: " + payload.toString());
259+
260+
PushResult result = _client.sendPush(payload.toString());
261+
assertEquals(INVALID_PARAMS, result.getErrorCode());
262+
}
263+
264+
@Test
265+
public void lackOfParams_notification_android_noalert() {
266+
JsonObject payload = new JsonObject();
267+
payload.add("platform", Platform.all().toJSON());
268+
payload.add("audience", Audience.all().toJSON());
269+
270+
JsonObject notification = new JsonObject();
271+
JsonObject android = new JsonObject();
272+
android.add("title", new JsonPrimitive("title"));
273+
274+
notification.add("android", android);
275+
payload.add("notification", notification);
276+
277+
System.out.println("json string: " + payload.toString());
278+
279+
PushResult result = _client.sendPush(payload.toString());
280+
assertEquals(INVALID_PARAMS, result.getErrorCode());
281+
}
282+
283+
@Test
284+
public void lackOfParams_notification_ios_noalert() {
285+
JsonObject payload = new JsonObject();
286+
payload.add("platform", Platform.all().toJSON());
287+
payload.add("audience", Audience.all().toJSON());
288+
289+
JsonObject notification = new JsonObject();
290+
JsonObject ios = new JsonObject();
291+
ios.add("badge", new JsonPrimitive(11));
292+
293+
notification.add("ios", ios);
294+
payload.add("notification", notification);
295+
296+
System.out.println("json string: " + payload.toString());
297+
298+
PushResult result = _client.sendPush(payload.toString());
299+
assertEquals(INVALID_PARAMS, result.getErrorCode());
300+
}
301+
302+
@Test
303+
public void lackOfParams_notification_winphone_noalert() {
304+
JsonObject payload = new JsonObject();
305+
payload.add("platform", Platform.all().toJSON());
306+
payload.add("audience", Audience.all().toJSON());
307+
308+
JsonObject notification = new JsonObject();
309+
JsonObject winphone = new JsonObject();
310+
winphone.add("title", new JsonPrimitive("title"));
311+
312+
notification.add("winphone", winphone);
313+
payload.add("notification", notification);
314+
315+
System.out.println("json string: " + payload.toString());
316+
317+
PushResult result = _client.sendPush(payload.toString());
318+
assertEquals(INVALID_PARAMS, result.getErrorCode());
319+
}
320+
321+
215322

216323
}
217324

test/cn/jpush/api/push/MessageTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,8 @@ public void sendMessageContentAndExtras() {
5959
.setPlatform(Platform.all())
6060
.setMessage(Message.newBuilder()
6161
.addExtra("key1", "value1")
62-
.addExtra("key2", "value2")
62+
.addExtra("key2", 222)
63+
.addExtra("key3", Boolean.FALSE)
6364
.setMsgContent(MSG_CONTENT).build())
6465
.build();
6566
PushResult result = _client.sendPush(payload);

0 commit comments

Comments
 (0)