Skip to content

Commit a879164

Browse files
author
Javen
committed
Refactoring: support number/boolean extra;
more tests.
1 parent c8952c1 commit a879164

17 files changed

+408
-188
lines changed

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,11 @@
99
import com.google.gson.JsonObject;
1010
import com.google.gson.JsonPrimitive;
1111

12-
public class Message implements PushModel {
13-
public static final String MESSAGE = "message";
14-
15-
public static final String TITLE = "title";
16-
public static final String MSG_CONTENT = "msg_content";
17-
public static final String CONTENT_TYPE = "content_type";
18-
public static final String EXTRAS = "extras";
12+
public class Message implements PushModel {
13+
private static final String TITLE = "title";
14+
private static final String MSG_CONTENT = "msg_content";
15+
private static final String CONTENT_TYPE = "content_type";
16+
private static final String EXTRAS = "extras";
1917

2018
private final String title;
2119
private final String msgContent;

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,10 @@
66
import com.google.gson.JsonPrimitive;
77

88
public class Options implements PushModel {
9-
public static final String OPTIONS = "options";
10-
11-
public static final String SENDNO = "sendno";
12-
public static final String OVERRIDE_MSG_ID = "override_msg_id";
13-
public static final String TIME_TO_LIVE = "time_to_live";
14-
public static final String APNS_PRODUCTION = "apns_production";
9+
private static final String SENDNO = "sendno";
10+
private static final String OVERRIDE_MSG_ID = "override_msg_id";
11+
private static final String TIME_TO_LIVE = "time_to_live";
12+
private static final String APNS_PRODUCTION = "apns_production";
1513

1614
private final int sendno;
1715
private final int overrideMsgId;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import com.google.gson.JsonElement;
99
import com.google.gson.JsonPrimitive;
1010

11-
public class Platform implements PushModel {
12-
public static final String PLATFORM = "platform";
13-
14-
public static final String ALL = "all";
11+
public class Platform implements PushModel {
12+
private static final String ALL = "all";
1513

1614
private final boolean all;
1715
private final ImmutableSet<DeviceType> deviceTypes;

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

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@
99
import com.google.gson.JsonObject;
1010

1111
public class PushPayload implements PushModel {
12+
private static final String PLATFORM = "platform";
13+
private static final String AUDIENCE = "audience";
14+
private static final String NOTIFICATION = "notification";
15+
private static final String MESSAGE = "message";
16+
private static final String OPTIONS = "options";
17+
1218
private static Gson _gson = new Gson();
1319

1420
private final Platform platform;
@@ -68,19 +74,19 @@ public void resetOptionsTimeToLive(long timeToLive) {
6874
public JsonElement toJSON() {
6975
JsonObject json = new JsonObject();
7076
if (null != platform) {
71-
json.add(Platform.PLATFORM, platform.toJSON());
77+
json.add(PLATFORM, platform.toJSON());
7278
}
7379
if (null != audience) {
74-
json.add(Audience.AUDIENCE, audience.toJSON());
80+
json.add(AUDIENCE, audience.toJSON());
7581
}
7682
if (null != notification) {
77-
json.add(Notification.NOTIFICATION, notification.toJSON());
83+
json.add(NOTIFICATION, notification.toJSON());
7884
}
7985
if (null != message) {
80-
json.add(Message.MESSAGE, message.toJSON());
86+
json.add(MESSAGE, message.toJSON());
8187
}
8288
if (null != options) {
83-
json.add(Options.OPTIONS, options.toJSON());
89+
json.add(OPTIONS, options.toJSON());
8490
}
8591
return json;
8692
}

src/cn/jpush/api/push/model/audience/Audience.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,13 @@
1111
import com.google.gson.JsonPrimitive;
1212

1313
public class Audience implements PushModel {
14-
public static final String AUDIENCE = "audience";
15-
public static final String ALL = "all";
16-
17-
public static final String TYPE_TAG = "tag";
18-
public static final String TYPE_TAG_AND = "tag_and";
19-
public static final String TYPE_ALIAS = "alias";
20-
public static final String TYPE_SEGMENT = "segment";
21-
public static final String TYPE_REGISTRATION_ID = "registration_id";
14+
private static final String ALL = "all";
15+
16+
private static final String TYPE_TAG = "tag";
17+
private static final String TYPE_TAG_AND = "tag_and";
18+
private static final String TYPE_ALIAS = "alias";
19+
private static final String TYPE_SEGMENT = "segment";
20+
private static final String TYPE_REGISTRATION_ID = "registration_id";
2221

2322
private final boolean all;
2423
private final ImmutableSet<AudienceTarget> targets;
Lines changed: 33 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,27 @@
11
package cn.jpush.api.push.model.notification;
22

3-
import java.util.Map;
4-
5-
import com.google.common.base.Preconditions;
63
import com.google.common.collect.ImmutableMap;
74
import com.google.gson.JsonElement;
85
import com.google.gson.JsonObject;
96
import com.google.gson.JsonPrimitive;
107

118
public class AndroidNotification extends PlatformNotification {
12-
public static final String NOTIFICATION_ANDROID = "android";
9+
private static final String NOTIFICATION_ANDROID = "android";
1310

14-
public static final String TITLE = "title";
15-
public static final String BUILDER_ID = "builder_id";
16-
public static final String EXTRAS = "extras";
11+
private static final String TITLE = "title";
12+
private static final String BUILDER_ID = "builder_id";
1713

1814
private final String title;
1915
private final int builderId;
20-
private final ImmutableMap<String, String> extras;
21-
16+
2217
private AndroidNotification(String alert, String title, int builderId,
23-
ImmutableMap<String, String> extras) {
24-
super(alert);
18+
ImmutableMap<String, String> extras,
19+
ImmutableMap<String, Number> numberExtras,
20+
ImmutableMap<String, Boolean> booleanExtras) {
21+
super(alert, extras, numberExtras, booleanExtras);
22+
2523
this.title = title;
2624
this.builderId = builderId;
27-
this.extras = extras;
2825
}
2926

3027
public static Builder newBuilder() {
@@ -43,42 +40,22 @@ public String getPlatform() {
4340

4441
@Override
4542
public JsonElement toJSON() {
46-
JsonObject json = new JsonObject();
47-
if (null != alert) {
48-
json.add(ALERT, new JsonPrimitive(this.alert));
49-
}
43+
JsonObject json = super.toJSON().getAsJsonObject();
44+
5045
if (builderId > 0) {
5146
json.add(BUILDER_ID, new JsonPrimitive(this.builderId));
5247
}
5348
if (null != title) {
5449
json.add(TITLE, new JsonPrimitive(title));
5550
}
56-
if (null != extras) {
57-
JsonObject extrasObject = new JsonObject();
58-
for (String key : extras.keySet()) {
59-
extrasObject.add(key, new JsonPrimitive(extras.get(key)));
60-
}
61-
json.add(EXTRAS, extrasObject);
62-
}
6351

64-
Preconditions.checkArgument(
65-
! (null == alert && null == title && 0 == builderId && null == extras)
66-
, "No any notification params are set.");
67-
6852
return json;
6953
}
7054

7155

72-
public static class Builder {
73-
private String alert;
56+
public static class Builder extends PlatformNotification.Builder<AndroidNotification> {
7457
private String title;
7558
private int builderId;
76-
private ImmutableMap.Builder<String, String> extrasBuilder;
77-
78-
public Builder setAlert(String alert) {
79-
this.alert = alert;
80-
return this;
81-
}
8259

8360
public Builder setTitle(String title) {
8461
this.title = title;
@@ -90,11 +67,8 @@ public Builder setBuilderId(int builderId) {
9067
return this;
9168
}
9269

93-
public Builder addExtra(Map<String, String> extra) {
94-
if (null == extrasBuilder) {
95-
extrasBuilder = ImmutableMap.builder();
96-
}
97-
extrasBuilder.putAll(extra);
70+
public Builder setAlert(String alert) {
71+
this.alert = alert;
9872
return this;
9973
}
10074

@@ -106,9 +80,27 @@ public Builder addExtra(String key, String value) {
10680
return this;
10781
}
10882

83+
public Builder addExtra(String key, Number value) {
84+
if (null == numberExtrasBuilder) {
85+
numberExtrasBuilder = ImmutableMap.builder();
86+
}
87+
numberExtrasBuilder.put(key, value);
88+
return this;
89+
}
90+
91+
public Builder addExtra(String key, Boolean value) {
92+
if (null == booleanExtrasBuilder) {
93+
booleanExtrasBuilder = ImmutableMap.builder();
94+
}
95+
booleanExtrasBuilder.put(key, value);
96+
return this;
97+
}
98+
10999
public AndroidNotification build() {
110100
return new AndroidNotification(alert, title, builderId,
111-
(null == extrasBuilder) ? null : extrasBuilder.build());
101+
(null == extrasBuilder) ? null : extrasBuilder.build(),
102+
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
103+
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());
112104
}
113105
}
114106
}
Lines changed: 34 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,35 @@
11
package cn.jpush.api.push.model.notification;
22

3-
import java.util.Map;
4-
5-
import com.google.common.base.Preconditions;
63
import com.google.common.collect.ImmutableMap;
74
import com.google.gson.JsonElement;
85
import com.google.gson.JsonObject;
96
import com.google.gson.JsonPrimitive;
107

118
public class IosNotification extends PlatformNotification {
12-
public static final String NOTIFICATION_IOS = "ios";
9+
private static final String NOTIFICATION_IOS = "ios";
1310

14-
public static final String DEFAULT_SOUND = "";
15-
public static final String APS = "aps";
11+
private static final String DEFAULT_SOUND = "";
1612

17-
public static final String BADGE = "badge";
18-
public static final String SOUND = "sound";
19-
public static final String CONTENT_AVAILABLE = "content-available";
20-
public static final String EXTRAS = "extras";
13+
private static final String BADGE = "badge";
14+
private static final String SOUND = "sound";
15+
private static final String CONTENT_AVAILABLE = "content-available";
2116

2217
private final boolean soundDisabled;
2318
private final String sound;
2419
private final int badge;
2520
private final boolean contentAvailable;
26-
private final ImmutableMap<String, String> extras;
2721

2822
private IosNotification(String alert, String sound, int badge,
2923
boolean contentAvailable, boolean soundDisabled,
30-
ImmutableMap<String, String> extras) {
31-
super(alert);
24+
ImmutableMap<String, String> extras,
25+
ImmutableMap<String, Number> numberExtras,
26+
ImmutableMap<String, Boolean> booleanExtras) {
27+
super(alert, extras, numberExtras, booleanExtras);
28+
3229
this.sound = sound;
3330
this.badge = badge;
3431
this.contentAvailable = contentAvailable;
3532
this.soundDisabled = soundDisabled;
36-
this.extras = extras;
3733
}
3834

3935
public static Builder newBuilder() {
@@ -52,10 +48,8 @@ public String getPlatform() {
5248

5349
@Override
5450
public JsonElement toJSON() {
55-
JsonObject json = new JsonObject();
56-
if (null != alert) {
57-
json.add(ALERT, new JsonPrimitive(this.alert));
58-
}
51+
JsonObject json = super.toJSON().getAsJsonObject();
52+
5953
if (badge >= 0) {
6054
json.add(BADGE, new JsonPrimitive(this.badge));
6155
}
@@ -70,34 +64,15 @@ public JsonElement toJSON() {
7064
json.add(CONTENT_AVAILABLE, new JsonPrimitive(1));
7165
}
7266

73-
if (null != extras) {
74-
JsonObject extrasObject = new JsonObject();
75-
for (String key : extras.keySet()) {
76-
extrasObject.add(key, new JsonPrimitive(extras.get(key)));
77-
}
78-
json.add(EXTRAS, extrasObject);
79-
}
80-
81-
Preconditions.checkArgument(
82-
! (null == alert && null == sound && badge < 0 && !contentAvailable && null == extras)
83-
, "No any notification params are set.");
84-
8567
return json;
8668
}
8769

8870

89-
public static class Builder {
90-
private String alert;
71+
public static class Builder extends PlatformNotification.Builder<IosNotification> {
9172
private String sound;
9273
private int badge = -1;
9374
private boolean contentAvailable = false;
9475
private boolean soundDisabled = false;
95-
private ImmutableMap.Builder<String, String> extrasBuilder;
96-
97-
public Builder setAlert(String alert) {
98-
this.alert = alert;
99-
return this;
100-
}
10176

10277
public Builder setSound(String sound) {
10378
this.sound = sound;
@@ -119,11 +94,8 @@ public Builder setContentAvailable(boolean contentAvailable) {
11994
return this;
12095
}
12196

122-
public Builder addExtras(Map<String, String> extras) {
123-
if (null == extrasBuilder) {
124-
extrasBuilder = ImmutableMap.builder();
125-
}
126-
extrasBuilder.putAll(extras);
97+
public Builder setAlert(String alert) {
98+
this.alert = alert;
12799
return this;
128100
}
129101

@@ -135,9 +107,27 @@ public Builder addExtra(String key, String value) {
135107
return this;
136108
}
137109

110+
public Builder addExtra(String key, Number value) {
111+
if (null == numberExtrasBuilder) {
112+
numberExtrasBuilder = ImmutableMap.builder();
113+
}
114+
numberExtrasBuilder.put(key, value);
115+
return this;
116+
}
117+
118+
public Builder addExtra(String key, Boolean value) {
119+
if (null == booleanExtrasBuilder) {
120+
booleanExtrasBuilder = ImmutableMap.builder();
121+
}
122+
booleanExtrasBuilder.put(key, value);
123+
return this;
124+
}
125+
138126
public IosNotification build() {
139127
return new IosNotification(alert, sound, badge, contentAvailable, soundDisabled,
140-
(null == extrasBuilder) ? null : extrasBuilder.build());
128+
(null == extrasBuilder) ? null : extrasBuilder.build(),
129+
(null == numberExtrasBuilder) ? null : numberExtrasBuilder.build(),
130+
(null == booleanExtrasBuilder) ? null : booleanExtrasBuilder.build());
141131
}
142132
}
143133
}

0 commit comments

Comments
 (0)