参考Api
第一步 maven
将下边的依赖条件放到你项目的 maven pom.xml 文件里。
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.2.17</version>
</dependency>
如果使用 Maven 构建项目,则需要在你的项目 pom.xml 里增加:
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.0.3</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>
第二步 代码已经全部写到一个方法里面(通过别名实现)
public static void main(String[] args) {
String alias = "app_doctor_123456";//声明别名
String masterSecret = "value";//需要注册才能得到
String appKey = "key";//需要注册才能得到,ios安卓会告诉我们
String ALERT = "推送信息啊啊啊啊啊啊啊啊啊啊11111111";
log.info("对别名" + alias + "的用户推送信息");
JSONObject json = new JSONObject();
json.put("11", true);
json.put("22", "33");
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(masterSecret, appKey, null, clientConfig);
PushPayload payload = PushPayload.newBuilder()
.setPlatform(Platform.android_ios())
.setAudience(Audience.alias(alias))
.setNotification(Notification.newBuilder()
.addPlatformNotification(IosNotification.newBuilder()
.addExtra("data", json.toJSONString())
.setAlert(ALERT)
.build())
.build())
.setOptions(Options.newBuilder()
.setApnsProduction(false)//true-推送生产环境 false-推送开发环境(测试使用参数)
.setTimeToLive(90)//消息在JPush服务器的失效时间(测试使用参数)
.build())
.build();
//PushPayload payload = buildPushObject_ios_messageWithExtras(alias);
try {
//payload.messageAll("内容");
jpushClient.sendPush(payload);
} 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());
log.info("Msg ID: " + e.getMsgId());
}
}