uniApp开通uniPush1.0个推,SpringBoot集成uniPush1.0个推
package com.joolun.cloud.common.jiguang.util;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.json.JSONObject;
import com.getui.push.v2.sdk.api.PushApi;
import com.getui.push.v2.sdk.api.UserApi;
import com.getui.push.v2.sdk.common.ApiResult;
import com.getui.push.v2.sdk.dto.req.Audience;
import com.getui.push.v2.sdk.dto.req.AudienceDTO;
import com.getui.push.v2.sdk.dto.req.CidAliasListDTO;
import com.getui.push.v2.sdk.dto.req.Settings;
import com.getui.push.v2.sdk.dto.req.message.PushDTO;
import com.getui.push.v2.sdk.dto.req.message.PushMessage;
import com.getui.push.v2.sdk.dto.res.TaskIdDTO;
import com.joolun.cloud.common.jiguang.dto.CIdAliasDTO;
import com.joolun.cloud.common.jiguang.dto.UniPushDTO;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
* @author yuanDing
* @version 1.0
* @date 2025-01-10 10:40
* uniPush 1.0推送工具类
*/
@Slf4j
@Configuration
@AllArgsConstructor
public class UniPushUtils {
private final PushApi pushApi;
private final UserApi userApi;
/**
* 单解绑
* 解除绑定cid和userId(解除绑定别名)
*
* @return
*/
public boolean unBoundingUserIdAndCId(CIdAliasDTO cIdAliasDTO) {
return this.unBoundingUserIdAndCIdList(ListUtil.toList(cIdAliasDTO));
}
/**
* 批量解绑
* 解除绑定cid和userId(批量解除绑定别名)
*
* @return
*/
public boolean unBoundingUserIdAndCIdList(List<CIdAliasDTO> cIdAliasDTOS) {
return userApi.batchUnbindAlias(this.getCidAliasListDTO(cIdAliasDTOS)).isSuccess();
}
/**
* 单绑
* 绑定cid和userId(绑定别名)
* 注意:1个cid只能绑定一个别名 1个别名最多只能绑定10个cid 超出以最后绑定为准
*
* @return
*/
public boolean boundingUserIdAndCId(CIdAliasDTO cIdAliasDTO) {
return this.boundingUserIdAndCIdList(new ArrayList<CIdAliasDTO>() {{
add(cIdAliasDTO);
}}).isSuccess();
}
/**
* 多绑
* 绑定cid和userId(绑定别名)
* 注意:1个cid只能绑定一个别名 1个别名最多只能绑定10个cid 超出已最后绑定为准
*
* @return
*/
public ApiResult<Void> boundingUserIdAndCIdList(List<CIdAliasDTO> cIdAliasDTOS) {
return userApi.bindAlias(this.getCidAliasListDTO(cIdAliasDTOS));
}
/**
* 获取cid alias DTO
* @param cIdAliasDTOS
* @return
*/
private CidAliasListDTO getCidAliasListDTO(List<CIdAliasDTO> cIdAliasDTOS) {
CidAliasListDTO cidAliasListDTO = new CidAliasListDTO();
// 0 普通别名 1系统别名
// cidAliasListDTO.setAliasType("1");
ArrayList<CidAliasListDTO.CidAlias> cidAliases = new ArrayList<>();
cIdAliasDTOS.forEach(aliasDTO -> {
CidAliasListDTO.CidAlias cidAlias = new CidAliasListDTO.CidAlias();
cidAlias.setAlias(aliasDTO.getAlias());
cidAlias.setCid(aliasDTO.getCId());
cidAliases.add(cidAlias);
});
cidAliasListDTO.setDataList(cidAliases);
return cidAliasListDTO;
}
/**
* 根据别名单推 alias参数必填
* @param uniPushDTO
* @return
*/
public boolean pushByAlias(UniPushDTO uniPushDTO) {
return pushApi.pushToSingleByAlias(this.getPushDTO(uniPushDTO , "alias")).isSuccess();
}
/**
* 根据CId单推 cid参数必填
* @param uniPushDTO
* @return
*/
public boolean pushByCId(UniPushDTO uniPushDTO) {
return pushApi.pushToSingleByCid(this.getPushDTO(uniPushDTO , "cid")).isSuccess();
}
/**
* 根据别名多推送 alias参数必填
* @param uniPushDTO
* @return
*/
public boolean pushByAliasList(UniPushDTO uniPushDTO) {
PushDTO pushDTO = new PushDTO();
PushMessage pushMessage = new PushMessage();
pushMessage.setTransmission(new JSONObject(uniPushDTO.getMessage()).toString());
pushDTO.setPushMessage(pushMessage);
ApiResult<TaskIdDTO> msg = pushApi.createMsg(pushDTO);
if (msg.isSuccess()) {
AudienceDTO audienceDTO = new AudienceDTO();
audienceDTO.setTaskid(msg.getData().getTaskId());
audienceDTO.setAsync(false);
Audience audience = new Audience();
for (String alias : uniPushDTO.getAliasList()) {
audience.addAlias(alias);
}
audienceDTO.setAudience(audience);
// 发送推送消息
return pushApi.pushListByAlias(audienceDTO).isSuccess();
}
return false;
}
/**
* 获取推送实体类
* @param uniPushDTO
* @param type
* @return
*/
public PushDTO<Audience> getPushDTO(UniPushDTO uniPushDTO , String type) {
//根据cid进行单推
PushDTO<Audience> pushDTO = new PushDTO<Audience>();
// 设置推送参数,requestid需要每次变化唯一
pushDTO.setRequestId(System.currentTimeMillis() + "");
Settings settings = new Settings();
pushDTO.setSettings(settings);
//消息有效期,走厂商消息必须设置该值
settings.setTtl(uniPushDTO.getTtl());
//在线走个推通道时推送的消息体
PushMessage pushMessage = new PushMessage();
pushDTO.setPushMessage(pushMessage);
//此格式的透传消息由 unipush 做了特殊处理,会自动展示通知栏。开发者也可自定义其它格式,在客户端自己处理。
pushMessage.setTransmission(new JSONObject(uniPushDTO.getMessage()).toString());
// 设置接收人信息
Audience audience = new Audience();
pushDTO.setAudience(audience);
switch (type) {
case "cid": uniPushDTO.getCIdList().forEach(audience::addCid);break;
case "alias": uniPushDTO.getAliasList().forEach(audience::addAlias);break;
}
/**
* 离线推送,暂未对接
*/
/* //设置离线推送时的消息体
PushChannel pushChannel = new PushChannel();
//安卓离线厂商通道推送的消息体
AndroidDTO androidDTO = new AndroidDTO();
Ups ups = new Ups();
ThirdNotification thirdNotification = new ThirdNotification();
ups.setNotification(thirdNotification);
thirdNotification.setTitle("安卓离线展示的标题");
thirdNotification.setBody("安卓离线展示的内容");
thirdNotification.setClickType("intent");
//注意:intent参数必须按下方文档(特殊参数说明)要求的固定格式传值,intent错误会导致客户端无法收到消息
thirdNotification.setIntent("请填写固定格式的intent");
androidDTO.setUps(ups);
pushChannel.setAndroid(androidDTO);*/
/**
* ios暂未对接 后续对接完放开注释
*/
//ios离线apn通道推送的消息体
// Alert alert = new Alert();
// alert.setTitle("苹果离线通知栏标题");
// alert.setBody("苹果离线通知栏内容");
// Aps aps = new Aps();
// aps.setContentAvailable(0);
// aps.setSound("default");
// aps.setAlert(alert);
// IosDTO iosDTO = new IosDTO();
// iosDTO.setAps(aps);
// iosDTO.setType("notify");
// pushChannel.setIos(iosDTO);
// pushDTO.setPushChannel(pushChannel);
return pushDTO;
}
}