fei.wang
2025-04-18 321a74059773cfecc01d6313f7c2e2d45545d6d3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
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<TbUser> tbUsers =  userDao.findUserCid();
        for (int i = 0; i < tbUsers.size(); i++) {
            List<TbTaskScheduling> 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<Map<String, Map<String, String>>> mapApiResult = pushToSingleByCid(cid, title, content, type);
                System.out.println("mapApiResult = " + mapApiResult);
            }
//            String cid = "bef19f7079ee60765e75ffb546f908c0";
 
        }
    }
 
    /**
     * 消息推送(离线推送)单cid推送
     * cid 必填 设备
     * title 必填 标题
     * content 必填 内容
     * linkUrl 必填 跳转链接
     * type 必填 1:消息推送 2:离线推送
     */
    public ApiResult<Map<String, Map<String, String>>> pushToSingleByCid(String cId ,
                                                                         String title,
                                                                         String content,
                                                                         String type ) {
        PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, type);
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.addCid(cId);
        // 进行cid单推
        PushApi pushApi = myApiHelper.creatApi(PushApi.class);
        ApiResult<Map<String, Map<String, String>>> 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<Map<String, Map<String, String>>> pushToSingleByCIds(List<String> cIds ,
                                                                          String title,
                                                                          String content,
                                                                          String type ) {
        PushDTO<Audience> pushDTO = this.buildPushDTO(title, content, type);
        // 设置接收人信息
        Audience audience = new Audience();
        pushDTO.setAudience(audience);
        audience.setCid(cIds);
        // 进行cid单推
        PushApi pushApi = myApiHelper.creatApi(PushApi.class);
        ApiResult<Map<String, Map<String, String>>> 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<Audience> buildPushDTO(String title, String content, String type) {
        PushDTO<Audience> 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<String, String> 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<String, Object> 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<Audience> offlinePushDTO(String title, String content, String linkUrl, String type) {
        PushDTO<Audience> pushDTO = new PushDTO<>();
        // 设置推送参数
        //requestid需要每次变化唯一
        pushDTO.setRequestId(System.currentTimeMillis() + "");
        pushDTO.setGroupName("wxb-group");
        // PushMessage在线走个推通道才会起作用的消息体
 
        Map<String, String> 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<String, Object> 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;
    }
}