与SpringBoot的集成
pom.xml:
<!-- 极光推送 -->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.4</version>
</dependency>
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jiguang-common</artifactId>
<version>1.1.1</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>
Config类
/**
* 极光推送
*
* @author mengqa
**/
@Configuration
public class JgPushConfig {
/**
* key 自己在极光申请后得到
*/
private String key = "*********************";
/**
* secret 自己在极光申请后得到
*/
private String secret = "*********************";
private JPushClient jPushClient;
@Bean("jPushConfig")
public JPushClient jPushConfig() {
ClientConfig clientConfig = ClientConfig.getInstance();
if (jPushClient == null) {
jPushClient = new JPushClient( secret, key,null, clientConfig);
}
return jPushClient;
}
}
推送接口:
/**
* @author mengqa
* @create 2018-04-26 11:03
**/
@Api(tags = "极光推送")
@RestController
@RequestMapping("/jgPush")
public class JgPushController {
private static final Logger LOG = LoggerFactory.getLogger(JgPushController.class);
@Autowired
private JgPushConfig jPushConfig;
@ApiOperation(value = "推送测试")
@GetMapping("/push")
public void push() {
JPushClient jPushClient = jPushConfig.jPushConfig();
PushInformation info = new PushInformation("内容", "标题");
PushPayload payload = info.getPushPayLoad();
try {
PushResult result = jPushClient.sendPush(payload);
LOG.info("推送结果 - " + result);
} catch (APIConnectionException e) {
LOG.error("Connection error. Should retry later. ", e);
LOG.error("Sendno: " + payload.getSendno());
} 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());
LOG.error("Sendno: " + payload.getSendno());
}
}
}
自己封装的实体, 根据你自己的需求或更好的想法:
/**
* 推送信息实体
* @author mengqa
* @date 2018-04-26 13:55
**/
public class PushInformation {
// 内容
private String content;
// 标题
private String title;
// 系统 0.全部 1.安卓 2.iOS
private Integer system = 0;
// 推送用户
private Set<String> users;
// 推送平台
private Platform platform;
// 推送相关设置
private Notification notification;
// 推送群体
private Audience audience;
private Notification.Builder builder;
public PushInformation() {
}
public PushInformation(String content, String title) {
this.content = content;
this.title = title;
this.builder = Notification.newBuilder()
.setAlert(content);
initBuilder();
}
public PushInformation(String content, String title, Integer system) {
this.content = content;
this.title = title;
this.system = system;
this.builder = Notification.newBuilder()
.setAlert(content);
initBuilder();
}
public PushInformation(String content, String title, Integer system, Set<String> users) {
this.content = content;
this.title = title;
this.system = system;
this.users = users;
this.builder = Notification.newBuilder()
.setAlert(content);
initBuilder();
}
public void setContent(String content) {
this.content = content;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public String getTitle() {
return title;
}
public Integer getSystem() {
return system;
}
public void setSystem(Integer system) {
this.system = system;
}
private void initBuilder() {
switch (system) {
case 0 :
this.platform = Platform.android_ios();
this.notification = buildAll(title, builder);
break;
case 1 :
this.platform = Platform.android();
this.notification = buildAndroid(title, builder);
break;
case 2 :
this.platform = Platform.ios();
notification = buildiOS(builder);
break;
default:
break;
}
if (!CollectionUtils.isEmpty(users)) {
String[] arr = users.toArray(new String[0]);
this.audience = Audience.alias(arr);
} else {
this.audience = Audience.all();
}
}
public PushPayload getPushPayLoad() {
return PushPayload.newBuilder()
.setPlatform(platform)
.setAudience(audience)
.setNotification(notification)
.build();
}
public Platform getPlatform() {
return platform;
}
public Notification getNotification() {
return notification;
}
public Audience getAudience() {
return audience;
}
private Notification buildAll(String title, Notification.Builder builder) {
return builder.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(title).build())
.addPlatformNotification(IosNotification.newBuilder()
.incrBadge(1).build())
.build();
}
private Notification buildiOS(Notification.Builder builder) {
IosNotification ios = IosNotification.newBuilder()
.incrBadge(1).build();
return builder.addPlatformNotification(ios).build();
}
private Notification buildAndroid(String title, Notification.Builder builder) {
AndroidNotification android = AndroidNotification.newBuilder()
.setTitle(title).build();
return builder.addPlatformNotification(android).build();
}
}