Skip to content

Commit c9452ca

Browse files
committed
Merge pull request #28 from jpush/dev
merge from dev
2 parents a0a0e0a + 43161f9 commit c9452ca

File tree

20 files changed

+471
-153
lines changed

20 files changed

+471
-153
lines changed

README.md

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,19 @@
1414

1515
版本更新:[Release页面](https://github.com/jpush/jpush-api-java-client/releases)。下载更新请到这里。
1616

17+
> 非常欢迎各位开发者提交代码,贡献一份力量,review过有效的代码将会合入本项目。
18+
1719

1820
## 安装
1921

2022
### maven 方式
2123
将下边的依赖条件放到你项目的 maven pom.xml 文件里。
2224

23-
```
25+
```Java
2426
<dependency>
2527
<groupId>cn.jpush.api</groupId>
2628
<artifactId>jpush-client</artifactId>
27-
<version>3.2.6</version>
29+
<version>3.2.7</version>
2830
</dependency>
2931
```
3032
### jar 包方式
@@ -40,31 +42,29 @@
4042
4143
如果使用 Maven 构建项目,则需要在你的项目 pom.xml 里增加:
4244

43-
```
44-
<dependency>
45-
<groupId>com.google.code.gson</groupId>
46-
<artifactId>gson</artifactId>
47-
<version>2.2.4</version>
48-
</dependency>
49-
<dependency>
50-
<groupId>org.slf4j</groupId>
51-
<artifactId>slf4j-api</artifactId>
52-
<version>1.7.5</version>
53-
</dependency>
54-
55-
<!-- For log4j -->
56-
<dependency>
57-
<groupId>org.slf4j</groupId>
58-
<artifactId>slf4j-log4j12</artifactId>
59-
<version>1.7.5</version>
60-
</dependency>
61-
<dependency>
62-
<groupId>log4j</groupId>
63-
<artifactId>log4j</artifactId>
64-
<version>1.2.16</version>
65-
</dependency>
66-
67-
45+
```Java
46+
<dependency>
47+
<groupId>com.google.code.gson</groupId>
48+
<artifactId>gson</artifactId>
49+
<version>2.2.4</version>
50+
</dependency>
51+
<dependency>
52+
<groupId>org.slf4j</groupId>
53+
<artifactId>slf4j-api</artifactId>
54+
<version>1.7.5</version>
55+
</dependency>
56+
57+
<!-- For log4j -->
58+
<dependency>
59+
<groupId>org.slf4j</groupId>
60+
<artifactId>slf4j-log4j12</artifactId>
61+
<version>1.7.5</version>
62+
</dependency>
63+
<dependency>
64+
<groupId>log4j</groupId>
65+
<artifactId>log4j</artifactId>
66+
<version>1.2.16</version>
67+
</dependency>
6868
```
6969

7070
如果不使用 Maven 构建项目,则项目 libs/ 目录下有依赖的 jar 可复制到你的项目里去。

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

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

3+
import cn.jpush.api.device.OnlineStatus;
34
import org.slf4j.Logger;
45
import org.slf4j.LoggerFactory;
56

@@ -8,11 +9,13 @@
89
import cn.jpush.api.common.resp.APIRequestException;
910
import cn.jpush.api.device.TagAliasResult;
1011

12+
import java.util.Map;
13+
1114
public class DevcieExample {
1215
protected static final Logger LOG = LoggerFactory.getLogger(DevcieExample.class);
1316

1417
private static final String appKey = "dd1066407b044738b6479275";
15-
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
18+
private static final String masterSecret = "6b135be0037a5c1e693c3dfa";
1619
private static final String TAG1 = "tag1";
1720
private static final String ALIAS1 = "alias1";
1821
private static final String ALIAS2 = "alias2";
@@ -22,7 +25,8 @@ public class DevcieExample {
2225
private static JPushClient jpushClient = new JPushClient(masterSecret, appKey);
2326

2427
public static void main(String[] args) {
25-
testGetDeviceTagAlias();
28+
// testGetDeviceTagAlias();
29+
testGetUserOnlineStatus();
2630
}
2731

2832
public static void testGetDeviceTagAlias() {
@@ -42,6 +46,22 @@ public static void testGetDeviceTagAlias() {
4246
LOG.info("Error Message: " + e.getErrorMessage());
4347
}
4448
}
49+
50+
public static void testGetUserOnlineStatus() {
51+
try {
52+
Map<String, OnlineStatus> result = jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2);
53+
54+
LOG.info(result.get(REGISTRATION_ID1).toString());
55+
LOG.info(result.get(REGISTRATION_ID2).toString());
56+
} catch (APIConnectionException e) {
57+
LOG.error("Connection error. Should retry later. ", e);
58+
} catch (APIRequestException e) {
59+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
60+
LOG.info("HTTP Status: " + e.getStatus());
61+
LOG.info("Error Code: " + e.getErrorCode());
62+
LOG.info("Error Message: " + e.getErrorMessage());
63+
}
64+
}
4565

4666
}
4767

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

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

33
import cn.jpush.api.common.ClientConfig;
4+
import cn.jpush.api.push.model.notification.IosAlert;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
67

@@ -18,6 +19,8 @@
1819
import cn.jpush.api.push.model.notification.IosNotification;
1920
import cn.jpush.api.push.model.notification.Notification;
2021

22+
import java.util.HashMap;
23+
2124
public class PushExample {
2225
protected static final Logger LOG = LoggerFactory.getLogger(PushExample.class);
2326

@@ -32,7 +35,8 @@ public class PushExample {
3235
public static final String TAG = "tag_api";
3336

3437
public static void main(String[] args) {
35-
testSendPushWithCustomConfig();
38+
// testSendPushWithCustomConfig();
39+
testSendIosAlert();
3640
}
3741

3842

@@ -154,5 +158,25 @@ public static void testSendPushWithCustomConfig() {
154158
}
155159
}
156160

161+
public static void testSendIosAlert() {
162+
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
163+
164+
IosAlert alert = IosAlert.newBuilder()
165+
.setTitleAndBody("test alert", "test ios alert json")
166+
.setActionLocKey("PLAY")
167+
.build();
168+
try {
169+
PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(), "alias1");
170+
LOG.info("Got result - " + result);
171+
} catch (APIConnectionException e) {
172+
LOG.error("Connection error. Should retry later. ", e);
173+
} catch (APIRequestException e) {
174+
LOG.error("Error response from JPush server. Should review and fix it. ", e);
175+
LOG.info("HTTP Status: " + e.getStatus());
176+
LOG.info("Error Code: " + e.getErrorCode());
177+
LOG.info("Error Message: " + e.getErrorMessage());
178+
}
179+
}
180+
157181
}
158182

pom.xml

Lines changed: 4 additions & 5 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.6-SNAPSHOT</version>
6+
<version>3.2.7-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,7 +35,7 @@
3535
<url>https://github.com/jpush/jpush-api-java-client</url>
3636
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
3737
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
38-
<tag>v3.2.6</tag>
38+
<tag>v3.2.7</tag>
3939
</scm>
4040

4141
<dependencies>
@@ -143,7 +143,6 @@
143143
<artifactId>maven-surefire-plugin</artifactId>
144144
<version>2.17</version>
145145
<configuration>
146-
<encoding>UTF-8</encoding>
147146
<groups>cn.jpush.api.FastTests</groups>
148147
<argLine>-Dfile.encoding=UTF-8</argLine>
149148
<excludes>
@@ -200,7 +199,7 @@
200199
<plugin>
201200
<groupId>org.apache.maven.plugins</groupId>
202201
<artifactId>maven-project-info-reports-plugin</artifactId>
203-
<version>2.7</version>
202+
<version>2.8</version>
204203
<reportSets>
205204
<reportSet>
206205
<reports>
@@ -214,7 +213,7 @@
214213
<plugin>
215214
<groupId>org.apache.maven.plugins</groupId>
216215
<artifactId>maven-javadoc-plugin</artifactId>
217-
<version>2.9.1</version>
216+
<version>2.10.3</version>
218217
<configuration>
219218
<overview>resources/javadoc-overview.html</overview>
220219
</configuration>

src/main/java/cn/jpush/api/JPushClient.java

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

3-
import java.util.Map;
4-
import java.util.Set;
5-
63
import cn.jpush.api.common.ClientConfig;
74
import cn.jpush.api.common.TimeUnit;
85
import cn.jpush.api.common.Week;
@@ -11,16 +8,14 @@
118
import cn.jpush.api.common.resp.APIRequestException;
129
import cn.jpush.api.common.resp.BooleanResult;
1310
import cn.jpush.api.common.resp.DefaultResult;
14-
import cn.jpush.api.device.AliasDeviceListResult;
15-
import cn.jpush.api.device.DeviceClient;
16-
import cn.jpush.api.device.TagAliasResult;
17-
import cn.jpush.api.device.TagListResult;
11+
import cn.jpush.api.device.*;
1812
import cn.jpush.api.push.PushClient;
1913
import cn.jpush.api.push.PushResult;
2014
import cn.jpush.api.push.model.Message;
2115
import cn.jpush.api.push.model.Platform;
2216
import cn.jpush.api.push.model.PushPayload;
2317
import cn.jpush.api.push.model.audience.Audience;
18+
import cn.jpush.api.push.model.notification.IosAlert;
2419
import cn.jpush.api.push.model.notification.Notification;
2520
import cn.jpush.api.report.MessagesResult;
2621
import cn.jpush.api.report.ReceivedsResult;
@@ -33,6 +28,9 @@
3328
import cn.jpush.api.schedule.model.TriggerPayload;
3429
import cn.jpush.api.utils.Preconditions;
3530

31+
import java.util.Map;
32+
import java.util.Set;
33+
3634
/**
3735
* The global entrance of JPush API library.
3836
*/
@@ -249,6 +247,28 @@ public PushResult sendIosNotificationWithAlias(String alert,
249247
.build();
250248
return _pushClient.sendPush(payload);
251249
}
250+
251+
/**
252+
* Send an iOS notification with alias.
253+
* If you want to send alert as a Json object, maybe this method is what you needed.
254+
*
255+
* @param alert The wrapper of APNs alert.
256+
* @param extras The extra params.
257+
* @param alias The alias list.
258+
* @return
259+
* @throws APIConnectionException
260+
* @throws APIRequestException
261+
*/
262+
public PushResult sendIosNotificationWithAlias(IosAlert alert,
263+
Map<String, String> extras, String... alias)
264+
throws APIConnectionException, APIRequestException {
265+
PushPayload payload = PushPayload.newBuilder()
266+
.setPlatform(Platform.ios())
267+
.setAudience(Audience.alias(alias))
268+
.setNotification(Notification.ios(alert, extras))
269+
.build();
270+
return _pushClient.sendPush(payload);
271+
}
252272

253273
/**
254274
* Shortcut
@@ -264,6 +284,28 @@ public PushResult sendIosNotificationWithRegistrationID(String alert,
264284
return _pushClient.sendPush(payload);
265285
}
266286

287+
/**
288+
* Send an iOS notification with registrationIds.
289+
* If you want to send alert as a Json object, maybe this method is what you needed.
290+
*
291+
* @param alert The wrapper of APNs alert.
292+
* @param extras The extra params.
293+
* @param registrationID The registration ids.
294+
* @return
295+
* @throws APIConnectionException
296+
* @throws APIRequestException
297+
*/
298+
public PushResult sendIosNotificationWithRegistrationID(IosAlert alert,
299+
Map<String, String> extras, String... registrationID)
300+
throws APIConnectionException, APIRequestException {
301+
PushPayload payload = PushPayload.newBuilder()
302+
.setPlatform(Platform.ios())
303+
.setAudience(Audience.registrationId(registrationID))
304+
.setNotification(Notification.ios(alert, extras))
305+
.build();
306+
return _pushClient.sendPush(payload);
307+
}
308+
267309

268310
// ---------------------- shortcuts - message
269311

@@ -407,6 +449,12 @@ public DefaultResult deleteAlias(String alias, String platform)
407449
return _deviceClient.deleteAlias(alias, platform);
408450
}
409451

452+
public Map<String, OnlineStatus> getUserOnlineStatus(String... registrationIds)
453+
throws APIConnectionException, APIRequestException
454+
{
455+
return _deviceClient.getUserOnlineStatus(registrationIds);
456+
}
457+
410458
// ----------------------- Schedule
411459

412460
/**

0 commit comments

Comments
 (0)