admin
2019-03-14 eed607d87b2eee1f09b4a28da614f3ad0b46601d
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
package com.yeshi.fanli.util.push;
 
import java.io.IOException;
import java.util.Map;
 
import javax.annotation.PostConstruct;
 
import org.json.simple.parser.ParseException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.yeshi.utils.JsonUtil;
 
import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Message.Builder;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;
import com.yeshi.fanli.entity.xinge.MessageInfo;
import com.yeshi.fanli.entity.xinge.PushRecord;
import com.yeshi.fanli.log.LogHelper;
import com.yeshi.fanli.log.PushLogHelper;
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.PropertiesUtil;
 
import net.sf.json.JSONObject;
 
@Component
public class XiaoMiPushUtil {
 
    private static XiaoMiPushUtil xiaoMiPushUtil;
 
    @Autowired
    private BusinessSystemService businessSystemService;
 
    @PostConstruct
    public void init() {
        xiaoMiPushUtil = this;
        xiaoMiPushUtil.businessSystemService = this.businessSystemService;
    }
 
    // AppKey
    private static String XIAOMI_F_ANDROID_APP_KEY = PropertiesUtil.getMap().get("xiaomi_f_android_app_key");
 
    // AppSecert
    private static String XIAOMI_F_ANDROID_APP_SECERT = PropertiesUtil.getMap().get("xiaomi_f_android_app_secert");
 
    /**
     * 
     * 方法说明: 小米推送安卓(全推)
     * 
     * @author mawurui createTime 2018年3月7日 上午9:57:56
     * @param info
     * @param params
     * @param system
     * @return
     */
    public static String allPushAndroidForXM(MessageInfo info, Map<String, Object> map) {
        String name = info.getPackageName();
        if (!Constant.systemCommonConfig.getAndroidPackageName().equalsIgnoreCase(name)
                && !Constant.systemCommonConfig.getIosBundleId().equalsIgnoreCase(name)) {
            return null;
        }
 
        // 从info中取页面传来的值
        Constants.useOfficial(); // 启动推送方法
        String PackageName = info.getPackageName();
        Sender sender = new Sender(XIAOMI_F_ANDROID_APP_SECERT); // 申请的AppSecert
        String title = info.getTitle();
        String payload = info.getContent();
        String description = info.getContent();
 
        Message message = null; // 做循环传来的key
        Builder builder = new Message.Builder().restrictedPackageName(PackageName).title(title).payload(payload)
                .description(description).passThrough(0) // 设置消息是否通过透传方式至App,
                                                            // 1表示透传,0表示通知栏消息(默认)
                .notifyType(1); // 设置通知类型, type类型(1-默认提示音, 2-使用默认震动提示,
                                // 3-使用默认led灯光提示)
        // 将extra放入MessageInfo对象中, 循环遍历extra中的key 和 value
        if (map != null) {
            for (String key : map.keySet()) {
                builder.extra(key, map.get(key).toString());
            }
        }
        message = builder.build();
        try {
            Result result = sender.broadcastAll(message, 3);
            return result.getMessageId();// 成功 返回消息的Id, 失败返回null
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 
     * 方法说明: 小米推送安卓(单推)
     * 
     * @author mawurui createTime 2018年3月7日 下午5:09:18
     * @param info
     * @param params
     * @param system
     * @return
     */
    public static String singlePushAndroidForXM(MessageInfo info, Map<String, Object> map, System system) {
        String name = info.getPackageName();
        if (!Constant.systemCommonConfig.getAndroidPackageName().equalsIgnoreCase(name)
                && !Constant.systemCommonConfig.getIosBundleId().equalsIgnoreCase(name)) {
            return null;
        }
        Constants.useOfficial(); // 启动推送方法
        String PackageName = info.getPackageName();
        Sender sender = new Sender(XIAOMI_F_ANDROID_APP_SECERT);
        String title = info.getTitle();
        String content = info.getContent();
        String uId = info.getAlias();
 
        Message message = null; // 做循环传来的key
        Builder builder = new Message.Builder().restrictedPackageName(PackageName).title(title).description(content)
                .passThrough(0) // 设置消息是否通过透传方式至App, 1表示透传,0表示通知栏消息(默认)
                .notifyType(1); // 设置通知类型, type类型(1-默认提示音, 2-使用默认震动提示,
                                // 3-使用默认led灯光提示)
        // 将extra放入MessageInfo对象中, 循环遍历extra中的key 和 value
        if (map != null) {
            for (String key : map.keySet()) {
                builder.extra(key, map.get(key).toString());
            }
        }
        message = builder.build();
        try {
            Result result = sender.sendToAlias(message, uId, 3);
            PushLogHelper.xmInfo("推送结果:" + result.getReason());
            return result.getMessageId();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
 
    /**
     * 
     * 方法说明: 小米推送红包
     * 
     * @author mawurui createTime 2018年3月8日 上午9:55:14
     * @param info
     */
    public static void pushByHongBao(String alias, System system) {
        MessageInfo info = new MessageInfo();
        info.setAlias(alias);
        info.setTitle("新到红包");
        info.setDescription("红包到来了...");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "hongbao");
        XiaoMiPushUtil.singlePushAndroidForXM(info, JsonUtil.parseData(jsonObject), system);
    }
 
    /**
     * 
     * 方法说明: 小米推送提现申请
     * 
     * @author mawurui createTime 2018年3月8日 上午10:00:16
     * @param info
     */
    public static void pushByApply(String alias, System system) {
        MessageInfo info = new MessageInfo();
        info.setAlias(alias);
        info.setTitle("提现申请");
        info.setDescription("提现审核中...");
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "cash");
        XiaoMiPushUtil.singlePushAndroidForXM(info, JsonUtil.parseData(jsonObject), system);
    }
 
    /**
     * 
     * 方法说明: 推送提现状态
     * 
     * @author mawurui createTime 2018年3月8日 上午10:26:34
     * @param alias
     * @param state
     * @param system
     */
    public static void pushByExtract(String alias, int state, System system) {
        MessageInfo info = new MessageInfo();
        info.setAlias(alias);
        if (state == 0) {
            info.setTitle("提现成功");
            info.setDescription("您有一笔交易,已提现成功!");
        } else if (state == 1) {
            info.setTitle("提现失败");
            info.setDescription("您有一笔交易,提现失败!");
        } else {
            info.setTitle("提现失败");
            info.setDescription("提现被拒绝");
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("type", "cash");
        XiaoMiPushUtil.singlePushAndroidForXM(info, JsonUtil.parseData(jsonObject), system);
    }
 
 
    /**
     * 
     * @param info
     * @param params
     * @param pushRecord
     * @return 1:都成功 2:仅android 成功 3.仅IOS成功 4.都失败
     */
    private static int pushApp(MessageInfo info, JSONObject json, PushRecord pushRecord, System b_android) {
        // jsonXm 转换为 map
        JSONObject mapXm = JSONObject.fromObject(json);
        String android = allPushAndroidForXM(info, mapXm);
        LogHelper.userInfo("安卓推送测试:" + android);
        pushRecord.setAndroidPushId(android);
        if (android != null) {
            return 1;
        } else {
            return 4;
        }
    }
 
    public static void pushZNX(Long uid, String title, String content, String msgId) {
        // 小米开始推送
        MessageInfo info = new MessageInfo();
        info.setActivty(
                String.format("%s.ui.BrowserActivity", Constant.systemCommonConfig.getAndroidBaseactivityName()));
        info.setAlias(uid + "");
        info.setTitle(title);
        info.setContent(content);
        info.setPackageName(Constant.systemCommonConfig.getAndroidPackageName());
        JSONObject json = new JSONObject();
        json.put("type", "ZNX");
        json.put("miPushUrl", String.format("%s.ui.mine.AppMailDetailActivity",
                Constant.systemCommonConfig.getAndroidBaseactivityName()));
 
        JSONObject contentJson = new JSONObject();
        contentJson.put("id", msgId);
        contentJson.put("title", title);
        contentJson.put("content", content);
        contentJson.put("isOpen", 0);
        contentJson.put("createTime", java.lang.System.currentTimeMillis());
        json.put("content", contentJson);
        JSONObject mapXm = JSONObject.fromObject(json);
        XiaoMiPushUtil.singlePushAndroidForXM(info, mapXm, null);
    }
 
}