
8 changed files with 420 additions and 11 deletions
@ -0,0 +1,40 @@ |
|||||
|
package com.zscat.mallplus.unipush; |
||||
|
|
||||
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
||||
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
||||
|
import com.zscat.mallplus.ums.entity.SysMessage; |
||||
|
import com.zscat.mallplus.ums.entity.SysMessageTask; |
||||
|
import com.zscat.mallplus.ums.mapper.SysMessageTaskMapper; |
||||
|
import com.zscat.mallplus.ums.service.ISysMessageService; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
|
||||
|
import java.util.Date; |
||||
|
import java.util.List; |
||||
|
|
||||
|
@Service |
||||
|
public class SysMessageTaskService extends ServiceImpl<SysMessageTaskMapper, SysMessageTask> { |
||||
|
|
||||
|
@Autowired |
||||
|
private ISysMessageService messageService; |
||||
|
|
||||
|
public void checkTask() { |
||||
|
QueryWrapper<SysMessageTask> qw = new QueryWrapper<>(); |
||||
|
qw.eq("status", 0); |
||||
|
qw.lt("sendtime", new Date()); |
||||
|
List<SysMessageTask> list = baseMapper.selectList(qw); |
||||
|
for (SysMessageTask task : list) { |
||||
|
SysMessage sm = new SysMessage(); |
||||
|
sm.setCode("系统消息"); |
||||
|
sm.setUserId(1); |
||||
|
sm.setId(null); |
||||
|
sm.setContent(task.getContent()); |
||||
|
sm.setParams(task.getParams()); |
||||
|
sm.setCtime(System.currentTimeMillis()); |
||||
|
messageService.sendToAll(sm); |
||||
|
task.setStatus(1); |
||||
|
baseMapper.updateById(task); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
} |
@ -0,0 +1,329 @@ |
|||||
|
package com.zscat.mallplus.unipush; |
||||
|
|
||||
|
import cn.hutool.core.thread.ThreadUtil; |
||||
|
import cn.hutool.json.JSONUtil; |
||||
|
import com.getui.push.v2.sdk.ApiHelper; |
||||
|
import com.getui.push.v2.sdk.GtApiConfiguration; |
||||
|
import com.getui.push.v2.sdk.api.PushApi; |
||||
|
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.message.PushDTO; |
||||
|
import com.getui.push.v2.sdk.dto.req.message.PushMessage; |
||||
|
import com.getui.push.v2.sdk.dto.req.message.android.GTNotification; |
||||
|
import com.getui.push.v2.sdk.dto.res.TaskIdDTO; |
||||
|
import com.zscat.mallplus.ums.entity.SysMessage; |
||||
|
import org.slf4j.Logger; |
||||
|
import org.slf4j.LoggerFactory; |
||||
|
import org.springframework.stereotype.Component; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.HashMap; |
||||
|
import java.util.List; |
||||
|
import java.util.Map; |
||||
|
|
||||
|
@Component |
||||
|
public class UniPushService { |
||||
|
|
||||
|
private static final Logger L = LoggerFactory.getLogger(UniPushService.class); |
||||
|
|
||||
|
// @Value("${unipush.appid:UBUIDJ8NQm50rGJsB6LYx1}")
|
||||
|
// private String appid;
|
||||
|
|
||||
|
private static String apiurl = "https://restapi.getui.com/v2/"; |
||||
|
private static String appid = "UBUIDJ8NQm50rGJsB6LYx1"; |
||||
|
private static String appkey = "RS3UZfeS509hcNEkmfS6R"; |
||||
|
private static String appsecret = "6QuFcPWFga8DQzSa03ruR7"; |
||||
|
private static String mastersecret = "6fjUinwRfDA3BcEnDQvTl5"; |
||||
|
private static String apppackage = "org.jbase.yxt.yyd.pyw"; |
||||
|
|
||||
|
private static PushApi pushApi = null; |
||||
|
|
||||
|
private PushApi singlePushApi() { |
||||
|
if (pushApi == null) { |
||||
|
System.setProperty("http.maxConnections", "200"); |
||||
|
GtApiConfiguration apiConfiguration = new GtApiConfiguration(); |
||||
|
//填写应用配置
|
||||
|
apiConfiguration.setAppId(appid); |
||||
|
apiConfiguration.setAppKey(appkey); |
||||
|
apiConfiguration.setMasterSecret(mastersecret); |
||||
|
// 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
|
||||
|
apiConfiguration.setDomain(apiurl); |
||||
|
// 实例化ApiHelper对象,用于创建接口对象
|
||||
|
ApiHelper apiHelper = ApiHelper.build(apiConfiguration); |
||||
|
// 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi
|
||||
|
pushApi = apiHelper.creatApi(PushApi.class); |
||||
|
} |
||||
|
return pushApi; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
public void test() { |
||||
|
// 设置httpClient最大连接数,当并发较大时建议调大此参数。或者启动参数加上 -Dhttp.maxConnections=200
|
||||
|
System.setProperty("http.maxConnections", "200"); |
||||
|
GtApiConfiguration apiConfiguration = new GtApiConfiguration(); |
||||
|
//填写应用配置
|
||||
|
apiConfiguration.setAppId("UBUIDJ8NQm50rGJsB6LYx1"); |
||||
|
apiConfiguration.setAppKey("RS3UZfeS509hcNEkmfS6R"); |
||||
|
apiConfiguration.setMasterSecret("6fjUinwRfDA3BcEnDQvTl5"); |
||||
|
// 接口调用前缀,请查看文档: 接口调用规范 -> 接口前缀, 可不填写appId
|
||||
|
apiConfiguration.setDomain("https://restapi.getui.com/v2/"); |
||||
|
// 实例化ApiHelper对象,用于创建接口对象
|
||||
|
ApiHelper apiHelper = ApiHelper.build(apiConfiguration); |
||||
|
// 创建对象,建议复用。目前有PushApi、StatisticApi、UserApi
|
||||
|
PushApi pushApi = apiHelper.creatApi(PushApi.class); |
||||
|
|
||||
|
//根据cid进行单推
|
||||
|
PushDTO<Audience> pushDTO = new PushDTO<Audience>(); |
||||
|
// 设置推送参数
|
||||
|
pushDTO.setRequestId(System.currentTimeMillis() + ""); |
||||
|
/**** 设置个推通道参数 *****/ |
||||
|
PushMessage pushMessage = new PushMessage(); |
||||
|
pushDTO.setPushMessage(pushMessage); |
||||
|
GTNotification notification = new GTNotification(); |
||||
|
pushMessage.setNotification(notification); |
||||
|
notification.setTitle("测试TitleXXX"); |
||||
|
notification.setBody("测试BodyXXXXXX"); |
||||
|
String payload = "{\"title\":\"liutitle\",\"body\":\"liubody\",\"name\":\"hao\",\"userid\":101,\"msgtitle\":\"haha\"}"; |
||||
|
notification.setPayload(payload); |
||||
|
notification.setClickType("payload"); |
||||
|
// notification.setClickType("url");
|
||||
|
// notification.setUrl("https://www.getui.com");
|
||||
|
/**** 设置个推通道参数,更多参数请查看文档或对象源码 *****/ |
||||
|
|
||||
|
// /**** 设置厂商相关参数 ****/
|
||||
|
// PushChannel pushChannel = new PushChannel();
|
||||
|
// pushDTO.setPushChannel(pushChannel);
|
||||
|
// /*配置安卓厂商参数*/
|
||||
|
// AndroidDTO androidDTO = new AndroidDTO();
|
||||
|
// pushChannel.setAndroid(androidDTO);
|
||||
|
// Ups ups = new Ups();
|
||||
|
// androidDTO.setUps(ups);
|
||||
|
// ThirdNotification thirdNotification = new ThirdNotification();
|
||||
|
// ups.setNotification(thirdNotification);
|
||||
|
// thirdNotification.setTitle("厂商title");
|
||||
|
// thirdNotification.setBody("厂商body");
|
||||
|
// thirdNotification.setClickType("url");
|
||||
|
// thirdNotification.setUrl("https://www.getui.com");
|
||||
|
// // 两条消息的notify_id相同,新的消息会覆盖老的消息,取值范围:0-2147483647
|
||||
|
// // thirdNotification.setNotifyId("11177");
|
||||
|
// /*配置安卓厂商参数结束,更多参数请查看文档或对象源码*/
|
||||
|
|
||||
|
// /*设置ios厂商参数*/
|
||||
|
// IosDTO iosDTO = new IosDTO();
|
||||
|
// pushChannel.setIos(iosDTO);
|
||||
|
// // 相同的collapseId会覆盖之前的消息
|
||||
|
// iosDTO.setApnsCollapseId("xxx");
|
||||
|
// Aps aps = new Aps();
|
||||
|
// iosDTO.setAps(aps);
|
||||
|
// Alert alert = new Alert();
|
||||
|
// aps.setAlert(alert);
|
||||
|
// alert.setTitle("ios title");
|
||||
|
// alert.setBody("ios body");
|
||||
|
// /*设置ios厂商参数结束,更多参数请查看文档或对象源码*/
|
||||
|
|
||||
|
/*设置接收人信息*/ |
||||
|
Audience audience = new Audience(); |
||||
|
pushDTO.setAudience(audience); |
||||
|
audience.addCid("89e286aee78d38faf748a690e603b1a3"); |
||||
|
/*设置接收人信息结束*/ |
||||
|
/**** 设置厂商相关参数,更多参数请查看文档或对象源码 ****/ |
||||
|
|
||||
|
// 进行cid单推
|
||||
|
ApiResult<Map<String, Map<String, String>>> apiResult = pushApi.pushToSingleByCid(pushDTO); |
||||
|
if (apiResult.isSuccess()) { |
||||
|
// success
|
||||
|
System.out.println(apiResult.getData()); |
||||
|
} else { |
||||
|
// failed
|
||||
|
System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public void testlist() { |
||||
|
String taskid = createMessageTaskaa(); |
||||
|
AudienceDTO pdto = new AudienceDTO(); |
||||
|
|
||||
|
Audience audience = new Audience(); |
||||
|
audience.addCid("89e286aee78d38faf748a690e603b1a3"); |
||||
|
audience.addCid("e4a96929cc5f40c886cb517853356bc2"); |
||||
|
|
||||
|
pdto.setAudience(audience); |
||||
|
pdto.setTaskid(taskid); |
||||
|
|
||||
|
ApiResult<Map<String, Map<String, String>>> mapApiResult = singlePushApi().pushListByCid(pdto); |
||||
|
|
||||
|
System.out.println(mapApiResult); |
||||
|
} |
||||
|
|
||||
|
public String createMessageTaskaa() { |
||||
|
PushDTO<Audience> pdto = new PushDTO<>(); |
||||
|
pdto.setGroupName("群发消息"); |
||||
|
|
||||
|
|
||||
|
PushMessage pushMessage = new PushMessage(); |
||||
|
|
||||
|
GTNotification notification = new GTNotification(); |
||||
|
notification.setTitle("测试Titleaa"); |
||||
|
notification.setBody("测试BodyXaa"); |
||||
|
|
||||
|
SysMessage msg = new SysMessage(); |
||||
|
msg.setId(11); |
||||
|
msg.setUserId(1); |
||||
|
msg.setCode("云链助手"); |
||||
|
msg.setParams("欢迎"); |
||||
|
msg.setContent("欢迎注册汇融云链~"); |
||||
|
msg.setCtime(System.currentTimeMillis()); |
||||
|
|
||||
|
// String payload = "{\"title\":\"lzh\",\"body\":\"liubody\",\"name\":\"hao\",\"userid\":101,\"msgtitle\":\"haha\"}";
|
||||
|
String payload = JSONUtil.toJsonStr(msg); |
||||
|
notification.setPayload(payload); |
||||
|
notification.setClickType("payload"); |
||||
|
|
||||
|
pushMessage.setNotification(notification); |
||||
|
|
||||
|
pdto.setPushMessage(pushMessage); |
||||
|
|
||||
|
ApiResult<TaskIdDTO> msgg = this.singlePushApi().createMsg(pdto); |
||||
|
System.out.println(msgg); |
||||
|
return msgg.getData().getTaskId(); |
||||
|
} |
||||
|
|
||||
|
public static void main(String[] args) { |
||||
|
List<String> clist = new ArrayList<>(); |
||||
|
clist.add("079fb42d1f4f824a3defd471a432d0fe"); |
||||
|
clist.add("89e286aee78d38faf748a690e603b1a3"); |
||||
|
|
||||
|
SysMessage msg = new SysMessage(); |
||||
|
msg.setId(11); |
||||
|
msg.setUserId(1); |
||||
|
msg.setCode("交易物流"); |
||||
|
msg.setParams("ABC"); |
||||
|
msg.setContent("欢迎注册汇融云链~"); |
||||
|
msg.setCtime(System.currentTimeMillis()); |
||||
|
new UniPushService().sendPush(msg, clist); |
||||
|
// new UniPushService().testlist();
|
||||
|
} |
||||
|
|
||||
|
private static String imgUrltx = "http://mall.yyundong.com/tx.png"; |
||||
|
private static String imgUrltz = "http://mall.yyundong.com/tz.png"; |
||||
|
private static String imgUrlwl = "http://mall.yyundong.com/wl.png"; |
||||
|
|
||||
|
public void sendPush(SysMessage mess, List<String> cids) { |
||||
|
// String code = mess.getCode();
|
||||
|
// if ("系统消息".equals(code)) {
|
||||
|
// sendTzMsg(mess);
|
||||
|
// } else {
|
||||
|
// sendUserMsg(mess, cids);
|
||||
|
// }
|
||||
|
|
||||
|
if (cids == null || cids.isEmpty()) |
||||
|
return; |
||||
|
|
||||
|
PushMessage pushMessage = createPushMessage(mess); |
||||
|
int size = cids.size(); |
||||
|
for (int i = 0; i < size; i++) { |
||||
|
sendSingleMsg(pushMessage, cids.get(i), i); |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
|
||||
|
public void sendPushAsync(SysMessage mess, List<String> cids) { |
||||
|
ThreadUtil.execute(() -> sendPush(mess, cids)); |
||||
|
} |
||||
|
|
||||
|
private void sendSingleMsg(PushMessage pushMessage, String cid, int index) { |
||||
|
PushDTO<Audience> pushDTO = new PushDTO<Audience>(); |
||||
|
pushDTO.setRequestId("" + System.currentTimeMillis() + index); |
||||
|
pushDTO.setPushMessage(pushMessage); |
||||
|
Audience audience = new Audience(); |
||||
|
audience.addCid(cid); |
||||
|
pushDTO.setAudience(audience); |
||||
|
ApiResult<Map<String, Map<String, String>>> apiResult = singlePushApi().pushToSingleByCid(pushDTO); |
||||
|
if (apiResult.isSuccess()) { |
||||
|
System.out.println(apiResult.getData()); |
||||
|
} else { |
||||
|
L.warn("推送信息失败:code:{}, msg: {}", apiResult.getCode(), apiResult.getMsg()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void sendUserMsg(SysMessage mess, List<String> cids) { |
||||
|
|
||||
|
if (cids == null || cids.isEmpty()) |
||||
|
return; |
||||
|
|
||||
|
String taskid = createMessageTask(mess); |
||||
|
AudienceDTO pdto = new AudienceDTO(); |
||||
|
|
||||
|
Audience audience = new Audience(); |
||||
|
cids.forEach(cid -> audience.addCid(cid)); |
||||
|
|
||||
|
pdto.setAudience(audience); |
||||
|
pdto.setTaskid(taskid); |
||||
|
|
||||
|
ApiResult<Map<String, Map<String, String>>> mapApiResult = singlePushApi().pushListByCid(pdto); |
||||
|
|
||||
|
} |
||||
|
|
||||
|
private void sendTzMsg(SysMessage mess) { |
||||
|
|
||||
|
PushDTO<String> pd = new PushDTO<>(); |
||||
|
pd.setRequestId("all" + System.currentTimeMillis()); |
||||
|
pd.setGroupName("system_notice"); |
||||
|
pd.setAudience("all"); |
||||
|
|
||||
|
pd.setPushMessage(createPushMessage(mess)); |
||||
|
|
||||
|
ApiResult<TaskIdDTO> taskIdDTOApiResult = singlePushApi().pushAll(pd); |
||||
|
// System.out.println(taskIdDTOApiResult);
|
||||
|
} |
||||
|
|
||||
|
private PushMessage createPushMessage(SysMessage mess) { |
||||
|
|
||||
|
String code = mess.getCode(); |
||||
|
PushMessage pushMessage = new PushMessage(); |
||||
|
|
||||
|
GTNotification notification = new GTNotification(); |
||||
|
// notification.setTitle(code);
|
||||
|
// notification.setBody(mess.getParams());
|
||||
|
notification.setTitle(mess.getParams()); |
||||
|
notification.setBody(mess.getContent()); |
||||
|
if ("交易物流".equals(code)) { |
||||
|
notification.setLogoUrl(imgUrlwl); |
||||
|
} else if ("云链助手".equals(code)) { |
||||
|
notification.setLogoUrl(imgUrltx); |
||||
|
} else { |
||||
|
notification.setLogoUrl(imgUrltz); |
||||
|
} |
||||
|
Map<String, Object> map = new HashMap<>(); |
||||
|
map.put("code", mess.getCode()); |
||||
|
map.put("params", mess.getParams()); |
||||
|
map.put("content", mess.getContent()); |
||||
|
map.put("cdate", mess.getCdate()); |
||||
|
map.put("id", mess.getId()); |
||||
|
map.put("ctime", mess.getCtime()); |
||||
|
map.put("status", mess.getStatus()); |
||||
|
map.put("userId", mess.getUserId()); |
||||
|
String payload = JSONUtil.toJsonStr(map); |
||||
|
notification.setPayload(payload); |
||||
|
notification.setClickType("payload"); |
||||
|
// notification.setClickType("payload_custom");
|
||||
|
|
||||
|
pushMessage.setNotification(notification); |
||||
|
|
||||
|
return pushMessage; |
||||
|
} |
||||
|
|
||||
|
private String createMessageTask(SysMessage mess) { |
||||
|
PushDTO<Audience> pdto = new PushDTO<>(); |
||||
|
pdto.setGroupName("tolist"); |
||||
|
|
||||
|
pdto.setPushMessage(createPushMessage(mess)); |
||||
|
|
||||
|
ApiResult<TaskIdDTO> result = this.singlePushApi().createMsg(pdto); |
||||
|
// System.out.println(msgg);
|
||||
|
return result.getData().getTaskId(); |
||||
|
} |
||||
|
|
||||
|
} |
Loading…
Reference in new issue