在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:jpush/jpush-api-java-client开源软件地址:https://github.com/jpush/jpush-api-java-client开源编程语言:Java 100.0%开源软件介绍:JPush API Java Library概述这是 JPush REST API 的 Java 版本封装开发包,是由极光推送官方提供的,一般支持最新的 API 功能。 对应的 REST API 文档:REST API - Push, REST API - Report. 版本更新:Release页面。下载更新请到这里。
安装依赖包
如果使用 Maven 构建项目,则需要在你的项目 pom.xml 里增加: <dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.11</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.3</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.7</version>
</dependency>
<!-- For log4j -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.7</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency> 如果不使用 Maven 构建项目,则项目 libs/ 目录下有依赖的 jar 可复制到你的项目里去。 编译源码
导入本项目
构建本项目可以用 Eclipse 类 IDE 导出 jar 包。建议直接使用 maven,执行命令:
自动化测试在项目目录下执行命令:
使用样例如果使用 NettyHttpClient(v3.2.15 版本新增),需要在响应返回后手动调用一下 NettyHttpClient 中的 close 方法,否则进程不会退出。代码示例:
3.2.17 版本后,在 PushClient 中添加了 setHttpClient(IHttpClient client) 方法,用户可以自由切换 ApacheHttpClient,NettyHttpClient 或是 NativeHttpClient。 推送样例
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ClientConfig.getInstance());
// For push, all you need do is to build PushPayload object.
PushPayload payload = buildPushObject_all_all_alert();
try {
PushResult result = jpushClient.sendPush(payload);
LOG.info("Got result - " + result);
} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
} 进行推送的关键在于构建一个 PushPayload 对象。以下示例一般的构建对象的用法。
public static PushPayload buildPushObject_all_all_alert() {
return PushPayload.alertAll(ALERT);
}
public static PushPayload buildPushObject_all_alias_alert() {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.alias("alias1"))
.setNotification(Notification.alert(ALERT))
.build();
}
public static PushPayload buildPushObject_android_tag_alertWithTitle() {
return PushPayload.newBuilder()
.setPlatform(Platform.android())
.setAudience(Audience.tag("tag1"))
.setNotification(Notification.android(ALERT, TITLE, null))
.build();
}
public static PushPayload buildPushObject_ios_tagAnd_alertWithExtrasAndMessage() {
return PushPayload.newBuilder()
.setPlatform(Platform.ios())
.setAudience(Audience.tag_and("tag1", "tag_all"))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(ALERT)
.setBadge(5)
.setSound("happy")
.addExtra("from", "JPush")
.build())
.build())
.setMessage(Message.content(MSG_CONTENT))
.setOptions(Options.newBuilder()
.setApnsProduction(true)
.build())
.build();
}
public static PushPayload buildPushObject_ios_audienceMore_messageWithExtras() {
return PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.newBuilder()
.addAudienceTarget(AudienceTarget.tag("tag1", "tag2"))
.addAudienceTarget(AudienceTarget.alias("alias1", "alias2"))
.build())
.setMessage(Message.newBuilder()
.setMsgContent(MSG_CONTENT)
.addExtra("from", "JPush")
.build())
.build();
}
public static void testSendWithSMS() {
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
try {
SMS sms = SMS.newBuilder()
.setDelayTime(1000)
.setTempID(2000)
.addPara("Test", 1)
.build();
PushResult result = jpushClient.sendAndroidMessageWithAlias("Test SMS", "test sms", sms, "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());
}
} 统计获取样例
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
try {
ReceivedsResult result = jpushClient.getReportReceiveds("1942377665");
LOG.debug("Got result - " + result);
} catch (APIConnectionException e) {
// Connection error, should retry later
LOG.error("Connection error, should retry later", e);
} catch (APIRequestException e) {
// Should review the error, and fix the request
LOG.error("Should review the error, and fix the request", e);
LOG.info("HTTP Status: " + e.getStatus());
LOG.info("Error Code: " + e.getErrorCode());
LOG.info("Error Message: " + e.getErrorMessage());
} Tag/Alias 样例
try {
TagAliasResult result = jpushClient.getDeviceTagAlias(REGISTRATION_ID1);
LOG.info(result.alias);
LOG.info(result.tags.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());
}
try {
DefaultResult result = jpushClient.bindMobile(REGISTRATION_ID1, "13000000000");
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());
} Schedule 样例
JPushClient jpushClient = new JPushClient(masterSecret, appKey);
String name = "test_schedule_example";
String time = "2016-07-30 12:30:25";
PushPayload push = PushPayload.alertAll("test schedule example.");
try {
ScheduleResult result = jpushClient.createSingleSchedule(name, time, push);
LOG.info("schedule result is " + 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());
} Custom Client 样例
public static void testCustomClient() {
ClientConfig config = ClientConfig.getInstance();
config.setMaxRetryTimes(5);
config.setConnectionTimeout(10 * 1000); // 10 seconds
config.setSSLVersion("TLSv1.1"); // JPush server supports SSLv3, TLSv1, TLSv1.1, TLSv1.2
JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config);
}
public static void testCustomPushClient() {
ClientConfig config = ClientConfig.getInstance();
config.setApnsProduction(false); // development env
config.setTimeToLive(60 * 60 * 24); // one day
// config.setGlobalPushSetting(false, 60 * 60 * 24); // development env, one day
JPushClient jPushClient = new JPushClient(masterSecret, appKey, null, config); // JPush client
// PushClient pushClient = new PushClient(masterSecret, appKey, null, config); // push client only
} Image Client 样例
public static void testUploadImageByUrl() throws APIConnectionException, APIRequestException {
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY);
ImageUrlPayload payload = ImageUrlPayload.newBuilder()
.setImageType(ImageType.LARGE_ICON)
.setImageUrl("http://xxx.com/image/a.jpg")
.build();
ImageUploadResult imageUploadResult = client.uploadImage(payload);
String mediaId = imageUploadResult.getMediaId();
}
public static void testUploadImageByFile() {
ImageClient client = new ImageClient(MASTER_SECRET, APP_KEY);
ImageFilePayload payload = ImageFilePayload.newBuilder()
.setImageType(ImageType.BIG_PICTURE)
// 本地文件路径
.setOppoFileName("/MyDir/a.jpg")
.setXiaomiFileName("/MyDir/a.jpg")
.build();
ImageUploadResult imageUploadResult = client.uploadImage(payload);
String mediaId = imageUploadResult.getMediaId();
} Weblogic 使用Java SDKWeblogic在使用jpush-api-java-client时需要注意的一些事项。 注意事项本文档基于weblogic 10.3.6 版本,12版本请自己对应配置路径。 极个别时候,证书会有版本升级等情况,所以一定要验证当前使用的证书和官方证书的指纹是否一致。 Weblogic console 设置
证书配置
证书对比方式 |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论