Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 27 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@

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

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


## 安装

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

```
```Java
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.6</version>
<version>3.2.7</version>
</dependency>
```
### jar 包方式
Expand All @@ -40,31 +42,29 @@

如果使用 Maven 构建项目,则需要在你的项目 pom.xml 里增加:

```
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

<!-- For log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>


```Java
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.5</version>
</dependency>

<!-- For log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.16</version>
</dependency>
```

如果不使用 Maven 构建项目,则项目 libs/ 目录下有依赖的 jar 可复制到你的项目里去。
Expand Down
24 changes: 22 additions & 2 deletions example/main/java/cn/jpush/api/examples/DevcieExample.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package cn.jpush.api.examples;

import cn.jpush.api.device.OnlineStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -8,11 +9,13 @@
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.device.TagAliasResult;

import java.util.Map;

public class DevcieExample {
protected static final Logger LOG = LoggerFactory.getLogger(DevcieExample.class);

private static final String appKey = "dd1066407b044738b6479275";
private static final String masterSecret = "2b38ce69b1de2a7fa95706ea";
private static final String masterSecret = "6b135be0037a5c1e693c3dfa";
private static final String TAG1 = "tag1";
private static final String ALIAS1 = "alias1";
private static final String ALIAS2 = "alias2";
Expand All @@ -22,7 +25,8 @@ public class DevcieExample {
private static JPushClient jpushClient = new JPushClient(masterSecret, appKey);

public static void main(String[] args) {
testGetDeviceTagAlias();
// testGetDeviceTagAlias();
testGetUserOnlineStatus();
}

public static void testGetDeviceTagAlias() {
Expand All @@ -42,6 +46,22 @@ public static void testGetDeviceTagAlias() {
LOG.info("Error Message: " + e.getErrorMessage());
}
}

public static void testGetUserOnlineStatus() {
try {
Map<String, OnlineStatus> result = jpushClient.getUserOnlineStatus(REGISTRATION_ID1, REGISTRATION_ID2);

LOG.info(result.get(REGISTRATION_ID1).toString());
LOG.info(result.get(REGISTRATION_ID2).toString());
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}

}

Expand Down
26 changes: 25 additions & 1 deletion example/main/java/cn/jpush/api/examples/PushExample.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cn.jpush.api.examples;

import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.push.model.notification.IosAlert;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -18,6 +19,8 @@
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;

import java.util.HashMap;

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

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

public static void main(String[] args) {
testSendPushWithCustomConfig();
// testSendPushWithCustomConfig();
testSendIosAlert();
}


Expand Down Expand Up @@ -154,5 +158,25 @@ public static void testSendPushWithCustomConfig() {
}
}

public static void testSendIosAlert() {
JPushClient jpushClient = new JPushClient(masterSecret, appKey);

IosAlert alert = IosAlert.newBuilder()
.setTitleAndBody("test alert", "test ios alert json")
.setActionLocKey("PLAY")
.build();
try {
PushResult result = jpushClient.sendIosNotificationWithAlias(alert, new HashMap<String, String>(), "alias1");
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
} catch (APIRequestException e) {
LOG.error("Error response from JPush server. Should review and fix it. ", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
}
}

}

9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.6-SNAPSHOT</version>
<version>3.2.7-SNAPSHOT</version>
<packaging>jar</packaging>
<url>https://github.com/jpush/jpush-api-java-client</url>
<name>JPush API Java Client</name>
Expand Down Expand Up @@ -35,7 +35,7 @@
<url>https://github.com/jpush/jpush-api-java-client</url>
<connection>scm:git:git@github.com:jpush/jpush-api-java-client.git</connection>
<developerConnection>scm:git:git@github.com:jpush/jpush-api-java-client.git</developerConnection>
<tag>v3.2.6</tag>
<tag>v3.2.7</tag>
</scm>

<dependencies>
Expand Down Expand Up @@ -143,7 +143,6 @@
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<configuration>
<encoding>UTF-8</encoding>
<groups>cn.jpush.api.FastTests</groups>
<argLine>-Dfile.encoding=UTF-8</argLine>
<excludes>
Expand Down Expand Up @@ -200,7 +199,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>2.7</version>
<version>2.8</version>
<reportSets>
<reportSet>
<reports>
Expand All @@ -214,7 +213,7 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.9.1</version>
<version>2.10.3</version>
<configuration>
<overview>resources/javadoc-overview.html</overview>
</configuration>
Expand Down
62 changes: 55 additions & 7 deletions src/main/java/cn/jpush/api/JPushClient.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package cn.jpush.api;

import java.util.Map;
import java.util.Set;

import cn.jpush.api.common.ClientConfig;
import cn.jpush.api.common.TimeUnit;
import cn.jpush.api.common.Week;
Expand All @@ -11,16 +8,14 @@
import cn.jpush.api.common.resp.APIRequestException;
import cn.jpush.api.common.resp.BooleanResult;
import cn.jpush.api.common.resp.DefaultResult;
import cn.jpush.api.device.AliasDeviceListResult;
import cn.jpush.api.device.DeviceClient;
import cn.jpush.api.device.TagAliasResult;
import cn.jpush.api.device.TagListResult;
import cn.jpush.api.device.*;
import cn.jpush.api.push.PushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Message;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosAlert;
import cn.jpush.api.push.model.notification.Notification;
import cn.jpush.api.report.MessagesResult;
import cn.jpush.api.report.ReceivedsResult;
Expand All @@ -33,6 +28,9 @@
import cn.jpush.api.schedule.model.TriggerPayload;
import cn.jpush.api.utils.Preconditions;

import java.util.Map;
import java.util.Set;

/**
* The global entrance of JPush API library.
*/
Expand Down Expand Up @@ -249,6 +247,28 @@ public PushResult sendIosNotificationWithAlias(String alert,
.build();
return _pushClient.sendPush(payload);
}

/**
* Send an iOS notification with alias.
* If you want to send alert as a Json object, maybe this method is what you needed.
*
* @param alert The wrapper of APNs alert.
* @param extras The extra params.
* @param alias The alias list.
* @return
* @throws APIConnectionException
* @throws APIRequestException
*/
public PushResult sendIosNotificationWithAlias(IosAlert alert,
Map<String, String> extras, String... alias)
throws APIConnectionException, APIRequestException {
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.ios(alert, extras))
.build();
return _pushClient.sendPush(payload);
}

/**
* Shortcut
Expand All @@ -264,6 +284,28 @@ public PushResult sendIosNotificationWithRegistrationID(String alert,
return _pushClient.sendPush(payload);
}

/**
* Send an iOS notification with registrationIds.
* If you want to send alert as a Json object, maybe this method is what you needed.
*
* @param alert The wrapper of APNs alert.
* @param extras The extra params.
* @param registrationID The registration ids.
* @return
* @throws APIConnectionException
* @throws APIRequestException
*/
public PushResult sendIosNotificationWithRegistrationID(IosAlert alert,
Map<String, String> extras, String... registrationID)
throws APIConnectionException, APIRequestException {
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.registrationId(registrationID))
.setNotification(Notification.ios(alert, extras))
.build();
return _pushClient.sendPush(payload);
}


// ---------------------- shortcuts - message

Expand Down Expand Up @@ -407,6 +449,12 @@ public DefaultResult deleteAlias(String alias, String platform)
return _deviceClient.deleteAlias(alias, platform);
}

public Map<String, OnlineStatus> getUserOnlineStatus(String... registrationIds)
throws APIConnectionException, APIRequestException
{
return _deviceClient.getUserOnlineStatus(registrationIds);
}

// ----------------------- Schedule

/**
Expand Down
Loading