JPushUtil工具类
package com.hanboard.educloud.util;
import cn.jpush.api.JPushClient;
import cn.jpush.api.push.PushResult;
import cn.jpush.api.push.model.Options;
import cn.jpush.api.push.model.Platform;
import cn.jpush.api.push.model.PushPayload;
import cn.jpush.api.push.model.PushPayload.Builder;
import cn.jpush.api.push.model.audience.Audience;
import cn.jpush.api.push.model.notification.IosNotification;
import cn.jpush.api.push.model.notification.Notification;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @descriptions:
* @author: wujun
* @date: 2019/8/22 11:27
* @version: 1.0
*/
@Component
public class JPushUtil {
private static Logger logger = LoggerFactory.getLogger(JPushUtil.class);
@Value("${spring.JPush.app_key}")
private String app_key;
@Value("${spring.JPush.master_secret}")
private String master_secret;
@Value("${spring.JPush.apnsProduction}")
private Boolean apnsProduction;
@Autowired
private RabbitTemplate rabbitTemplate;
@Autowired
private static JPushUtil jPushUtil;
@PostConstruct
public void init() {
jPushUtil = this;
jPushUtil.app_key = this.app_key;
jPushUtil.master_secret = this.master_secret;
jPushUtil.apnsProduction = this.apnsProduction;
jPushUtil.rabbitTemplate = this.rabbitTemplate;
}
/**
* 通知推送
* 备注:推送方式不为空时,推送的值也不能为空;推送方式为空时,推送值不做要求
*
* @param type 推送方式:1、“tag”标签推送,2、“alias”别名推送
* @param value 推送的标签或别名值
* @param alert 推送的内容
*/
public static void pushNotice(String type, String value, String alert) {
JPushClient jpushClient = new JPushClient(jPushUtil.master_secret, jPushUtil.app_key);
Builder builder = PushPayload.newBuilder();
builder.setPlatform(Platform.all());//设置接受的平台,all为所有平台,包括安卓、ios、和微软的
//设置如果用户不在线、离线消息保存的时间
Options options = Options.sendno();
options.setTimeToLive(86400L); //设置为86400为保存一天,如果不设置默认也是保存一天
builder.setOptions(options);
//设置推送方式
if (type.equals("alias")) {
builder.setAudience(Audience.alias(value));//根据别名推送
// builder.setAudience(Audience.all());//根据别名推送
} else if (type.equals("tag")) {
builder.setAudience(Audience.tag(value));//根据标签推送
} else {
builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
}
//设置为采用通知的方式发送消息
builder.setNotification(Notification.alert(alert));
PushPayload pushPayload = builder.build();
try {
// //进行推送,实际推送就在这一步
PushResult pushResult = jpushClient.sendPush(pushPayload);
System.out.println(pushResult);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 极光推送>>All所有平台
*
* @param type JPushUtilType.xx 发送类型
* @param targetList 发送的集合
* @param message 消息体
* @param param 返回参数
* @return
*/
public static void pushNotice(String type, List<String> targetList, String message, Map param) {
if(CollectionUtils.isNotEmpty(targetList)){
jpushAndroid(type, targetList, message, param);
jpushIOS(type, targetList, message, param);
}
}
/**
* 格式化数据
*
* @param targetList
* @return
*/
public static String formatTargetListData(List<String> targetList) {
String result = "";
if (CollectionUtils.isEmpty(targetList)) {
return null;
}
for (String s : targetList) {
result += s + ",";
}
result.substring(0, result.length() - 1);
return result;
}
/**
* 获取多个极光推送端
*
* @return
*/
public static List<JPushClient> getJPushClientList() {
List<JPushClient> list = new ArrayList<>();
String master_secret = jPushUtil.master_secret;
String app_key = jPushUtil.app_key;
String[] secrets = master_secret.split(",");
String[] keys = app_key.split(",");
for (int i = 0; i < keys.length; i++) {
String key = keys[i];
String secret = secrets[i];
JPushClient jpushClient = new JPushClient(secret, key);
list.add(jpushClient);
}
return list;
}
//极光推送>>Android
//Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数
public static void jpushAndroid(String type, List<String> targetList, String message, Map param) {
//创建JPushClient(极光推送的实例)
// JPushClient jpushClient = new JPushClient(jPushUtil.master_secret, jPushUtil.app_key);
//创建option
Builder builder = PushPayload.newBuilder();
builder.setPlatform(Platform.android()); //所有平台的用户
if (type.equals(JPushUtilTypeEnum.ALIAS)) {
builder.setAudience(Audience.alias(targetList));//根据别名推送
} else if (type.equals(JPushUtilTypeEnum.TAG)) {
builder.setAudience(Audience.tag(targetList));//根据标签推送
} else {
builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
}
Notification notification = Notification.android(message, "应急监测", param);
builder.setNotification(notification);
builder.setOptions(Options.newBuilder().setApnsProduction(false).build());
// builder.setMessage(Message.newBuilder().setMsgContent(message).addExtras(param).setTitle("666").build());//自定义信息
PushPayload pushPayload = builder.build();
List<JPushClient> jPushClientList = getJPushClientList();
for (JPushClient jpushClient : jPushClientList) {
try {
PushResult pu = jpushClient.sendPush(pushPayload);
} catch (Exception e) {
logger.error("安卓消息推送失败:{}",e.getMessage());
}
}
}
//极光推送>>ios
//Map<String, String> parm是我自己传过来的参数,同学们可以自定义参数
public static void jpushIOS(String type, List<String> targetList, String message, Map param) {
//创建JPushClient(极光推送的实例)
// JPushClient jpushClient = new JPushClient(jPushUtil.master_secret, jPushUtil.app_key);
//创建option
Builder builder = PushPayload.newBuilder();
builder.setPlatform(Platform.ios()); //所有平台的用户
if (type.equals(JPushUtilTypeEnum.ALIAS)) {
builder.setAudience(Audience.alias(targetList));//根据别名推送
} else if (type.equals(JPushUtilTypeEnum.TAG)) {
builder.setAudience(Audience.tag(targetList));//根据标签推送
} else {
builder.setAudience(Audience.all());//Audience设置为all,说明采用广播方式推送,所有用户都可以接收到
}
Notification notification = Notification.newBuilder().addPlatformNotification(IosNotification.newBuilder()
.setAlert(message)
.setBadge(+1)
.setSound("happy")//这里是设置提示音(更多可以去官网看看)
.addExtras(param)
.build())
.build()
;
builder.setNotification(notification);
builder .setOptions(Options.newBuilder().setApnsProduction(jPushUtil.apnsProduction).build());
PushPayload pushPayload = builder.build();
List<JPushClient> jPushClientList = getJPushClientList();
for (JPushClient jpushClient : jPushClientList) {
try {
PushResult pu = jpushClient.sendPush(pushPayload);
logger.info("苹果"+pu.toString());
} catch (Exception e) {
logger.error("苹果消息推送失败:{}",e.getMessage());
}
}
}
/**
* 微信消息推送数据封装
* @param result 参数集合
* @param userIds 用户id集合
* @return
*/
public static void pushWchart(Map result , List<String> userIds){
if(CollectionUtils.isNotEmpty(userIds)){
for (String userId : userIds) {
result.put("userId",userId);
try {
jPushUtil.rabbitTemplate.convertAndSend("WeChatOfficialQueue",result);
}catch (Exception e){
logger.error("pushWchart="+e.getMessage());
}
}
}
}
}
.yml文件配置
spring:
JPush:
app_key: "b28d74e562a0d46c184acf87,9d6d16f46a2aba53a065682d,742b35883a887b1ad7e6d9ba"
master_secret: "65983b60bcfb8e981319614c,8b2905b263c48f0d93e06ff6,34273a67d2b164b887246052"
apnsProduction: false