Skip to content

Commit ac30b43

Browse files
authored
Merge pull request #79 from jpush/dev
android notification add alert_type
2 parents 25efbc3 + bdf97ed commit ac30b43

File tree

4 files changed

+84
-88
lines changed

4 files changed

+84
-88
lines changed

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

Lines changed: 57 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Map;
77

88
import cn.jiguang.common.ServiceHelper;
9+
import cn.jiguang.common.connection.ApacheHttpClient;
910
import cn.jiguang.common.connection.NativeHttpClient;
1011
import cn.jiguang.common.connection.NettyHttpClient;
1112
import cn.jiguang.common.resp.ResponseWrapper;
@@ -43,15 +44,16 @@ public class PushExample {
4344
public static final String MSG_CONTENT = "Test from API Example - msgContent";
4445
public static final String REGISTRATION_ID = "0900e8d85ef";
4546
public static final String TAG = "tag_api";
47+
public static long sendCount = 0;
48+
private static long sendTotalTime = 0;
4649

4750
public static void main(String[] args) {
4851
// testSendPushWithCustomConfig();
4952
// testSendIosAlert();
50-
// testSendPush();
51-
testSendPushes();
53+
testSendPush();
54+
// testSendPushes();
5255
// testSendPush_fromJSON();
5356
// testSendPushWithCallback();
54-
// testSendPushesWithMultiCallback();
5557
}
5658

5759
// 使用 NettyHttpClient 异步接口发送请求
@@ -74,96 +76,69 @@ public void onSucceed(ResponseWrapper responseWrapper) {
7476
}
7577
}
7678

77-
public static void testSendPushesWithMultiCallback() {
78-
NettyHttpClient client = new NettyHttpClient(ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET),
79-
null, ClientConfig.getInstance());
80-
String host = (String) ClientConfig.getInstance().get(ClientConfig.PUSH_HOST_NAME);
81-
URI uri = null;
82-
try {
83-
uri = new URI(host + (String) ClientConfig.getInstance().get(ClientConfig.PUSH_PATH));
84-
PushPayload payload = PushPayload.alertAll("test");
85-
System.out.println(payload.toString());
86-
NettyHttpClient.BaseCallback callback1 = new NettyHttpClient.BaseCallback() {
87-
@Override
88-
public void onSucceed(ResponseWrapper responseWrapper) {
89-
System.out.println("callback1 Got result: " + responseWrapper.responseContent);
90-
}
91-
};
92-
NettyHttpClient.BaseCallback callback2 = new NettyHttpClient.BaseCallback() {
93-
@Override
94-
public void onSucceed(ResponseWrapper responseWrapper) {
95-
System.out.println("callback2 Got result: " + responseWrapper.responseContent);
96-
}
97-
};
98-
MyThread thread1 = new MyThread(client, callback1);
99-
MyThread thread2 = new MyThread(client, callback2);
100-
thread1.start();
101-
thread2.start();
102-
} catch (URISyntaxException e) {
103-
e.printStackTrace();
104-
}
105-
}
106-
107-
private static class MyThread extends Thread {
108-
109-
private NettyHttpClient client;
110-
private NettyHttpClient.BaseCallback callback;
111-
112-
public MyThread(NettyHttpClient client, NettyHttpClient.BaseCallback callback) {
113-
this.client = client;
114-
this.callback = callback;
115-
}
116-
117-
@Override
118-
public void run() {
119-
// super.run();
120-
System.out.println("running send push");
121-
try {
122-
String host = (String) ClientConfig.getInstance().get(ClientConfig.PUSH_HOST_NAME);
123-
URI uri = new URI(host + (String) ClientConfig.getInstance().get(ClientConfig.PUSH_PATH));
124-
PushPayload payload = PushPayload.alertAll("test");
125-
System.out.println(payload.toString());
126-
client.sendRequest(HttpMethod.POST, payload.toString(), uri, callback);
127-
} catch (URISyntaxException e) {
128-
e.printStackTrace();
129-
}
130-
}
131-
}
132-
133-
13479
public static void testSendPush() {
13580
// HttpProxy proxy = new HttpProxy("localhost", 3128);
13681
// Can use this https proxy: https://github.com/Exa-Networks/exaproxy
13782
ClientConfig clientConfig = ClientConfig.getInstance();
138-
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
83+
final JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
13984
String authCode = ServiceHelper.getBasicAuthorization(APP_KEY, MASTER_SECRET);
14085
// Here you can use NativeHttpClient or NettyHttpClient.
14186
NativeHttpClient httpClient = new NativeHttpClient(authCode, null, clientConfig);
14287
// Call setHttpClient to set httpClient,
14388
// If you don't invoke this method, default httpClient will use NativeHttpClient.
89+
// ApacheHttpClient httpClient = new ApacheHttpClient(authCode, null, clientConfig);
14490
jpushClient.getPushClient().setHttpClient(httpClient);
145-
146-
147-
// For push, all you need do is to build PushPayload object.
148-
PushPayload payload = buildPushObject_all_alias_alert();
149-
try {
150-
PushResult result = jpushClient.sendPush(payload);
151-
LOG.info("Got result - " + result);
152-
// 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
153-
// If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
154-
// jpushClient.close();
155-
} catch (APIConnectionException e) {
156-
LOG.error("Connection error. Should retry later. ", e);
157-
LOG.error("Sendno: " + payload.getSendno());
158-
159-
} catch (APIRequestException e) {
160-
LOG.error("Error response from JPush server. Should review and fix it. ", e);
161-
LOG.info("HTTP Status: " + e.getStatus());
162-
LOG.info("Error Code: " + e.getErrorCode());
163-
LOG.info("Error Message: " + e.getErrorMessage());
164-
LOG.info("Msg ID: " + e.getMsgId());
165-
LOG.error("Sendno: " + payload.getSendno());
91+
final PushPayload payload = buildPushObject_android_newly_support();
92+
for(int i=0;i<10;i++) {
93+
Thread thread = new Thread() {
94+
public void run() {
95+
for (int j = 0; j < 200; j++) {
96+
long start = System.currentTimeMillis();
97+
try {
98+
jpushClient.sendPush(payload);
99+
PushResult result = jpushClient.sendPush(payload);
100+
LOG.info("Got result - " + result);
101+
102+
} catch (APIConnectionException e) {
103+
LOG.error("Connection error. Should retry later. ", e);
104+
LOG.error("Sendno: " + payload.getSendno());
105+
106+
} catch (APIRequestException e) {
107+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
108+
LOG.info("HTTP Status: " + e.getStatus());
109+
LOG.info("Error Code: " + e.getErrorCode());
110+
LOG.info("Error Message: " + e.getErrorMessage());
111+
LOG.info("Msg ID: " + e.getMsgId());
112+
LOG.error("Sendno: " + payload.getSendno());
113+
}
114+
115+
System.out.println("耗时" + (System.currentTimeMillis() - start) + "毫秒 sendCount:" + (++sendCount));
116+
}
117+
}
118+
};
119+
thread.start();
166120
}
121+
122+
// // For push, all you need do is to build PushPayload object.
123+
// PushPayload payload = buildPushObject_all_alias_alert();
124+
// try {
125+
// PushResult result = jpushClient.sendPush(payload);
126+
// LOG.info("Got result - " + result);
127+
// // 如果使用 NettyHttpClient,需要手动调用 close 方法退出进程
128+
// // If uses NettyHttpClient, call close when finished sending request, otherwise process will not exit.
129+
// // jpushClient.close();
130+
// } catch (APIConnectionException e) {
131+
// LOG.error("Connection error. Should retry later. ", e);
132+
// LOG.error("Sendno: " + payload.getSendno());
133+
//
134+
// } catch (APIRequestException e) {
135+
// LOG.error("Error response from JPush server. Should review and fix it. ", e);
136+
// LOG.info("HTTP Status: " + e.getStatus());
137+
// LOG.info("Error Code: " + e.getErrorCode());
138+
// LOG.info("Error Message: " + e.getErrorMessage());
139+
// LOG.info("Msg ID: " + e.getMsgId());
140+
// LOG.error("Sendno: " + payload.getSendno());
141+
// }
167142
}
168143

169144
//use String to build PushPayload instance

pom.xml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,21 @@
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.17</tag>
38+
<tag>v3.2.18</tag>
3939
</scm>
4040

4141
<dependencies>
4242
<dependency>
4343
<groupId>cn.jpush.api</groupId>
4444
<artifactId>jiguang-common</artifactId>
45-
<version>1.0.3</version>
45+
<version>1.0.5</version>
4646
</dependency>
47+
<dependency>
48+
<groupId>org.apache.httpcomponents</groupId>
49+
<artifactId>httpclient</artifactId>
50+
<version>4.5.3</version>
51+
<scope>compile</scope>
52+
</dependency>
4753
<dependency>
4854
<groupId>io.netty</groupId>
4955
<artifactId>netty-all</artifactId>

src/main/java/cn/jpush/api/push/model/notification/AndroidNotification.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,26 @@ public class AndroidNotification extends PlatformNotification {
1313
private static final String BUILDER_ID = "builder_id";
1414
private static final String INBOX = "inbox";
1515
private static final String STYLE = "style";
16+
private static final String ALERT_TYPE = "alert_type";
1617
private static final String BIG_TEXT = "big_text";
1718
private static final String BIG_PIC_PATH = "big_pic_path";
1819
private static final String PRIORITY = "priority";
1920
private static final String CATEGORY = "category";
2021

2122
private final String title;
2223
private final int builderId;
24+
// 0 ~ 4
2325
private int style = 0;
26+
// -1 ~ 7
27+
private int alert_type = -1;
2428
private String big_text;
2529
private Object inbox;
2630
private String big_pic_path;
2731
private int priority;
2832
private String category;
2933

30-
private AndroidNotification(Object alert, String title, int builderId, int style, String bigText, Object inbox,
31-
String bigPicPath, int priority, String category,
34+
private AndroidNotification(Object alert, String title, int builderId, int style, int alertType, String bigText,
35+
Object inbox, String bigPicPath, int priority, String category,
3236
Map<String, String> extras,
3337
Map<String, Number> numberExtras,
3438
Map<String, Boolean> booleanExtras,
@@ -38,6 +42,7 @@ private AndroidNotification(Object alert, String title, int builderId, int style
3842
this.title = title;
3943
this.builderId = builderId;
4044
this.style = style;
45+
this.alert_type = alertType;
4146
this.big_text = bigText;
4247
this.inbox = inbox;
4348
this.big_pic_path = bigPicPath;
@@ -83,6 +88,10 @@ public JsonElement toJSON() {
8388
json.add(STYLE, new JsonPrimitive(this.style));
8489
}
8590

91+
if (-1 != alert_type) {
92+
json.add(ALERT_TYPE, new JsonPrimitive(this.alert_type));
93+
}
94+
8695
if (null != big_text) {
8796
json.add(BIG_TEXT, new JsonPrimitive(this.big_text));
8897
}
@@ -114,6 +123,7 @@ public static class Builder extends PlatformNotification.Builder<AndroidNotifica
114123
private String title;
115124
private int builderId;
116125
private int style = 0;
126+
private int alert_type;
117127
private String big_text;
118128
private Object inbox;
119129
private String big_pic_path;
@@ -144,6 +154,11 @@ public Builder setStyle(int style) {
144154
return this;
145155
}
146156

157+
public Builder setAlertType(int alertType) {
158+
this.alert_type = alertType;
159+
return this;
160+
}
161+
147162
public Builder setBigText(String bigText) {
148163
this.big_text = bigText;
149164
return this;
@@ -175,7 +190,7 @@ public Builder setInbox(Object inbox) {
175190

176191

177192
public AndroidNotification build() {
178-
return new AndroidNotification(alert, title, builderId, style, big_text, inbox, big_pic_path, priority,
193+
return new AndroidNotification(alert, title, builderId, style, alert_type, big_text, inbox, big_pic_path, priority,
179194
category, extrasBuilder, numberExtrasBuilder, booleanExtrasBuilder, jsonExtrasBuilder);
180195
}
181196
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class PushClientTest extends BaseTest {
3333
public void testSendPush() {
3434
ClientConfig clientConfig = ClientConfig.getInstance();
3535
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
36-
PushPayload payload = PushPayload.alertAll(ALERT);
36+
PushPayload payload = buildPushObject_all_alias_alert();
3737
try {
3838
PushResult result = jpushClient.sendPush(payload);
3939
int status = result.getResponseCode();

0 commit comments

Comments
 (0)