Skip to content

Commit d864f78

Browse files
author
Javen
committed
Fix bug - cannot support json in extra value
1 parent 499540d commit d864f78

File tree

5 files changed

+112
-20
lines changed

5 files changed

+112
-20
lines changed

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ public class AndroidNotification extends PlatformNotification {
2020
private AndroidNotification(String alert, String title, int builderId,
2121
ImmutableMap<String, String> extras,
2222
ImmutableMap<String, Number> numberExtras,
23-
ImmutableMap<String, Boolean> booleanExtras) {
24-
super(alert, extras, numberExtras, booleanExtras);
23+
ImmutableMap<String, Boolean> booleanExtras,
24+
ImmutableMap<String, JsonObject> jsonExtras) {
25+
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
2526

2627
this.title = title;
2728
this.builderId = builderId;
@@ -129,11 +130,25 @@ public Builder addExtra(String key, Boolean value) {
129130
return this;
130131
}
131132

133+
public Builder addExtra(String key, JsonObject value) {
134+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
135+
if (null == value) {
136+
LOG.debug("Extra value is null, throw away it.");
137+
return this;
138+
}
139+
if (null == jsonExtrasBuilder) {
140+
jsonExtrasBuilder = ImmutableMap.builder();
141+
}
142+
jsonExtrasBuilder.put(key, value);
143+
return this;
144+
}
145+
132146
public AndroidNotification build() {
133147
return new AndroidNotification(alert, title, builderId,
134148
(null == extrasBuilder) ? null : extrasBuilder.build(),
135149
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
136-
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());
150+
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build(),
151+
(null == jsonExtrasBuilder) ? null : jsonExtrasBuilder.build());
137152
}
138153
}
139154
}

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

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,9 @@ private IosNotification(String alert, String sound, String badge,
5050
boolean contentAvailable, boolean soundDisabled, boolean badgeDisabled,
5151
ImmutableMap<String, String> extras,
5252
ImmutableMap<String, Number> numberExtras,
53-
ImmutableMap<String, Boolean> booleanExtras) {
54-
super(alert, extras, numberExtras, booleanExtras);
53+
ImmutableMap<String, Boolean> booleanExtras,
54+
ImmutableMap<String, JsonObject> jsonExtras) {
55+
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
5556

5657
this.sound = sound;
5758
this.badge = badge;
@@ -220,12 +221,27 @@ public Builder addExtra(String key, Boolean value) {
220221
return this;
221222
}
222223

224+
public Builder addExtra(String key, JsonObject value) {
225+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
226+
if (null == value) {
227+
LOG.debug("Extra value is null, throw away it.");
228+
return this;
229+
}
230+
if (null == jsonExtrasBuilder) {
231+
jsonExtrasBuilder = ImmutableMap.builder();
232+
}
233+
jsonExtrasBuilder.put(key, value);
234+
return this;
235+
}
236+
237+
223238
public IosNotification build() {
224239
return new IosNotification(alert, sound, badge, contentAvailable,
225240
soundDisabled, badgeDisabled,
226241
(null == extrasBuilder) ? null : extrasBuilder.build(),
227242
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
228-
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());
243+
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build(),
244+
(null == jsonExtrasBuilder) ? null : jsonExtrasBuilder.build());
229245
}
230246
}
231247
}

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

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,17 @@ public abstract class PlatformNotification implements PushModel {
2222
private final ImmutableMap<String, String> extras;
2323
private final ImmutableMap<String, Number> numberExtras;
2424
private final ImmutableMap<String, Boolean> booleanExtras;
25+
private final ImmutableMap<String, JsonObject> jsonExtras;
2526

2627
public PlatformNotification(String alert, ImmutableMap<String, String> extras,
27-
ImmutableMap<String, Number> numberExtras, ImmutableMap<String, Boolean> booleanExtras) {
28+
ImmutableMap<String, Number> numberExtras,
29+
ImmutableMap<String, Boolean> booleanExtras,
30+
ImmutableMap<String, JsonObject> jsonExtras) {
2831
this.alert = alert;
2932
this.extras = extras;
3033
this.numberExtras = numberExtras;
3134
this.booleanExtras = booleanExtras;
35+
this.jsonExtras = jsonExtras;
3236
}
3337

3438
@Override
@@ -40,7 +44,7 @@ public JsonElement toJSON() {
4044
}
4145

4246
JsonObject extrasObject = null;
43-
if (null != extras || null != numberExtras || null != booleanExtras) {
47+
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
4448
extrasObject = new JsonObject();
4549
}
4650

@@ -71,8 +75,17 @@ public JsonElement toJSON() {
7175
}
7276
}
7377
}
74-
75-
if (null != extras || null != numberExtras || null != booleanExtras) {
78+
if (null != jsonExtras) {
79+
JsonObject value = null;
80+
for (String key : jsonExtras.keySet()) {
81+
value = jsonExtras.get(key);
82+
if (null != value) {
83+
extrasObject.add(key, value);
84+
}
85+
}
86+
}
87+
88+
if (null != extras || null != numberExtras || null != booleanExtras || null != jsonExtras) {
7689
json.add(EXTRAS, extrasObject);
7790
}
7891

@@ -94,12 +107,14 @@ protected abstract static class Builder<T> {
94107
protected ImmutableMap.Builder<String, String> extrasBuilder;
95108
protected ImmutableMap.Builder<String, Number> numberExtrasBuilder;
96109
protected ImmutableMap.Builder<String, Boolean> booleanExtrasBuilder;
110+
protected ImmutableMap.Builder<String, JsonObject> jsonExtrasBuilder;
97111

98112
public abstract Builder<T> setAlert(String alert);
99113

100114
public abstract Builder<T> addExtra(String key, String value);
101115
public abstract Builder<T> addExtra(String key, Number value);
102116
public abstract Builder<T> addExtra(String key, Boolean value);
117+
public abstract Builder<T> addExtra(String key, JsonObject value);
103118
public abstract Builder<T> addExtras(Map<String, String> extras);
104119

105120
public abstract T build();

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

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import java.util.Map;
44

5-
import cn.jpush.api.push.model.notification.AndroidNotification.Builder;
6-
75
import com.google.common.base.Preconditions;
86
import com.google.common.collect.ImmutableMap;
97
import com.google.gson.JsonElement;
@@ -22,8 +20,9 @@ public class WinphoneNotification extends PlatformNotification {
2220
private WinphoneNotification(String alert, String title, String openPage,
2321
ImmutableMap<String, String> extras,
2422
ImmutableMap<String, Number> numberExtras,
25-
ImmutableMap<String, Boolean> booleanExtras) {
26-
super(alert, extras, numberExtras, booleanExtras);
23+
ImmutableMap<String, Boolean> booleanExtras,
24+
ImmutableMap<String, JsonObject> jsonExtras) {
25+
super(alert, extras, numberExtras, booleanExtras, jsonExtras);
2726

2827
this.title = title;
2928
this.openPage = openPage;
@@ -131,11 +130,27 @@ public Builder addExtra(String key, Boolean value) {
131130
return this;
132131
}
133132

133+
public Builder addExtra(String key, JsonObject value) {
134+
Preconditions.checkArgument(! (null == key), "Key should not be null.");
135+
if (null == value) {
136+
LOG.debug("Extra value is null, throw away it.");
137+
return this;
138+
}
139+
if (null == jsonExtrasBuilder) {
140+
jsonExtrasBuilder = ImmutableMap.builder();
141+
}
142+
jsonExtrasBuilder.put(key, value);
143+
return this;
144+
}
145+
146+
147+
134148
public WinphoneNotification build() {
135149
return new WinphoneNotification(alert, title, openPage,
136150
(null == extrasBuilder) ? null : extrasBuilder.build(),
137151
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
138-
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());
152+
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build(),
153+
(null == jsonExtrasBuilder) ? null : jsonExtrasBuilder.build());
139154
}
140155
}
141156
}

test/cn/jpush/api/push/model/notification/NotificationTest.java

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public void testNoAlert() {
3232
}
3333

3434
@Test
35-
public void testAlertAndroid() {
35+
public void testAlert_android() {
3636
Notification notification = Notification.newBuilder()
3737
.addPlatformNotification(AndroidNotification.alert("alert"))
3838
.build();
@@ -44,7 +44,7 @@ public void testAlertAndroid() {
4444
}
4545

4646
@Test
47-
public void testAlertIos() {
47+
public void testAlert_ios() {
4848
Notification notification = Notification.newBuilder()
4949
.addPlatformNotification(IosNotification.alert("alert"))
5050
.build();
@@ -58,7 +58,7 @@ public void testAlertIos() {
5858
}
5959

6060
@Test
61-
public void testAlertMpns() {
61+
public void testAlert_winphone() {
6262
Notification notification = Notification.newBuilder()
6363
.addPlatformNotification(WinphoneNotification.alert("alert"))
6464
.build();
@@ -70,14 +70,45 @@ public void testAlertMpns() {
7070
}
7171

7272
@Test
73-
public void testAlertAll() {
73+
public void testAlert_all() {
7474
Notification notification = Notification.alert("alert");
7575
JsonObject json = new JsonObject();
7676
json.add("alert", new JsonPrimitive("alert"));
77-
77+
78+
Assert.assertEquals("", json, notification.toJSON());
79+
}
80+
81+
@Test
82+
public void testExtras_jsonValue() {
83+
JsonObject extraValue = new JsonObject();
84+
extraValue.add("v_key", new JsonPrimitive("v_value"));
85+
86+
Notification notification = Notification
87+
.newBuilder()
88+
.addPlatformNotification(
89+
AndroidNotification
90+
.newBuilder()
91+
.setAlert("alert")
92+
.addExtra("key", extraValue)
93+
.build())
94+
.build();
95+
96+
JsonObject json = new JsonObject();
97+
JsonObject android = new JsonObject();
98+
android.add("alert", new JsonPrimitive("alert"));
99+
100+
JsonObject extra = new JsonObject();
101+
extra.add("key", extraValue);
102+
android.add("extras", extra);
103+
104+
json.add("android", android);
105+
78106
Assert.assertEquals("", json, notification.toJSON());
79107
}
80108

109+
110+
111+
81112
@Test
82113
public void testShortcut_android() {
83114
Notification notification = Notification.android("alert", "title", null);

0 commit comments

Comments
 (0)