admin
2024-10-16 7fa83e5dd03f7896bd1d1e8c47f5e926ff3d4ba0
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
package com.ks.push.utils;
 
import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.ks.push.pojo.DO.BPushMessage;
import com.ks.push.pojo.DO.PushPlatform;
import com.ks.push.utils.push.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.yeshi.utils.StringUtil;
import org.yeshi.utils.push.entity.PushAppInfo;
import org.yeshi.utils.push.entity.PushMessage;
 
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
 
public class PushUtil {
 
    static Logger logger = LoggerFactory.getLogger(PushUtil.class);
 
    /**
     * 推送通知
     *
     * @param platform
     * @param appInfo
     * @param pushMessage
     * @param tokenList
     * @throws Exception
     */
    public static void pushNotifyCation(PushPlatform platform, PushAppInfo appInfo, BPushMessage pushMessage, String identifyCode, List<String> tokenList) throws Exception {
        if (appInfo == null) {
            throw new Exception("应用参数为空");
        }
 
        if (pushMessage == null) {
            throw new Exception("消息为空");
        }
 
        if (tokenList == null || tokenList.size() == 0) {
            throw new Exception("token为空");
        }
 
        logger.info("{}推送应用信息:{}", platform.name(), new Gson().toJson(appInfo));
        logger.info("{}推送内容信息:{}", platform.name(), new Gson().toJson(pushMessage));
        logger.info("{}推送token信息:{}", platform.name(), new Gson().toJson(tokenList));
 
 
        Map<String, String> extras = null;
        if (pushMessage.getExtras() != null) {
            extras = new HashMap<>();
            for (Iterator<String> its = pushMessage.getExtras().keySet().iterator(); its.hasNext(); ) {
                String key = its.next();
                String value = pushMessage.getExtras().get(key);
                if (!StringUtil.isNullOrEmpty(key) && !StringUtil.isNullOrEmpty(value)) {
                    extras.put(key, value);
                }
            }
        }
 
        PushMessage message = new PushMessage(pushMessage.getTitle(), pushMessage.getContent(), pushMessage.getAndroidActivity(), pushMessage.getAndroidActivityScheme(), pushMessage.getAndroidHostPath(), extras);
 
        if (platform == PushPlatform.xm) {
            pushXM(appInfo, message, tokenList);
        } else if (platform == PushPlatform.hw) {
            pushHW(appInfo, message, identifyCode, tokenList);
        } else if (platform == PushPlatform.oppo) {
            pushOPPO(appInfo, message, tokenList);
        } else if (platform == PushPlatform.vivo) {
            pushVIVO(appInfo, message, tokenList);
        } else if (platform == PushPlatform.mz) {
            pushMZ(appInfo, message, tokenList);
        } else if (platform == PushPlatform.jpush) {
            pushJPush(appInfo, message, tokenList,!pushMessage.isMessage());
        }
    }
 
    /**
     * 小米推送
     *
     * @param appInfo
     * @param message
     * @param tokenList
     * @throws Exception
     */
    private static void pushXM(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
        XiaoMiPushUtil.pushNotificationByRegIds(appInfo, message, tokenList);
    }
 
    /**
     * 华为推送
     *
     * @param appInfo
     * @param message
     * @param bigTag
     * @param tokenList
     * @throws Exception
     */
    private static void pushHW(PushAppInfo appInfo, PushMessage message, String bigTag, List<String> tokenList) throws Exception {
        HuaWeiPushUtil.pushNotificationByTokens(appInfo, message, bigTag, tokenList);
    }
 
    /**
     * OPPO推送
     *
     * @param appInfo
     * @param message
     * @param tokenList
     * @throws Exception
     */
    private static void pushOPPO(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
        OppoPushUtil.pushNotificationByRegIds(appInfo, message, tokenList);
    }
 
    /**
     * VIVO推送
     *
     * @param appInfo
     * @param message
     * @param tokenList
     * @throws Exception
     */
    private static void pushVIVO(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
        VIVOPushUtil.pushNotificationByRegIds(appInfo, message, tokenList);
    }
 
    /**
     * 魅族推送
     *
     * @param appInfo
     * @param message
     * @param tokenList
     * @throws Exception
     */
    private static void pushMZ(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
        MeiZuPushUtil.pushNotificationByPushId(appInfo, message, tokenList);
    }
 
 
    /**
     * 极光推送
     *
     * @param appInfo
     * @param message
     * @param tokenList
     * @throws Exception
     */
    private static void pushJPush(PushAppInfo appInfo, PushMessage message, List<String> tokenList, boolean alert) throws Exception {
        if (alert) {
            JpushUtil.pushNotification(appInfo, message, tokenList, null);
        } else {
            JSONObject data = new JSONObject();
            if (message.getActivityParams() != null) {
                for (Iterator<String> its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) {
                    String key = its.next();
                    data.put(key, message.getActivityParams().get(key));
                }
            }
            JpushUtil.pushMessage(appInfo, data, tokenList, null);
        }
    }
 
 
}