Skip to content

Commit 2b66871

Browse files
committed
add toSerializeJSON to PushPayload
1 parent 406afbd commit 2b66871

File tree

7 files changed

+137
-26
lines changed

7 files changed

+137
-26
lines changed

example/main/java/cn/jpush/api/examples/PushExample.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,12 @@ public static void testSendPush() {
5151
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
5252

5353
// For push, all you need do is to build PushPayload object.
54-
PushPayload payload = buildPushObject_all_all_alert();
55-
54+
PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
55+
String payloadJson = payload.toSerializeJSON().toString();
56+
System.out.print(payloadJson);
57+
PushPayload newPayload = PushPayload.fromJSON(payloadJson);
5658
try {
57-
PushResult result = jpushClient.sendPush(payload);
59+
PushResult result = jpushClient.sendPush(newPayload);
5860
LOG.info("Got result - " + result);
5961

6062
} catch (APIConnectionException e) {
@@ -68,6 +70,31 @@ public static void testSendPush() {
6870
LOG.info("Msg ID: " + e.getMsgId());
6971
}
7072
}
73+
74+
//use Payload.fromJSON to build payload instance
75+
public static void testSendPush_fromJSON() {
76+
ClientConfig clientConfig = ClientConfig.getInstance();
77+
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
78+
79+
String payloadString = "{\"platform\":{\"all\":\"false\",\"deviceTypes\":[\"android\",\"ios\"]},\"audience\":{\"all\":\"true\"},\"notification\":{\"alert\":\"Test\",\"ios\":{\"alert\":\"第 1 条\",\"extras\":{\"extra_key\":\"extra_value\"},\"badge\":\"+1\",\"sound\":\"\"},\"android\":{\"alert\":\"第 1 条\",\"extras\":{\"android_key\":\"android_value\"}}},\"options\":{\"sendno\":182145298,\"apns_production\":false}}";
80+
//also equals
81+
//String payloadString = "{\"platform\":{\"all\":\"true\"},\"audience\":{\"all\":\"true\"},\"notification\":{\"alert\":\"Test\",\"ios\":{\"alert\":\"第 1 条\",\"extras\":{\"extra_key\":\"extra_value\"},\"badge\":\"+1\",\"sound\":\"\"},\"android\":{\"alert\":\"第 1 条\",\"extras\":{\"android_key\":\"android_value\"}}},\"options\":{\"sendno\":182145298,\"apns_production\":false}}";
82+
PushPayload payload = PushPayload.fromJSON(payloadString);
83+
try {
84+
PushResult result = jpushClient.sendPush(payload);
85+
LOG.info("Got result - " + result);
86+
87+
} catch (APIConnectionException e) {
88+
LOG.error("Connection error. Should retry later. ", e);
89+
90+
} catch (APIRequestException e) {
91+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
92+
LOG.info("HTTP Status: " + e.getStatus());
93+
LOG.info("Error Code: " + e.getErrorCode());
94+
LOG.info("Error Message: " + e.getErrorMessage());
95+
LOG.info("Msg ID: " + e.getMsgId());
96+
}
97+
}
7198

7299
public static PushPayload buildPushObject_all_all_alert() {
73100
return PushPayload.alertAll(ALERT);

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

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,17 @@
55

66
import com.google.gson.JsonArray;
77
import com.google.gson.JsonElement;
8+
import com.google.gson.JsonObject;
89
import com.google.gson.JsonPrimitive;
9-
10-
import cn.jiguang.commom.DeviceType;
1110
import cn.jiguang.commom.utils.Preconditions;
1211

1312
public class Platform implements PushModel {
1413
private static final String ALL = "all";
1514

1615
private final boolean all;
17-
private final Set<DeviceType> deviceTypes;
16+
private final Set<String> deviceTypes;
1817

19-
private Platform(boolean all, Set<DeviceType> deviceTypes) {
18+
private Platform(boolean all, Set<String> deviceTypes) {
2019
this.all = all;
2120
this.deviceTypes = deviceTypes;
2221
}
@@ -30,35 +29,35 @@ public static Platform all() {
3029
}
3130

3231
public static Platform android() {
33-
return newBuilder().addDeviceType(DeviceType.Android).build();
32+
return newBuilder().addDeviceType("android").build();
3433
}
3534

3635
public static Platform ios() {
37-
return newBuilder().addDeviceType(DeviceType.IOS).build();
36+
return newBuilder().addDeviceType("ios").build();
3837
}
3938

4039
public static Platform winphone() {
41-
return newBuilder().addDeviceType(DeviceType.WinPhone).build();
40+
return newBuilder().addDeviceType("winphone").build();
4241
}
4342

4443
public static Platform android_ios() {
4544
return newBuilder()
46-
.addDeviceType(DeviceType.Android)
47-
.addDeviceType(DeviceType.IOS)
45+
.addDeviceType("android")
46+
.addDeviceType("ios")
4847
.build();
4948
}
5049

5150
public static Platform android_winphone() {
5251
return newBuilder()
53-
.addDeviceType(DeviceType.Android)
54-
.addDeviceType(DeviceType.WinPhone)
52+
.addDeviceType("android")
53+
.addDeviceType("winphone")
5554
.build();
5655
}
5756

5857
public static Platform ios_winphone() {
5958
return newBuilder()
60-
.addDeviceType(DeviceType.IOS)
61-
.addDeviceType(DeviceType.WinPhone)
59+
.addDeviceType("ios")
60+
.addDeviceType("winphone")
6261
.build();
6362
}
6463

@@ -73,25 +72,40 @@ public JsonElement toJSON() {
7372
}
7473

7574
JsonArray json = new JsonArray();
76-
for (DeviceType deviceType : deviceTypes) {
77-
json.add(new JsonPrimitive(deviceType.value()));
75+
for (String deviceType : deviceTypes) {
76+
json.add(new JsonPrimitive(deviceType));
77+
}
78+
return json;
79+
}
80+
81+
public JsonElement toSerializeJSON() {
82+
JsonObject json = new JsonObject();
83+
if (all) {
84+
json.add(ALL, new JsonPrimitive(true));
85+
} else {
86+
json.add(ALL, new JsonPrimitive(false));
87+
}
88+
JsonArray jsonArray = new JsonArray();
89+
for (String deviceType : deviceTypes) {
90+
jsonArray.add(new JsonPrimitive(deviceType));
7891
}
92+
json.add("deviceTypes", jsonArray);
7993
return json;
8094
}
8195

8296

8397
public static class Builder {
8498
private boolean all;
85-
private Set<DeviceType> deviceTypes;
99+
private Set<String> deviceTypes;
86100

87101
public Builder setAll(boolean all) {
88102
this.all = all;
89103
return this;
90104
}
91105

92-
public Builder addDeviceType(DeviceType deviceType) {
106+
public Builder addDeviceType(String deviceType) {
93107
if (null == deviceTypes) {
94-
deviceTypes = new HashSet<DeviceType>();
108+
deviceTypes = new HashSet<String>();
95109
}
96110
deviceTypes.add(deviceType);
97111
return this;

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ public JsonElement toJSON() {
153153

154154
return json;
155155
}
156+
157+
public JsonElement toSerializeJSON() {
158+
JsonObject json = new JsonObject();
159+
if (null != platform) {
160+
json.add(PLATFORM, platform.toSerializeJSON());
161+
}
162+
if (null != audience) {
163+
json.add(AUDIENCE, audience.toSerializeJSON());
164+
}
165+
if (null != notification) {
166+
json.add(NOTIFICATION, notification.toJSON());
167+
}
168+
if (null != message) {
169+
json.add(MESSAGE, message.toJSON());
170+
}
171+
if (null != options) {
172+
json.add(OPTIONS, options.toJSON());
173+
}
174+
if (null != sms) {
175+
json.add(SMS, sms.toJSON());
176+
}
177+
178+
return json;
179+
}
156180

157181
public boolean isGlobalExceedLength() {
158182
int messageLength = 0;

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,21 @@ public JsonElement toJSON() {
119119
}
120120
return json;
121121
}
122+
123+
public JsonElement toSerializeJSON() {
124+
JsonObject json = new JsonObject();
125+
if (all) {
126+
json.add(ALL, new JsonPrimitive(true));
127+
} else {
128+
json.add(ALL, new JsonPrimitive(false));
129+
}
130+
if (null != targets) {
131+
for (AudienceTarget target : targets) {
132+
json.add(target.getAudienceTypeValue(), target.toJSON());
133+
}
134+
}
135+
return json;
136+
}
122137

123138
public static class Builder {
124139
private boolean all = false;

src/test/java/cn/jpush/api/push/PushClientTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,48 @@
33
import static org.junit.Assert.assertTrue;
44
import static org.junit.Assert.fail;
55

6+
import cn.jiguang.commom.ClientConfig;
7+
import cn.jpush.api.JPushClient;
8+
import cn.jpush.api.examples.PushExample;
9+
import cn.jpush.api.push.model.Platform;
610
import org.junit.Test;
711

812
import cn.jiguang.common.connection.HttpProxy;
913
import cn.jiguang.common.resp.APIConnectionException;
1014
import cn.jiguang.common.resp.APIRequestException;
1115
import cn.jpush.api.BaseTest;
1216
import cn.jpush.api.push.model.PushPayload;
17+
import org.slf4j.Logger;
18+
import org.slf4j.LoggerFactory;
1319

1420
public class PushClientTest extends BaseTest {
1521

22+
protected static final Logger LOG = LoggerFactory.getLogger(PushClientTest.class);
23+
24+
@Test
25+
public void testSendPush() {
26+
ClientConfig clientConfig = ClientConfig.getInstance();
27+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
28+
29+
// String payloadString = "{\"platform\":{\"all\":\"true\"},\"audience\":{\"all\":\"true\"},\"notification\":{\"alert\":\"Test\",\"ios\":{\"alert\":\"第 1 条\",\"extras\":{\"extra_key\":\"extra_value\"},\"badge\":\"+1\",\"sound\":\"\"},\"android\":{\"alert\":\"第 1 条\",\"extras\":{\"android_key\":\"android_value\"}}},\"options\":{\"sendno\":182145298,\"apns_production\":false}}";
30+
String payloadString = "{\"platform\":{\"all\":\"false\",\"deviceTypes\":[\"android\",\"ios\"]},\"audience\":{\"all\":\"true\"},\"notification\":{\"alert\":\"Test\",\"ios\":{\"alert\":\"第 1 条\",\"extras\":{\"extra_key\":\"extra_value\"},\"badge\":\"+1\",\"sound\":\"\"},\"android\":{\"alert\":\"第 1 条\",\"extras\":{\"android_key\":\"android_value\"}}},\"options\":{\"sendno\":182145298,\"apns_production\":false}}";
31+
PushPayload payload = PushPayload.fromJSON(payloadString);
32+
try {
33+
PushResult result = jpushClient.sendPush(payload);
34+
LOG.info("Got result - " + result);
35+
36+
} catch (APIConnectionException e) {
37+
LOG.error("Connection error. Should retry later. ", e);
38+
39+
} catch (APIRequestException e) {
40+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
41+
LOG.info("HTTP Status: " + e.getStatus());
42+
LOG.info("Error Code: " + e.getErrorCode());
43+
LOG.info("Error Message: " + e.getErrorMessage());
44+
LOG.info("Msg ID: " + e.getMsgId());
45+
}
46+
}
47+
1648
@Test(expected = IllegalArgumentException.class)
1749
public void test_invalid_json() {
1850
PushClient pushClient = new PushClient(MASTER_SECRET, APP_KEY);

src/test/java/cn/jpush/api/push/model/PlatformTesst.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.google.gson.JsonElement;
1212
import com.google.gson.JsonPrimitive;
1313

14-
import cn.jiguang.commom.DeviceType;
1514
import cn.jpush.api.FastTests;
1615

1716
@Category(FastTests.class)
@@ -32,7 +31,7 @@ public void testNotAll() {
3231

3332
@Test
3433
public void testAndroid() {
35-
Platform android = Platform.newBuilder().addDeviceType(DeviceType.Android).build();
34+
Platform android = Platform.newBuilder().addDeviceType("winphone").build();
3635
JsonArray array = new JsonArray();
3736
array.add(new JsonPrimitive("android"));
3837

src/test/java/cn/jpush/api/push/remote/BasicFunctionsTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,9 @@ public void sendSimpleNotification_Pall_Nwp() throws Exception {
110110
public void sendSimpleNotification_Pall_Nall() throws Exception {
111111
PushPayload payload = PushPayload.newBuilder()
112112
.setPlatform(Platform.newBuilder()
113-
.addDeviceType(DeviceType.IOS)
114-
.addDeviceType(DeviceType.WinPhone)
115-
.addDeviceType(DeviceType.Android).build())
113+
.addDeviceType("ios")
114+
.addDeviceType("winphone")
115+
.addDeviceType("android").build())
116116
.setAudience(Audience.all())
117117
.setNotification(Notification.newBuilder()
118118
.addPlatformNotification(WinphoneNotification.alert("Pall Nall wp alert"))

0 commit comments

Comments
 (0)