Skip to content

Commit 2d8ac18

Browse files
authored
Merge pull request #75 from jpush/dev
Dev
2 parents f54570b + c103c1d commit 2d8ac18

File tree

9 files changed

+311
-96
lines changed

9 files changed

+311
-96
lines changed

README.md

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<dependency>
2727
<groupId>cn.jpush.api</groupId>
2828
<artifactId>jpush-client</artifactId>
29-
<version>3.2.11</version>
29+
<version>3.2.15</version>
3030
</dependency>
3131
```
3232
### jar 包方式
@@ -46,17 +46,13 @@
4646
<dependency>
4747
<groupId>cn.jpush.api</groupId>
4848
<artifactId>jiguang-common</artifactId>
49-
<version>0.1.6</version>
50-
<exclusions>
51-
<exclusion>
52-
<groupId>org.slf4j</groupId>
53-
<artifactId>slf4j-jdk14</artifactId>
54-
</exclusion>
55-
<exclusion>
56-
<groupId>org.slf4j</groupId>
57-
<artifactId>slf4j-nop</artifactId>
58-
</exclusion>
59-
</exclusions>
49+
<version>1.0.1</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>io.netty</groupId>
53+
<artifactId>netty-all</artifactId>
54+
<version>4.1.6.Final</version>
55+
<scope>compile</scope>
6056
</dependency>
6157
<dependency>
6258
<groupId>com.google.code.gson</groupId>
@@ -118,8 +114,7 @@
118114
> 以下片断来自项目代码里的文件:example / cn.jpush.api.examples.PushExample
119115
120116
```Java
121-
ClientConfig clientConfig = ClientConfig.getInstance();
122-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
117+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
123118

124119
// For push, all you need do is to build PushPayload object.
125120
PushPayload payload = buildPushObject_all_all_alert();

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class DeviceExample {
2626
private static JPushClient jpushClient = new JPushClient(masterSecret, appKey);
2727

2828
public static void main(String[] args) {
29-
// testGetDeviceTagAlias();
29+
testGetDeviceTagAlias();
3030
// testGetUserOnlineStatus();
3131
}
3232

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

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package cn.jpush.api.examples;
22

3+
import java.net.URI;
4+
import java.net.URISyntaxException;
35
import java.util.HashMap;
46
import java.util.Map;
57

8+
import cn.jiguang.common.ServiceHelper;
9+
import cn.jiguang.common.connection.NettyHttpClient;
10+
import cn.jiguang.common.resp.ResponseWrapper;
611
import cn.jpush.api.push.model.notification.*;
7-
import com.google.gson.Gson;
8-
import com.google.gson.GsonBuilder;
12+
import com.google.gson.*;
13+
import io.netty.handler.codec.http.HttpMethod;
914
import org.slf4j.Logger;
1015
import org.slf4j.LoggerFactory;
1116

@@ -21,14 +26,16 @@
2126
import cn.jpush.api.push.model.SMS;
2227
import cn.jpush.api.push.model.audience.Audience;
2328
import cn.jpush.api.push.model.audience.AudienceTarget;
24-
import com.google.gson.JsonObject;
2529

2630
public class PushExample {
2731
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
2832

2933
// demo App defined in resources/jpush-api.conf
3034
private static final String appKey ="dd1066407b044738b6479275";
3135
private static final String masterSecret = "e8cc9a76d5b7a580859bcfa7";
36+
37+
protected static final String APP_KEY ="d4ee2375846bc30fa51334f5";
38+
protected static final String MASTER_SECRET = "2bf52ee46fdeaadb8718fc15";
3239

3340
public static final String TITLE = "Test from API example";
3441
public static final String ALERT = "Test from API Example - alert";
@@ -39,32 +46,109 @@ public class PushExample {
3946
public static void main(String[] args) {
4047
// testSendPushWithCustomConfig();
4148
// testSendIosAlert();
42-
// testSendPush();
43-
testSendPush_fromJSON();
49+
testSendPush();
50+
// testSendPush_fromJSON();
51+
// testSendPushes();
52+
// testSendPushesWithMultiCallback();
4453
}
54+
55+
// 使用 NettyHttpClient 异步接口发送请求
56+
public static void testSendPushes() {
57+
ClientConfig clientConfig = ClientConfig.getInstance();
58+
NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET),
59+
null, clientConfig);
60+
for (int i = 0; i < 4; i++) {
61+
NettyHttpClient.BaseCallback callback = new NettyHttpClient.BaseCallback() {
62+
@Override
63+
public void onSucceed(ResponseWrapper responseWrapper) {
64+
System.out.println("callback i");
65+
}
66+
};
67+
MyThread thread = new MyThread(client, callback);
68+
thread.start();
69+
}
70+
}
71+
72+
public static void testSendPushesWithMultiCallback() {
73+
NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET),
74+
null, ClientConfig.getInstance());
75+
String host = (String) ClientConfig.getInstance().get(ClientConfig.PUSH_HOST_NAME);
76+
URI uri = null;
77+
try {
78+
uri = new URI(host + (String) ClientConfig.getInstance().get(ClientConfig.PUSH_PATH));
79+
PushPayload payload = PushPayload.alertAll("test");
80+
System.out.println(payload.toString());
81+
NettyHttpClient.BaseCallback callback1 = new NettyHttpClient.BaseCallback() {
82+
@Override
83+
public void onSucceed(ResponseWrapper responseWrapper) {
84+
System.out.println("callback1 Got result: " + responseWrapper.responseContent);
85+
}
86+
};
87+
NettyHttpClient.BaseCallback callback2 = new NettyHttpClient.BaseCallback() {
88+
@Override
89+
public void onSucceed(ResponseWrapper responseWrapper) {
90+
System.out.println("callback2 Got result: " + responseWrapper.responseContent);
91+
}
92+
};
93+
MyThread thread1 = new MyThread(client, callback1);
94+
MyThread thread2 = new MyThread(client, callback2);
95+
thread1.start();
96+
thread2.start();
97+
} catch (URISyntaxException e) {
98+
e.printStackTrace();
99+
}
100+
}
101+
102+
private static class MyThread extends Thread {
103+
104+
private NettyHttpClient client;
105+
private NettyHttpClient.BaseCallback callback;
106+
107+
public MyThread(NettyHttpClient client, NettyHttpClient.BaseCallback callback) {
108+
this.client = client;
109+
this.callback = callback;
110+
}
111+
112+
@Override
113+
public void run() {
114+
// super.run();
115+
System.out.println("running send push");
116+
try {
117+
String host = (String) ClientConfig.getInstance().get(ClientConfig.PUSH_HOST_NAME);
118+
URI uri = new URI(host + (String) ClientConfig.getInstance().get(ClientConfig.PUSH_PATH));
119+
PushPayload payload = PushPayload.alertAll("test");
120+
System.out.println(payload.toString());
121+
client.sendRequest(HttpMethod.POST, payload.toString(), uri, callback);
122+
} catch (URISyntaxException e) {
123+
e.printStackTrace();
124+
}
125+
}
126+
}
45127

46128

47129
public static void testSendPush() {
48130
// HttpProxy proxy = new HttpProxy("localhost", 3128);
49131
// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
50132
ClientConfig clientConfig = ClientConfig.getInstance();
51-
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
133+
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
52134

53135
// For push, all you need do is to build PushPayload object.
54-
PushPayload payload = buildPushObject_ios_tagAnd_alertWithExtrasAndMessage();
136+
PushPayload payload = buildPushObject_android_newly_support();
55137
try {
56138
PushResult result = jpushClient.sendPush(payload);
57139
LOG.info("Got result - " + result);
58140

59141
} catch (APIConnectionException e) {
60142
LOG.error("Connection error. Should retry later. ", e);
143+
LOG.error("Sendno: " + payload.getSendno());
61144

62145
} catch (APIRequestException e) {
63146
LOG.error("Error response from JPush server. Should review and fix it. ", e);
64147
LOG.info("HTTP Status: " + e.getStatus());
65148
LOG.info("Error Code: " + e.getErrorCode());
66149
LOG.info("Error Message: " + e.getErrorMessage());
67150
LOG.info("Msg ID: " + e.getMsgId());
151+
LOG.error("Sendno: " + payload.getSendno());
68152
}
69153
}
70154

@@ -84,13 +168,15 @@ public static void testSendPush_fromJSON() {
84168

85169
} catch (APIConnectionException e) {
86170
LOG.error("Connection error. Should retry later. ", e);
171+
LOG.error("Sendno: " + payload.getSendno());
87172

88173
} catch (APIRequestException e) {
89174
LOG.error("Error response from JPush server. Should review and fix it. ", e);
90175
LOG.info("HTTP Status: " + e.getStatus());
91176
LOG.info("Error Code: " + e.getErrorCode());
92177
LOG.info("Error Message: " + e.getErrorMessage());
93178
LOG.info("Msg ID: " + e.getMsgId());
179+
LOG.error("Sendno: " + payload.getSendno());
94180
}
95181
}
96182

@@ -178,6 +264,33 @@ public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage()
178264
.build())
179265
.build();
180266
}
267+
268+
public static PushPayload buildPushObject_android_newly_support() {
269+
JsonObject inbox = new JsonObject();
270+
inbox.add("line1", new JsonPrimitive("line1 string"));
271+
inbox.add("line2", new JsonPrimitive("line2 string"));
272+
inbox.add("contentTitle", new JsonPrimitive("title string"));
273+
inbox.add("summaryText", new JsonPrimitive("+3 more"));
274+
Notification notification = Notification.newBuilder()
275+
.addPlatformNotification(AndroidNotification.newBuilder()
276+
.setAlert(ALERT)
277+
.setBigPicPath("path to big picture")
278+
.setBigText("long text")
279+
.setBuilderId(1)
280+
.setCategory("CATEGORY_SOCIAL")
281+
.setInbox(inbox)
282+
.setStyle(1)
283+
.setTitle("Alert test")
284+
.setPriority(1)
285+
.build())
286+
.build();
287+
return PushPayload.newBuilder()
288+
.setPlatform(Platform.all())
289+
.setAudience(Audience.registrationId("18071adc030dcba91c0"))
290+
.setNotification(notification)
291+
.setOptions(Options.sendno())
292+
.build();
293+
}
181294

182295
public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
183296
return PushPayload.newBuilder()

pom.xml

Lines changed: 16 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
<groupId>cn.jpush.api</groupId>
55
<artifactId>jpush-client</artifactId>
6-
<version>3.2.12-SNAPSHOT</version>
6+
<version>3.2.15-SNAPSHOT</version>
77
<packaging>jar</packaging>
88
<url>https://github.com/jpush/jpush-api-java-client</url>
99
<name>JPush API Java Client</name>
@@ -35,26 +35,22 @@
3535
<url>https://github.com/jpush/jpush-api-java-client</url>
3636
<connection>scm:git:[email protected]:jpush/jpush-api-java-client.git</connection>
3737
<developerConnection>scm:git:[email protected]:jpush/jpush-api-java-client.git</developerConnection>
38-
<tag>v3.2.11</tag>
38+
<tag>v3.2.15</tag>
3939
</scm>
4040

4141
<dependencies>
42-
<dependency>
43-
<groupId>cn.jpush.api</groupId>
44-
<artifactId>jiguang-common</artifactId>
45-
<version>0.1.6</version>
46-
<exclusions>
47-
<exclusion>
48-
<groupId>org.slf4j</groupId>
49-
<artifactId>slf4j-jdk14</artifactId>
50-
</exclusion>
51-
<exclusion>
52-
<groupId>org.slf4j</groupId>
53-
<artifactId>slf4j-nop</artifactId>
54-
</exclusion>
55-
</exclusions>
56-
</dependency>
57-
<dependency>
42+
<dependency>
43+
<groupId>cn.jpush.api</groupId>
44+
<artifactId>jiguang-common</artifactId>
45+
<version>1.0.1</version>
46+
</dependency>
47+
<dependency>
48+
<groupId>io.netty</groupId>
49+
<artifactId>netty-all</artifactId>
50+
<version>4.1.6.Final</version>
51+
<scope>compile</scope>
52+
</dependency>
53+
<dependency>
5854
<groupId>com.google.code.gson</groupId>
5955
<artifactId>gson</artifactId>
6056
<version>2.3</version>
@@ -118,7 +114,7 @@
118114
<plugin>
119115
<groupId>org.apache.maven.plugins</groupId>
120116
<artifactId>maven-release-plugin</artifactId>
121-
<version>2.5.3</version>
117+
<version>2.5.1</version>
122118
<dependencies>
123119
<dependency>
124120
<groupId>org.apache.maven.plugins</groupId>
@@ -236,4 +232,5 @@
236232
</plugins>
237233
</reporting>
238234

235+
239236
</project>

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

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

3+
import cn.jiguang.common.connection.NettyHttpClient;
34
import com.google.gson.JsonParseException;
45
import com.google.gson.JsonParser;
56

@@ -27,7 +28,7 @@
2728
*/
2829
public class PushClient {
2930

30-
private final NativeHttpClient _httpClient;
31+
private final NettyHttpClient _httpClient;
3132
private String _baseUrl;
3233
private String _pushPath;
3334
private String _pushValidatePath;
@@ -85,7 +86,7 @@ public PushClient(String masterSecret, String appKey, int maxRetryTimes, HttpPro
8586
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);
8687

8788
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
88-
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
89+
this._httpClient = new NettyHttpClient(authCode, proxy, conf);
8990
}
9091

9192
public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientConfig conf) {
@@ -99,7 +100,7 @@ public PushClient(String masterSecret, String appKey, HttpProxy proxy, ClientCon
99100
this._timeToLive = (Long) conf.get(ClientConfig.TIME_TO_LIVE);
100101

101102
String authCode = ServiceHelper.getBasicAuthorization(appKey, masterSecret);
102-
this._httpClient = new NativeHttpClient(authCode, proxy, conf);
103+
this._httpClient = new NettyHttpClient(authCode, proxy, conf);
103104

104105
}
105106

0 commit comments

Comments
 (0)