1、加入依赖
<!--极光推送-->
<dependency>
<groupId>cn.jpush.api</groupId>
<artifactId>jpush-client</artifactId>
<version>3.3.7</version>
</dependency>
2、极光配置
#极光配置
jpush:
appKey:
masterSecret:
3、极光配置映射
/**
* 极光推送配置
* Created by linchaokun on 2018/5/7.
*/
@Data
@ConfigurationProperties(prefix = "jpush")
@Component
public class JpushConfig {
/**
* 极光申请的app key
*/
private String appKey;
/**
* 极光申请的secret
*/
private String masterSecret;
}
4、自动注入JPushClient
/**
* 自动注入 JPushClient 极光配置
* Created by 林朝昆
*/
@Component
public class JpushInstanceConfig {
@Autowired
private JpushConfig jpushConfig;
@Bean
public JPushClient jpushClient() {
JPushClient jpushClient = new JPushClient(jpushConfig.getMasterSecret(), jpushConfig.getAppKey(), null, ClientConfig.getInstance());
return jpushClient;
}
}
5、获取PushPayload对象
/**
* 使用别名:
* 用于给某特定用户推送消息。别名,可以近似地被认为,是用户帐号里的昵称。
* 使用标签:
* 用于给某一群人推送消息。
* 标签类似于博客里为文章打上 tag ,即为某资源分类。
* Created by linchaokun on 2018/7/5.
*/
public class JPushClientPC {
/**
* 发送所有的消息 全部人
*
* @param responseNoticeMessage
* @return
*/
public static PushPayload buildPushObject_all_message(ResponseNoticeMessage responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.all())//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据别名集合 推送
*
* @param userNames 别名List
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_alias_message(List<String> userNames, ResponseNoticeMessage responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.alias(userNames))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据别名 推送
*
* @param userName 别名
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_alias_message(String userName, ResponseNoticeMessage responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.alias(userName))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据标签集合推送
*
* @param tags 标签List
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_tags_message(List<String> tags, ResponseNoticeMessage responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.tag(tags))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
/**
* 根据标签推送
*
* @param tag 标签
* @param responseNoticeMessage 消息
* @return
*/
public static PushPayload buildPushObject_all_tag_message(String tag, ResponseNoticeMessage responseNoticeMessage) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())//设置接受的平台
.setAudience(Audience.tag(tag))//
//.setNotification(Notification.alert(JSON.toJSONString(responseNoticeMessage)))
.setMessage(Message.content(JSON.toJSONString(responseNoticeMessage))) //内部消息不显示,类似web socket
.build();
}
}
6、极光推送工具类
/**
* 极光推送工具类
* Created by linchaokun on 2018/7/5.
*/
@Component
@Slf4j
public class JpushUtil {
@Autowired
private JPushClient jpushClient;
/**
* 推送所有平台 用于广播消息 用于管理员使用
*
* @param responseNoticeMessage 消息
*/
public void sendPushAllMessage(ResponseNoticeMessage responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_message(responseNoticeMessage);
sendPush(payload);
}
/**
* 根据别名推送所有平台
* 一次推送最多 1000 个。
*
* @param userNames 别名
* @param responseNoticeMessage 消息
*/
public void sendPushAliasAllMessage(List<String> userNames, ResponseNoticeMessage responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_alias_message(userNames, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据别名推送所有平台
*
* @param userName 别名
* @param responseNoticeMessage 消息
*/
public void sendPushAliasAllMessage(String userName, ResponseNoticeMessage responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_alias_message(userName, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据标签推送所有平台
* 一次推送最多 20 个。
*
* @param tags 标签名
* @param responseNoticeMessage 消息
*/
public void sendPushTagsAllMessage(List<String> tags, ResponseNoticeMessage responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_tags_message(tags, responseNoticeMessage);
sendPush(payload);
}
/**
* 根据标签推送所有平台
*
* @param tag 标签名
* @param responseNoticeMessage 消息
*/
public void sendPushTagsAllMessage(String tag, ResponseNoticeMessage responseNoticeMessage) {
//生成推送的内容
PushPayload payload = JPushClientPC.buildPushObject_all_tag_message(tag, responseNoticeMessage);
sendPush(payload);
}
/**
* 发送
* @param payload
* @return
*/
private PushResult sendPush(PushPayload payload){
try {
PushResult result = jpushClient.sendPush(payload);
jpushClient.close();
return result;
} catch (Exception e) {
log.info("sendPushAllAlert : HTTP Status -=- {}", e.getMessage());
}
return null;
}
}