很多 app都要用到推送,当时每次封装成工具类的时候方法形参太多 可阅读性变得很差 也不方便扩展,所有下定决心改改屎山
首先我们来写一个配置吧
这里使用springboot的 @ConfigurationProperties(prefix = "push") 将获取到的信息 放在配置文件里即可
// key
private String appKey;
// secret
private String masterSecret;
// ios下是否是开发环境
private Integer product;
推送设备枚举
public enum PushDeviceEnum {
/**
* 全部设备
*/
ALL(0, "ALL"),
/***
* IOS
*/
IOS(4, "ios"),
/**
* android
*/
ANDROID(3, "android");
private Integer index;
private String name;
PushDeviceEnum(Integer index, String name) {
this.index = index;
this.name = name;
}
public Integer getIndex() {
return this.index;
}
public static PushDeviceEnum getDevice(Integer index) {
if (index == null) {
return null;
}
for (PushDeviceEnum value : PushDeviceEnum.values()) {
if (value.getIndex().equals(index)) {
return value;
}
}
return null;
}
}
好了 万事具备 只欠东风了
初始化整个极光推送等
@Slf4j
public class PushBuilder {
private String apiKey;
private String secretKey;
private Integer product;
/**
* 标题
*/
private String title;
/**
* 内容
*/
private String content;
private PushPayload.Builder load;
/**
* 设备 ios or android or all
*/
private Integer deviceType;
/***
* 扩展字段
*/
private Map<String, String> param;
private JPushClient client;
private Map<String, String> result;
/**
*
* @param config 已被初始化后的极光推送配置
*/
public PushBuilder(PushConfig config) {
this.apiKey = config.getAppKey();
this.secretKey = config.getMasterSecret();
this.product = config.getProduct();
}
/**
* 1.初始化 JPushClient
* 2.初始化 推送设备 ios/android/all
* 3.加载 PushPayload.Builder
* @param pushDeviceEnum 设备枚举
* @return this
* @throws Exception 初始化失败时 抛出异常
*/
public PushBuilder init(PushDeviceEnum pushDeviceEnum) throws Exception {
this.client = new JPushClient(this.secretKey, this.apiKey);
this.deviceType = pushDeviceEnum.getIndex();
this.load = PushPayload.newBuilder();
this.result = new HashMap<>(3);
if (PushDeviceEnum.ANDROID.getIndex().equals(this.deviceType)) {
this.load.setPlatform(Platform.android());
} else if (PushDeviceEnum.IOS.getIndex().equals(this.deviceType)) {
this.load.setPlatform(Platform.android_ios());
} else if (PushDeviceEnum.ALL.getIndex().equals(this.deviceType)) {
this.load.setPlatform(Platform.all());
} else {
throw new Exception("未知操作系统");
}
return this;
}
/**
* 推送所有
* @return this
*/
public PushBuilder pushAll() {
this.load.setAudience(Audience.all());
return this;
}
/**
* 根据regId推送 可跟业务修改推送方式
* @param deviceId 设备id
* @return this
*/
public PushBuilder pushOne(String... deviceId) {
// 可推送多人 将参数修改为 String... deviceId deviceId为客户端的注册id
this.load.setAudience(Audience.registrationId(deviceId));
return this;
}
public PushBuilder title(String title) {
this.title = title;
return this;
}
public PushBuilder content(String content) {
this.content = content;
return this;
}
public PushBuilder param(Map<String, String> param) {
this.param = param;
return this;
}
/**
* 这里初始化payload 和消息对象
*/
private void payload() {
Notification.Builder notify = Notification.newBuilder();
notify.addPlatformNotification(AndroidNotification.newBuilder()
.setTitle(this.title)
.setAlert(this.content)
.addExtras(param)
.setBuilderId(1)
.build())
.addPlatformNotification(IosNotification.newBuilder()
.setAlert(this.content)
.incrBadge(1)
.setSound("sound.caf")
.addExtras(param)
.build());
this.load.setNotification(notify.build());
this.load.setOptions(Options.newBuilder()
.setApnsProduction(this.product == 1)
.setTimeToLive(86400)
.build());
}
/**
* 这里才会实际的去推送消息
* @return 推送结果 可自定义返回结果
* @throws Exception 发送推送异常时候抛出
*/
public Map<String, String> push() throws Exception {
this.payload();
PushResult pushResult;
try {
pushResult = this.client.sendPush(this.load.build());
log.info("response:{}", pushResult);
if (pushResult.isResultOK()) {
this.result.put("info", JSON.toJSONString(pushResult));
this.result.put("msgId", String.valueOf(pushResult.msg_id));
this.result.put("content", this.content);
this.result.put("title", this.title);
this.result.put("deviceType", this.deviceType == null ? "0" : this.deviceType.toString());
log.info("消息已发送:title:{},content:{},msgId:{}", this.title, content, pushResult.msg_id);
} else {
log.error("unknown error:{}", pushResult);
throw new Exception(JSON.toJSONString(pushResult));
}
} catch (APIConnectionException | APIRequestException e) {
log.error("push error:", e);
throw e;
} finally {
this.client.close();
}
return this.result;
}
}
调用实例
new PushBuilder(config)
.init(PushDeviceEnum.ALL)
.title(title)
.content(content)
.pushAll()
.param(param)
.push();
这里实际用new的方式 已经可以进行消息推送了 当然再PushBuilder里面属性可以初始化某些值 如果链式调用没有对他赋值 就使用默认值
但是消息推送成功/失败需要存入数据库或者进行其他业务 这里就不是特别清晰
所以我在aop里面对它进行了成功失败处理 让方法变得更纯粹一点
来源:CSDN
作者:不周山的秘密
链接:https://blog.csdn.net/LemonSirForYou/article/details/103479205