admin
2022-01-12 4a7367a869ef12375ea6678ca44e102b8919c624
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
package com.ks.push.controller;
 
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ks.push.dto.mq.InvalidDeviceTokenInfo;
import com.ks.push.manager.BPushPlatformAppInfoManager;
import com.ks.push.manager.CMQManager;
import com.ks.push.pojo.DO.PushPlatform;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.wx.WXUtil;
 
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
 
/**
 * @author Administrator
 * @title: PushCallbackController
 * @description: 各大手机推送平台的消息回执
 * @date 2021/9/15 12:17
 */
@Controller
@RequestMapping("callback")
public class PushCallbackController {
    Logger logger = LoggerFactory.getLogger(PushCallbackController.class);
 
    @Resource
    private BPushPlatformAppInfoManager bPushPlatformAppInfoManager;
 
 
    //回调接口详情:https://developer.huawei.com/consumer/cn/doc/development/HMSCore-Guides/msg-receipt-guide-0000001050040176#ZH-CN_TOPIC_0000001087208860__p121151147184318
    @ResponseBody
    @RequestMapping("hw")
    public String hw(HttpServletRequest request) {
        String content = WXUtil.getContent(request);
        if (StringUtil.isNullOrEmpty(content)) {
            return "";
        }
        JSONObject json = JSONObject.parseObject(content);
        JSONArray statuses = json.getJSONArray("statuses");
        for (int i = 0; i < statuses.size(); i++) {
            JSONObject statuse = statuses.getJSONObject(i);
            String appId = statuse.getString("appid");
            String bigTag = statuse.getString("biTag");
            String token = statuse.getString("token");
            String appCode = bPushPlatformAppInfoManager.selectAppCode(appId, PushPlatform.hw);
            //0-成功
            //2-应用未安装
            //5-指定的Token在当前Android终端用户下不存在
            //6-通知栏消息不展示
            //10-非活跃设备消息,设备为非活跃设备(终端设备未接入网络达30天),消息不进行下发。
            //15-离线用户消息覆盖
            //27-在终端设备上目标应用进程不存在导致透传消息被缓存
            //102-消息频控丢弃
            //144-profileId不存在
            //201-消息发送管控
            int status = statuse.getInteger("status");
            if (status == 0) {
                logger.debug("华为消息回执-推送成功:bigTag-{}  token-{}", bigTag, token);
            } else {
                logger.debug("华为消息回执-推送失败:bigTag-{}  token-{} status-{}", bigTag, token, status);
                if (status == 2 || status == 5 || status == 10) {
                    //删除无效设备
                    CMQManager.getInstance().addInvalidDevieToken(new InvalidDeviceTokenInfo(appCode, PushPlatform.hw, token));
                }
            }
        }
 
 
        JSONObject data = new JSONObject();
        data.put("code", 0);
        data.put("message", "success");
        return data.toJSONString();
    }
 
 
    //回执消息的格式:https://dev.mi.com/console/doc/detail?pId=1278#_3_6
    @ResponseBody
    @RequestMapping("xm")
    public String xm(HttpServletRequest request) {
        String content = WXUtil.getContent(request);
        logger.debug("小米消息回执-{}", content);
        JSONObject json = JSONObject.parseObject(content);
 
        for (String msgId : json.keySet()) {
            JSONObject data = json.getJSONObject(msgId);
            String appId = data.getString("param");
            String appCode = bPushPlatformAppInfoManager.selectAppCode(appId, PushPlatform.xm);
            int type = data.getInteger("type");
            switch (type) {
                //送达
                case 1:
                    break;
                //点击
                case 2:
                    break;
                //无法找到目标设备
                case 16:
                    String regIdStr = data.getString("targets");
                    String[] regIds = regIdStr.split(",");
                    for (String rid : regIds) {
                        //删除无效设备
                        CMQManager.getInstance().addInvalidDevieToken(new InvalidDeviceTokenInfo(appCode, PushPlatform.xm, rid));
                    }
                    break;
            }
        }
 
 
        JSONObject data = new JSONObject();
        data.put("code", 0);
        data.put("message", "success");
        return "";
    }
 
 
    @ResponseBody
    @RequestMapping("oppo")
    public String oppo(HttpServletRequest request) {
        String content = WXUtil.getContent(request);
        logger.debug("oppo消息回执-{}", content);
        JSONObject data = new JSONObject();
        data.put("code", 0);
        data.put("message", "success");
        return "";
    }
 
}