package com.hxzkappboot.util; import com.alibaba.druid.support.json.JSONUtils; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.getui.push.v2.sdk.ApiHelper; 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.Settings; import com.getui.push.v2.sdk.dto.req.Strategy; import com.getui.push.v2.sdk.dto.req.message.PushChannel; 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.AndroidDTO; import com.getui.push.v2.sdk.dto.req.message.android.ThirdNotification; import com.getui.push.v2.sdk.dto.req.message.android.Ups; import com.getui.push.v2.sdk.dto.req.message.ios.Alert; import com.getui.push.v2.sdk.dto.req.message.ios.Aps; import com.getui.push.v2.sdk.dto.req.message.ios.IosDTO; //import jakarta.annotation.Resource; import com.hxzkappboot.mapper.UserDao; import com.hxzkappboot.pojo.TbTaskScheduling; import com.hxzkappboot.pojo.TbUser; import com.hxzkappboot.service.TbTaskSchedulingService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @Slf4j @Component public class PushMsgUtils { // 消息推送 public static final String MESSAGE_PUSH = "1"; // 离线推送 public static final String OFFLINE_PUSH = "2"; @Resource(name = "myApiHelper") private ApiHelper myApiHelper; @Autowired UserDao userDao; @Autowired TbTaskSchedulingService tbTaskSchedulingService; @Scheduled(cron = "*/5 * * * * ?")//5秒执行一次 public void push() throws Exception { List tbUsers = userDao.findUserCid(); for (int i = 0; i < tbUsers.size(); i++) { List fenceList = new ArrayList(); fenceList = tbTaskSchedulingService.earlyWarning(); for (int j = 0; j < fenceList.size(); j++) { String cid = tbUsers.get(i).getCid(); String title = "预警通知"; String content = "系统预警:"+ fenceList.get(j).getFencename() + "开始工作,请尽快提前准备进入工作区域"; String type = PushMsgUtils.MESSAGE_PUSH; ApiResult>> mapApiResult = pushToSingleByCid(cid, title, content, type); System.out.println("mapApiResult = " + mapApiResult); } // String cid = "bef19f7079ee60765e75ffb546f908c0"; } } /** * 消息推送(离线推送)单cid推送 * cid 必填 设备 * title 必填 标题 * content 必填 内容 * linkUrl 必填 跳转链接 * type 必填 1:消息推送 2:离线推送 */ public ApiResult>> pushToSingleByCid(String cId , String title, String content, String type ) { PushDTO pushDTO = this.buildPushDTO(title, content, type); // 设置接收人信息 Audience audience = new Audience(); pushDTO.setAudience(audience); audience.addCid(cId); // 进行cid单推 PushApi pushApi = myApiHelper.creatApi(PushApi.class); ApiResult>> apiResult = pushApi.pushToSingleByCid(pushDTO); if (apiResult.isSuccess()) { // success log.info("推送成功"); System.out.println(apiResult.getData()); } else { // failed log.error("推送失败"); System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); } return apiResult; } /** * 消息推送(离线推送)单cid推送 * cid 必填 设备 * title 必填 标题 * content 必填 内容 * linkUrl 必填 跳转链接 * type 必填 1:消息推送 2:离线推送 */ public ApiResult>> pushToSingleByCIds(List cIds , String title, String content, String type ) { PushDTO pushDTO = this.buildPushDTO(title, content, type); // 设置接收人信息 Audience audience = new Audience(); pushDTO.setAudience(audience); audience.setCid(cIds); // 进行cid单推 PushApi pushApi = myApiHelper.creatApi(PushApi.class); ApiResult>> apiResult = pushApi.pushToSingleByCid(pushDTO); if (apiResult.isSuccess()) { // success log.info("推送成功"); System.out.println(apiResult.getData()); } else { // failed log.error("推送失败"); System.out.println("code:" + apiResult.getCode() + ", msg: " + apiResult.getMsg()); } return apiResult; } /** * 消息参数模板 */ private PushDTO buildPushDTO(String title, String content, String type) { PushDTO pushDTO = new PushDTO<>(); // 设置推送参数 //requestid需要每次变化唯一 pushDTO.setRequestId(System.currentTimeMillis() + ""); pushDTO.setGroupName("wxb-group"); //配置推送条件 // 1: 表示该消息在用户在线时推送个推通道,用户离线时推送厂商通道; // 2: 表示该消息只通过厂商通道策略下发,不考虑用户是否在线; // 3: 表示该消息只通过个推通道下发,不考虑用户是否在线; // 4: 表示该消息优先从厂商通道下发,若消息内容在厂商通道代发失败后会从个推通道下发。 Strategy strategy = new Strategy(); strategy.setDef(1); strategy.setSt(1); Settings settings = new Settings(); settings.setStrategy(strategy); pushDTO.setSettings(settings); //消息有效期,走厂商消息需要设置该值 settings.setTtl(3600000); PushChannel pushChannel = new PushChannel(); Map map = new HashMap<>(); map.put("title", title); map.put("content", content); map.put("type", type); // 转json对象 String payload = JSONUtils.toJSONString(map); // ========================= ios离线配置 ============================== //推送苹果离线通知标题内容 Alert alert = new Alert(); //苹果离线通知栏标题 alert.setTitle(title); //苹果离线通知栏内容 alert.setBody(content); Aps aps = new Aps(); //1表示静默推送(无通知栏消息),静默推送时不需要填写其他参数。 // 苹果建议1小时最多推送3条静默消息 aps.setContentAvailable(0); aps.setSound("default"); aps.setAlert(alert); IosDTO iosDTO = new IosDTO(); iosDTO.setPayload(payload); iosDTO.setAps(aps); iosDTO.setType("notify"); pushChannel.setIos(iosDTO); // =================== 安卓离线厂商通道推送消息体 =========================== AndroidDTO androidDTO = new AndroidDTO(); pushDTO.setPushChannel(pushChannel); pushChannel.setAndroid(androidDTO); Ups ups = new Ups(); androidDTO.setUps(ups); ThirdNotification notification1 = new ThirdNotification(); ups.setNotification(notification1); //安卓离线展示的标题 notification1.setTitle(title); //安卓离线展示的内容 notification1.setBody(content); notification1.setClickType("intent"); notification1.setIntent("intent:#Intent;action=android.intent.action.oppopush;" + "launchFlags=0x14000000;" + "component=包名/io.dcloud.PandoraEntry;S.UP-OL-SU=true;" + "S.title=" + title + ";" + "S.content="+ content + ";" + "S.payload=" + payload + ";end"); // notification1.setPayload(payload); //各厂商自有功能单项设置 // ups.addOption("HW", "/message/android/notification/badge/class", "io.dcloud.PandoraEntry "); // ups.addOption("HW", "/message/android/notification/badge/add_num", 1); // ups.addOption("HW", "/message/android/notification/importance", "HIGH"); // ups.addOption("VV","classification",1); //设置options 方式一 // ups.addOption("HW","badgeAddNum",3); // ups.addOption("HW","badgeClass","com.getui.demo.GetuiSdkDemoActivity"); // ups.addOption("OP","app_message_id",11); // ups.addOption("VV","message_sort",1); // ups.addOptionAll("channel","default"); // PushMessage在线走个推通道才会起作用的消息体 PushMessage pushMessage = new PushMessage(); pushDTO.setPushMessage(pushMessage); Map mapTC = new HashMap<>(); mapTC.put("title", title); mapTC.put("content", content); mapTC.put("payload", map); String jsonTC = JSONUtils.toJSONString(mapTC); pushMessage.setTransmission(jsonTC); log.info("pushDTO:{}", pushDTO); return pushDTO; } /** * 离线通知消息参数模板 * */ private PushDTO offlinePushDTO(String title, String content, String linkUrl, String type) { PushDTO pushDTO = new PushDTO<>(); // 设置推送参数 //requestid需要每次变化唯一 pushDTO.setRequestId(System.currentTimeMillis() + ""); pushDTO.setGroupName("wxb-group"); // PushMessage在线走个推通道才会起作用的消息体 Map map = new HashMap<>(); map.put("title", title); map.put("content", content); map.put("linkUrl", linkUrl); map.put("type", type); PushMessage pushMessage = new PushMessage(); pushDTO.setPushMessage(pushMessage); Map mapTC = new HashMap<>(); mapTC.put("title", title); mapTC.put("content", content); mapTC.put("payload", map); String jsonTC = JSONUtils.toJSONString(mapTC); System.err.println(jsonTC); pushMessage.setTransmission(jsonTC); log.info("pushDTO:{}", pushDTO); return pushDTO; } }