流程
- 在极光官网先注册一个账号,创建一个应用,获取AppKey、Master Secret (官网:https://www.jiguang.cn)
- 在java项目中导入依赖的jar包,如果你是maven项目,在pom文件中添加依赖即可。
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.4.7</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.8</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.6.Final</version>
<scope>compile</scope>
</dependency>
- 创建一个JPushUtil类
public class JpushClientUtil {
private final static String APPKEY = "d9cec8d81fdf8e69398550b5";
private final static String MASTERSECRET = "d07cd0328470209f1dc35c0c";
private final static Boolean ApnsProduction = false;
private final static int RETURNCODE = 200;
private final static JPushClient jPushClient = new JPushClient(MASTERSECRET, APPKEY);
private JpushClientUtil() {
throw new AssertionError("不能产生该对象");
}
public static int sendToAlias(List<String> alias, String notification_title, String msg_title, String msg_content, String extrasparam) {
int result = 0;
try {
PushPayload pushPayload = JpushClientUtil.buildPushObject_all_alias_alertWithTitle(alias, notification_title, msg_title, msg_content, extrasparam);
PushResult pushResult = jPushClient.sendPush(pushPayload);
if (pushResult.getResponseCode() == RETURNCODE) {
result = 1;
}
log.info("[极光推送]PushResult result is " + pushResult);
} 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());
}
return result;
}
public static int sendToAlias2(List<String> alias, String notification_title, String msg_title, String msg_content, String extrasparam, String time) {
int result = 0;
try {
PushPayload pushPayload = JpushClientUtil.buildPushObject_all_alias_alertWithTitle(alias, notification_title, msg_title, msg_content, extrasparam);
ScheduleResult scheduleResult = jPushClient.createSingleSchedule("点呗生活", time, pushPayload);
if (scheduleResult.getResponseCode() == RETURNCODE) {
result = 1;
}
log.info("[极光推送]ScheduleResult result is " + scheduleResult);
} 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());
}
return result;
}