Skip to content

Commit 5aed130

Browse files
author
Javen
committed
1. Change mpns to winphone
2. Fix - platform is Required 3. Fix - payload to String bug 4. add and fix more tests.
1 parent 7afc854 commit 5aed130

File tree

11 files changed

+142
-211
lines changed

11 files changed

+142
-211
lines changed

src/cn/jpush/api/examples/JPushClientExample.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.slf4j.LoggerFactory;
55

66
import cn.jpush.api.JPushClient;
7+
import cn.jpush.api.push.PushClient;
78
import cn.jpush.api.push.model.PushPayload;
89
import cn.jpush.api.report.ReceivedsResult;
910

@@ -20,14 +21,19 @@ public class JPushClientExample {
2021
public static final String TAG = "tag_api";
2122

2223
public static void main(String[] args) {
23-
//testSendNotification();
24-
testSendMesasge();
25-
testGetReport();
24+
testSendNotification();
25+
// testSendMesasge();
26+
// testGetReport();
2627
}
2728

2829
private static void testSendNotification() {
30+
// Just for test
31+
PushClient.HOST_NAME_SSL = "https://api.jpush.cn:19688";
32+
2933
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
3034
PushPayload payload = PushPayload.notificationAlertAll(CONTENT);
35+
LOG.info("Paylaod JSON - " + payload.toString());
36+
3137
jpushClient.sendPush(payload);
3238
}
3339

src/cn/jpush/api/push/PushClient.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Can be used directly.
1616
*/
1717
public class PushClient extends BaseHttpClient {
18-
private static final String HOST_NAME_SSL = "https://api.jpush.cn";
18+
public static String HOST_NAME_SSL = "https://api.jpush.cn";
1919
private static final String PUSH_PATH = "/v3/push";
2020

2121
private static final String PUSH_URL = HOST_NAME_SSL + PUSH_PATH;
@@ -59,7 +59,7 @@ public PushResult sendPush(PushPayload pushPayload) {
5959
pushPayload.resetOptionsApnsProduction(_apnsProduction);
6060
}
6161

62-
ResponseResult response = sendPost(PUSH_URL, pushPayload.toJSON().getAsString(), _authCode);
62+
ResponseResult response = sendPost(PUSH_URL, pushPayload.toString(), _authCode);
6363
PushResult pushResult = null;
6464
if (response.responseCode == RESPONSE_OK) {
6565
pushResult = _gson.fromJson(response.responseContent, PushResult.class);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import com.google.gson.JsonPrimitive;
77

88
public class Options implements PushModel {
9-
public static final String OPTIONAL = "optional";
9+
public static final String OPTIONS = "options";
1010

1111
public static final String SENDNO = "sendno";
1212
public static final String OVERRIDE_MSG_ID = "override_msg_id";

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

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import cn.jpush.api.push.model.notification.Notification;
55

66
import com.google.common.base.Preconditions;
7+
import com.google.gson.Gson;
78
import com.google.gson.JsonElement;
89
import com.google.gson.JsonObject;
910

1011
public class PushPayload implements PushModel {
12+
private static Gson _gson = new Gson();
13+
1114
private final Platform platform;
1215
private final Audience audience;
1316
private final Notification notification;
@@ -29,12 +32,14 @@ public static Builder newBuilder() {
2932

3033
public static PushPayload notificationAlertAll(String alert) {
3134
return new Builder()
35+
.setPlatform(Platform.all())
3236
.setAudience(Audience.all())
3337
.setNotification(Notification.alert(alert)).build();
3438
}
3539

3640
public static PushPayload simpleMessageAll(String content) {
3741
return new Builder()
42+
.setPlatform(Platform.all())
3843
.setAudience(Audience.all())
3944
.setMessage(Message.content(content)).build();
4045
}
@@ -58,20 +63,29 @@ public void resetOptionsTimeToLive(long timeToLive) {
5863
@Override
5964
public JsonElement toJSON() {
6065
JsonObject json = new JsonObject();
61-
json.add(Platform.PLATFORM, platform.toJSON());
62-
json.add(Audience.AUDIENCE, audience.toJSON());
66+
if (null != platform) {
67+
json.add(Platform.PLATFORM, platform.toJSON());
68+
}
69+
if (null != audience) {
70+
json.add(Audience.AUDIENCE, audience.toJSON());
71+
}
6372
if (null != notification) {
6473
json.add(Notification.NOTIFICATION, notification.toJSON());
6574
}
6675
if (null != message) {
6776
json.add(Message.MESSAGE, message.toJSON());
6877
}
69-
if (null != message) {
70-
json.add(Options.OPTIONAL, options.toJSON());
78+
if (null != options) {
79+
json.add(Options.OPTIONS, options.toJSON());
7180
}
7281
return json;
7382
}
7483

84+
@Override
85+
public String toString() {
86+
return _gson.toJson(toJSON());
87+
}
88+
7589
public static class Builder {
7690
private Platform platform = null;
7791
private Audience audience = null;
@@ -105,7 +119,7 @@ public Builder setOptions(Options options) {
105119
}
106120

107121
public PushPayload build() {
108-
Preconditions.checkArgument(! (null == audience || null == platform), "Audience should be set.");
122+
Preconditions.checkArgument(! (null == audience || null == platform), "Audience/Platform should be set.");
109123
Preconditions.checkArgument(! (null == notification && null == message), "notification or message should be set at least one.");
110124
return new PushPayload(platform, audience, notification, message, options);
111125
}

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -33,24 +33,8 @@ public String getAudienceTypeValue() {
3333

3434
public JsonElement toJSON() {
3535
JsonArray array = new JsonArray();
36-
boolean numberValue = false;
37-
if (audienceType == AudienceType.REGISTRATION_ID
38-
|| audienceType == AudienceType.SEGMENT) {
39-
numberValue = true;
40-
}
41-
42-
try {
43-
for (String value : values) {
44-
int number = 0;
45-
if (numberValue) {
46-
number = Integer.parseInt(value);
47-
array.add(new JsonPrimitive(number));
48-
} else {
49-
array.add(new JsonPrimitive(value));
50-
}
51-
}
52-
} catch (NumberFormatException e) {
53-
Preconditions.checkArgument(true, "Value of registration_id/segment should be int.");
36+
for (String value : values) {
37+
array.add(new JsonPrimitive(value));
5438
}
5539
return array;
5640
}

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

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,29 +24,28 @@ public static Builder newBuilder() {
2424
}
2525

2626
/**
27-
* Quick set all platform alert
27+
* Quick set all platform alert.
28+
* Platform notification can override this alert.
2829
*
2930
* @param alert Notification alert
3031
* @return first level notification object
3132
*/
3233
public static Notification alert(String alert) {
33-
return newBuilder()
34-
.addPlatformNotification(AndroidNotification.alert(alert))
35-
.addPlatformNotification(IosNotification.alert(alert))
36-
.addPlatformNotification(MpnsNotification.alert(alert))
37-
.setAlert(alert).build();
34+
return newBuilder().setAlert(alert).build();
3835
}
3936

4037
public JsonElement toJSON() {
4138
JsonObject json = new JsonObject();
4239
if (null != alert) {
4340
json.add(PlatformNotification.ALERT, new JsonPrimitive(alert));
4441
}
45-
for (PlatformNotification pn : notifications) {
46-
if (this.alert != null && pn.getAlert() == null) {
47-
pn.setAlert(this.alert);
42+
if (null != notifications) {
43+
for (PlatformNotification pn : notifications) {
44+
if (this.alert != null && pn.getAlert() == null) {
45+
pn.setAlert(this.alert);
46+
}
47+
json.add(pn.getPlatform(), pn.toJSON());
4848
}
49-
json.add(pn.getPlatform(), pn.toJSON());
5049
}
5150
return json;
5251
}
@@ -69,8 +68,9 @@ public Builder addPlatformNotification(PlatformNotification notification) {
6968
}
7069

7170
public Notification build() {
72-
Preconditions.checkArgument(! (null == builder), "Should set at least one platform notification");
73-
return new Notification(alert, builder.build());
71+
Preconditions.checkArgument(! (null == builder && null == alert),
72+
"No notification payload is set.");
73+
return new Notification(alert, (null == builder) ? null : builder.build());
7474
}
7575
}
7676
}
Lines changed: 0 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
package cn.jpush.api;
2-
import static org.junit.Assert.assertEquals;
3-
42
import org.junit.Before;
5-
import org.junit.Test;
6-
7-
import cn.jpush.api.common.DeviceType;
8-
import cn.jpush.api.common.ErrorCodeEnum;
9-
import cn.jpush.api.push.PushResult;
10-
import cn.jpush.api.push.NotificationParams;
11-
import cn.jpush.api.push.model.audience.AudienceType;
123

134
public class PushErrorTests {
145
private static final String appKey ="dd1066407b044738b6479275";
@@ -22,68 +13,9 @@ public class PushErrorTests {
2213

2314
@Before
2415
public void before() {
25-
jpushAndroid = new JPushClient(masterSecret, appKey, 0, DeviceType.Android, false);
26-
jpushIos = new JPushClient(masterSecret, appKey, 0, DeviceType.IOS, false);
2716
}
2817

29-
public int sendNotification(String content) {
30-
NotificationParams params = new NotificationParams();
31-
params.setReceiverType(AudienceType.TAG);
32-
params.setReceiverValue(TAG);
33-
PushResult result = jpushAndroid.sendNotification(content, params, null);
34-
return result.errcode;
35-
}
36-
37-
@Test
38-
public void testSendNotificationWithInvalidAppkey(){
39-
String appKey = "7d431e42dfa6a6d693ac2d05"; // invalid app_key
40-
jpushAndroid = new JPushClient(masterSecret, appKey);
41-
42-
assertEquals(ErrorCodeEnum.InvalidAppKey.value(), sendNotification(MSG_CONTENT));
43-
}
44-
45-
@Test
46-
public void testSendNotificationWithBigMessage(){
47-
String msgContent = "jpushjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj" +
48-
"ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
49-
"ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
50-
"ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss" +
51-
"sddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeeeeeeeeeee" +
52-
"sdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddeeeeeeeeeee" +
53-
"sdddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddcontenteeeeeeeeeeee";
5418

55-
assertEquals(ErrorCodeEnum.InvalidParameter.value(), sendNotification(msgContent));
56-
}
57-
58-
@Test
59-
public void testSendNotificationWithAppKeyValidateFailed (){
60-
String masterSecret = "5e987ac6d2e04d95a9d8f0d2"; // invalid masterSeret
61-
jpushAndroid = new JPushClient(masterSecret, appKey);
62-
63-
assertEquals(ErrorCodeEnum.VerificationFailed.value(), sendNotification(MSG_CONTENT));
64-
}
65-
66-
@Test
67-
public void testSendNotificationWithInvalidTag() {
68-
String invalid_tag = "invalid_tag_1_1_";
69-
NotificationParams params = new NotificationParams();
70-
params.setReceiverType(AudienceType.TAG);
71-
params.setReceiverValue(invalid_tag);
72-
PushResult result = jpushAndroid.sendNotification(MSG_CONTENT, params, null);
73-
74-
assertEquals(ErrorCodeEnum.NoTarget.value(), result.errcode);
75-
}
76-
77-
@Test
78-
public void testSendNotificationWithAppKeyInvalidPushByAlgin(){
79-
String invalid_alias = "invalid_alias_1_1_";
80-
NotificationParams params = new NotificationParams();
81-
params.setReceiverType(AudienceType.ALIAS);
82-
params.setReceiverValue(invalid_alias);
83-
PushResult result = jpushAndroid.sendNotification(MSG_CONTENT, params, null);
84-
85-
assertEquals(ErrorCodeEnum.NoTarget.value(), result.errcode);
86-
}
8719

8820
}
8921

0 commit comments

Comments
 (0)