Skip to content

Commit 986154c

Browse files
author
x1a0d@n9
committed
release version
1 parent a56db79 commit 986154c

10 files changed

Lines changed: 75 additions & 83 deletions

.classpath

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
<classpath>
33
<classpathentry kind="src" path="src"/>
44
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
5-
<classpathentry kind="lib" path="lib/commons-codec-1.6.jar"/>
65
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
76
<classpathentry kind="lib" path="lib/jackson-all-1.9.4.jar"/>
8-
<classpathentry kind="lib" path="lib/testng-5.8-jdk15.jar"/>
97
<classpathentry kind="lib" path="lib/httpclient-4.2.2.jar"/>
108
<classpathentry kind="lib" path="lib/httpcore-4.2.2.jar"/>
11-
<classpathentry kind="lib" path="lib/httpmime-4.2.2.jar"/>
129
<classpathentry kind="output" path="bin"/>
1310
</classpath>

lib/commons-codec-1.6.jar

-227 KB
Binary file not shown.

lib/httpmime-4.2.2.jar

-26 KB
Binary file not shown.

lib/testng-5.8-jdk15.jar

-824 KB
Binary file not shown.

src/cn/jpush/api/CustomMessageParams.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
package cn.jpush.api;
22

3-
import java.io.IOException;
43
import java.util.Map;
5-
6-
import org.codehaus.jackson.JsonGenerationException;
7-
import org.codehaus.jackson.map.JsonMappingException;
84
import org.codehaus.jackson.map.ObjectMapper;
95

106
/*
@@ -44,11 +40,7 @@ public String toString() {
4440
String extraJson = new ObjectMapper().writeValueAsString(this.extra);
4541
buffer.append(",\"extra\":\"" + ((null != extraJson) ? "{}":"") + "\"");
4642
buffer.append("}");
47-
} catch (JsonGenerationException e) {
48-
e.printStackTrace();
49-
} catch (JsonMappingException e) {
50-
e.printStackTrace();
51-
} catch (IOException e) {
43+
} catch (Exception e) {
5244
e.printStackTrace();
5345
}
5446
return buffer.toString();

src/cn/jpush/api/ErrorCodeEnum.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cn.jpush.api;
22

33
public enum ErrorCodeEnum {
4+
//没有错误,发送成功
5+
NOERROR(0),
6+
47
//系统内部错误
58
SystemError(10),
69

src/cn/jpush/api/JPushClient.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,6 @@ protected MessageResult fromJson(final Object json) {
296296
if (null == json) {
297297
return null;
298298
}
299-
300299
return MessageResult.fromValue(String.valueOf(json));
301300
}
302301

@@ -323,11 +322,6 @@ protected String getHostname() {
323322
return this.hostname;
324323
}
325324

326-
public CallbackMessage getCallbackMessage() {
327-
//unimplements
328-
return null;
329-
}
330-
331325
public HttpEntity toEntity(MessageParams message) {
332326
String md5SecretKey = StringUtils.toMD5(this.getPassword());
333327
String input = this.getUsername() + message.getSendNo() + message.getReceiverType() + message.getReceiverValue() + md5SecretKey;

src/cn/jpush/api/MessageResult.java

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,17 +40,19 @@ public void setErrmsg(String errmsg) {
4040
public static MessageResult fromValue(String result) {
4141
MessageResult messageResult = null;
4242
try {
43-
@SuppressWarnings("unchecked")
44-
Map<String, Object> map = new ObjectMapper().readValue(result.toString(), Map.class);
45-
messageResult = new MessageResult();
46-
if (null != map.get("sendno")) {
47-
messageResult.setSendno(Integer.parseInt(String.valueOf(map.get("sendno"))));
48-
}
49-
if (null != map.get("errcode")) {
50-
messageResult.setErrcode(Integer.parseInt(String.valueOf(map.get("errcode"))));
51-
}
52-
if (null != map.get("errmsg")) {
53-
messageResult.setErrmsg(String.valueOf(map.get("errmsg")));
43+
if ( (null != result) && (!"".equals(result)) ) {
44+
@SuppressWarnings("unchecked")
45+
Map<String, Object> map = new ObjectMapper().readValue(result, Map.class);
46+
messageResult = new MessageResult();
47+
if (null != map.get("sendno")) {
48+
messageResult.setSendno(Integer.parseInt(String.valueOf(map.get("sendno"))));
49+
}
50+
if (null != map.get("errcode")) {
51+
messageResult.setErrcode(Integer.parseInt(String.valueOf(map.get("errcode"))));
52+
}
53+
if (null != map.get("errmsg")) {
54+
messageResult.setErrmsg(String.valueOf(map.get("errmsg")));
55+
}
5456
}
5557
} catch (JsonParseException e) {
5658
e.printStackTrace();
Lines changed: 58 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
package cn.jpush.test;
2-
3-
import org.testng.Assert;
4-
import org.testng.annotations.*;
1+
package cn.jpush.example;
52

63
import cn.jpush.api.CustomMessageParams;
74
import cn.jpush.api.DeviceEnum;
5+
import cn.jpush.api.ErrorCodeEnum;
86
import cn.jpush.api.JPushClient;
97
import cn.jpush.api.MessageResult;
108
import cn.jpush.api.NotifyMessageParams;
119
import cn.jpush.api.ReceiverTypeEnum;
1210

13-
public class JpushClientTest {
11+
public class JpushClientExample {
1412

15-
private final String username = "username";
16-
private final String password = "password";
17-
private final String callbackUrl = "url";
18-
private JPushClient client = null;
13+
private static final String username = "username";
14+
private static final String password = "password";
15+
private static final String callbackUrl = "";//没有的话,填写空字符串
16+
private static JPushClient client = null;
1917

20-
@BeforeTest
21-
public void init() {
18+
public static void main(String[] args) {
19+
//初始化
2220
client = new JPushClient(username, password, callbackUrl);
23-
}
24-
25-
@AfterTest
26-
public void shutdown() {
21+
22+
//发送消息或者通知
23+
testSendNotifyMessageWithAppKey();
24+
25+
//连接池概念,根据相应需求释放链接资源
2726
client.shutdown();
2827
}
2928

30-
@Test
31-
public void testSendNotificationWithImei() {
29+
public static void testSendNotificationWithImei() {
3230
String imei = "860949003474563";
3331
String appKey = "466f7032ac604e02fb7bda89";
3432
int sendNo = 1;
@@ -40,11 +38,11 @@ public void testSendNotificationWithImei() {
4038
msgTitle, msgContent,
4139
DeviceEnum.Android,
4240
DeviceEnum.IOS);
43-
Assert.assertEquals(msgResult.getErrcode(), 0);
41+
42+
dumpMessageResult(msgResult);
4443
}
4544

46-
@Test
47-
public void testSendCustomMessageWithImei() {
45+
public static void testSendCustomMessageWithImei() {
4846
String imei = "860949003474563";
4947
String appKey = "466f7032ac604e02fb7bda89";
5048
int sendNo = 1;
@@ -55,11 +53,11 @@ public void testSendCustomMessageWithImei() {
5553
appKey, sendNo, sendDescription,
5654
msgTitle, msgContent,
5755
DeviceEnum.Android);
58-
Assert.assertEquals(msgResult.getErrcode(), 0);
56+
57+
dumpMessageResult(msgResult);
5958
}
6059

61-
@Test
62-
public void testSendNotifyMessageWithAppKey() {
60+
public static void testSendNotifyMessageWithAppKey() {
6361
String appKey = "466f7032ac604e02fb7bda89";
6462
int sendNo = 1;
6563
String sendDescription = "title";
@@ -69,11 +67,11 @@ public void testSendNotifyMessageWithAppKey() {
6967
appKey, sendNo, sendDescription,
7068
msgTitle, msgContent,
7169
DeviceEnum.Android);
72-
Assert.assertEquals(msgResult.getErrcode(), 0);
70+
71+
dumpMessageResult(msgResult);
7372
}
7473

75-
@Test
76-
public void testSendCustomMessageWithAppKey() {
74+
public static void testSendCustomMessageWithAppKey() {
7775
String appKey = "466f7032ac604e02fb7bda89";
7876
int sendNo = 1;
7977
String sendDescription = "title";
@@ -83,11 +81,11 @@ public void testSendCustomMessageWithAppKey() {
8381
appKey, sendNo, sendDescription,
8482
msgTitle, msgContent,
8583
DeviceEnum.Android);
86-
Assert.assertEquals(msgResult.getErrcode(), 0);
84+
85+
dumpMessageResult(msgResult);
8786
}
8887

89-
@Test
90-
public void testSendNotifyMessageWithTag() {
88+
public static void testSendNotifyMessageWithTag() {
9189
String tag = "fruit";
9290
String appKey = "fa1fa9091f2e5c786a87ecae";
9391
int sendNo = 1;
@@ -98,11 +96,11 @@ public void testSendNotifyMessageWithTag() {
9896
appKey, sendNo, sendDescription,
9997
msgTitle, msgContent,
10098
DeviceEnum.Android);
101-
Assert.assertEquals(msgResult.getErrcode(), 0);
99+
100+
dumpMessageResult(msgResult);
102101
}
103102

104-
@Test
105-
public void testSendCustomMessageWithTag() {
103+
public static void testSendCustomMessageWithTag() {
106104
String tag = "fruit";
107105
String appKey = "fa1fa9091f2e5c786a87ecae";
108106
int sendNo = 1;
@@ -113,11 +111,11 @@ public void testSendCustomMessageWithTag() {
113111
appKey, sendNo, sendDescription,
114112
msgTitle, msgContent,
115113
DeviceEnum.Android);
116-
Assert.assertEquals(msgResult.getErrcode(), 0);
114+
115+
dumpMessageResult(msgResult);
117116
}
118117

119-
@Test
120-
public void testSendNotifyMessageWithAlias() {
118+
public static void testSendNotifyMessageWithAlias() {
121119
String alias = "alias1";
122120
String appKey = "fa1fa9091f2e5c786a87ecae";
123121
int sendNo = 1;
@@ -128,11 +126,11 @@ public void testSendNotifyMessageWithAlias() {
128126
appKey, sendNo, sendDescription,
129127
msgTitle, msgContent,
130128
DeviceEnum.Android);
131-
Assert.assertEquals(msgResult.getErrcode(), 0);
129+
130+
dumpMessageResult(msgResult);
132131
}
133132

134-
@Test
135-
public void testSendCustomMessageWithAlias() {
133+
public static void testSendCustomMessageWithAlias() {
136134
String alias = "alias1";
137135
String appKey = "fa1fa9091f2e5c786a87ecae";
138136
int sendNo = 1;
@@ -143,11 +141,11 @@ public void testSendCustomMessageWithAlias() {
143141
appKey, sendNo, sendDescription,
144142
msgTitle, msgContent,
145143
DeviceEnum.Android);
146-
Assert.assertEquals(msgResult.getErrcode(), 0);
144+
145+
dumpMessageResult(msgResult);
147146
}
148147

149-
@Test
150-
public void testSendNotifyMessage() {
148+
public static void testSendNotifyMessage() {
151149
NotifyMessageParams params = new NotifyMessageParams();
152150
params.setAppKeys("466f7032ac604e02fb7bda89");
153151
params.setSendNo(1);
@@ -161,12 +159,13 @@ public void testSendNotifyMessage() {
161159
params.getMsgContent().setBuilderId("0");
162160

163161
MessageResult msgResult = client.sendNotification(params);
164-
Assert.assertEquals(msgResult.getErrcode(), 0);
162+
163+
dumpMessageResult(msgResult);
165164
}
166165

167-
@Test
168-
public void testSendCustomMessage() {
166+
public static void testSendCustomMessage() {
169167
CustomMessageParams params = new CustomMessageParams();
168+
//请不要省略以下参数
170169
params.setAppKeys("466f7032ac604e02fb7bda89");
171170
params.setSendNo(1);
172171
params.setSendDescription("description");
@@ -177,9 +176,23 @@ public void testSendCustomMessage() {
177176
params.getMsgContent().setTitle("notification title");
178177
params.getMsgContent().setMessage("notification message");
179178
params.getMsgContent().setContentType("0");
179+
//extra是推送给客户端的json数据,开发者根据自己的需求定义
180180
params.getMsgContent().setExtra(new java.util.HashMap<String, Object>());
181181

182182
MessageResult msgResult = client.sendCustomMessage(params);
183-
Assert.assertEquals(msgResult.getErrcode(), 0);
183+
184+
dumpMessageResult(msgResult);
185+
}
186+
187+
private static void dumpMessageResult(MessageResult msgResult) {
188+
if (null != msgResult) {
189+
if (msgResult.getErrcode() == ErrorCodeEnum.NOERROR.value()) {
190+
System.out.println("发送成功, sendNo=" + msgResult.getSendno());
191+
} else {
192+
System.out.println("发送失败, 错误代码=" + msgResult.getErrcode() + ", 错误消息=" + msgResult.getErrmsg());
193+
}
194+
} else {
195+
System.out.println("无法获取服务器返回的数据");
196+
}
184197
}
185198
}

testng.xml

Lines changed: 0 additions & 9 deletions
This file was deleted.

0 commit comments

Comments
 (0)