admin
2021-01-04 aa6ef62aef83e277d4171df1d9f0803f91738216
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package com.newvideo.util.push;
 
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
 
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
 
import com.newvideo.domain.DetailSystem;
import com.newvideo.domain.VideoInfo;
import com.newvideo.domain.VideoIntersection;
import com.newvideo.service.imp.SystemService;
import com.newvideo.util.LogUtil;
import com.newvideo.util.StringUtil;
import com.newvideo.util.TimeUtil;
 
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
 
/**
 * 推送帮助类
 * 
 * @author Administrator
 * 
 */
public class PushUtil {
 
    public static String getSign(String body, String masterkey) {
        LogUtil.i(StringUtil.Md5("你好"));
        String st = "POST" + "http://msg.umeng.com/api/send" + body + masterkey;
        return StringUtil.Md5(st);
    }
 
    /**
     * 推送系统通知
     * 
     * @param title
     * @param content
     * @param extraVal
     * @return
     */
    public static boolean sendBroadcastAndroid(String title, String content, JSONObject extraVal,
            List<String> versionList, List<DetailSystem> detailSystem) {
        for (DetailSystem system : detailSystem)
            sendBroadcastAndroid(title, content, "1", null, "com.weikou.beibeivideo.ui.mine.SystemMessageActivity",
                    extraVal == null ? "" : extraVal.toString(), system.getUmengAppKey(), system.getUmengMasterKey(),
                    versionList);
        return true;
    }
 
    /**
     * 推送视频--android
     * 
     * @param title
     * @param content
     * @param info
     * @return
     */
    public static boolean sendAndroidVideoInfo(String title, String content, VideoInfo info, List<String> versionList,
            List<DetailSystem> detailSystem) {
        JSONObject obj = new JSONObject();
        obj.put("Id", info.getId());
        obj.put("ThirdType", info.getThirdType());
        obj.put("Share", info.getShare());
        for (DetailSystem system : detailSystem)
            sendBroadcastAndroid(title, content, "2", null, "com.weikou.beibeivideo.ui.media.VideoDetailActivity",
                    obj.toString(), system.getUmengAppKey(), system.getUmengMasterKey(), versionList);
 
        return true;
    }
 
    public static boolean sendAndroidUrl(String title, String content, String url, List<String> versionList,
            List<DetailSystem> detailSystem) {
        JSONObject obj = new JSONObject();
        obj.put("url", url);
        for (DetailSystem system : detailSystem)
            sendBroadcastAndroid(title, content, "3", null, "com.weikou.beibeivideo.ui.mine.BrowserActivity",
                    obj.toString(), system.getUmengAppKey(), system.getUmengMasterKey(), versionList);
 
        return true;
    }
 
    public static boolean sendAndroidIntersection(String title, String content, String t, String id,
            List<String> versionList, List<DetailSystem> detailSystem) {
        JSONObject obj = new JSONObject();
        obj.put("id", id);
        obj.put("title", t);
        obj.put("push", "true");
 
        for (int i = 0; i < detailSystem.size(); i++) {
            sendBroadcastAndroid(title, content, "4", null, "com.weikou.beibeivideo.ui.common.MVideosActivity",
                    obj.toString(), detailSystem.get(i).getUmengAppKey(), detailSystem.get(i).getUmengMasterKey(),
                    versionList);
        }
        return true;
    }
 
    private static boolean sendBroadcastAndroid(String title, String content, String custom, String[] device,
            String activity, String extraVal, String appkey, String masterkey, List<String> appversion) {
 
        JSONObject object = new JSONObject();
        object.put("appkey", appkey);
        object.put("timestamp", System.currentTimeMillis() + "");
        if (device == null || device.length == 0)
            object.put("type", "groupcast");
        else {
            object.put("type", "listcast");
            String device_tokens = "";
            for (String st : device) {
                device_tokens += st + ",";
            }
            device_tokens = device_tokens.substring(0, device_tokens.length() - 1);
            object.put("device_tokens", device_tokens);
        }
 
        JSONObject payJson = new JSONObject();
        payJson.put("display_type", "notification");
        JSONObject body = new JSONObject();
        body.put("ticker", title);
        body.put("title", title);
        body.put("text", content);
        body.put("custom", custom);
        // body.put("alias", "2508");
        body.put("after_open", "go_activity");
        body.put("activity", activity);// com.weikou.beibeivideo.ui.mine.SystemMessageActivity
        payJson.put("body", body);
        payJson.put("extra", extraVal);
        object.put("filter", getVersionFilter(appversion));
        object.put("payload", payJson);
        object.put("production_mode", "true");// true--正式设备 false--测试模式
        object.put("description", "Android");
        String url = "http://msg.umeng.com/api/send?sign=" + getSign(object.toString(), masterkey);
        String result = post(url, object.toString());
        JSONObject obj =JSONObject.fromObject(result);
        if (obj.optString("ret").equalsIgnoreCase("SUCCESS")) {
            return true;
        } else
            return false;
    }
 
    private static String getVersionFilter(List<String> versionList) {
        if (versionList == null || versionList.size() == 0)
            return "";
        JSONObject object = new JSONObject();
        JSONArray array = new JSONArray();
        for (String st : versionList) {
            JSONObject c = new JSONObject();
            c.put("app_version", st);
            array.add(c);
        }
 
        JSONObject ar = new JSONObject();
        ar.put("or", array);
        JSONArray arr = new JSONArray();
        arr.add(ar);
        JSONObject aj = new JSONObject();
        aj.put("and", arr);
        object.put("where", aj);
        LogUtil.i(object.toString());
        return object.toString();
    }
 
    private static boolean sendBroadcastIOS(String title, String content, String custom, String[] device,
            JSONObject extraVal, String appkey, String masterkey) {
 
        JSONObject object = new JSONObject();
        object.put("appkey", appkey);
        object.put("timestamp", System.currentTimeMillis() + "");
        if (device == null || device.length == 0)
            object.put("type", "broadcast");
        else {
            object.put("type", "listcast");
            String device_tokens = "";
            for (String st : device) {
                device_tokens += st + ",";
            }
            device_tokens = device_tokens.substring(0, device_tokens.length() - 1);
            object.put("device_tokens", device_tokens);
        }
 
        JSONObject payJson = new JSONObject();
        payJson.put("display_type", "notification");
        JSONObject aps = new JSONObject();
        aps.put("alert", title);
        payJson.put("aps", aps);
        JSONObject data = new JSONObject();
        data.put("type", "1");
        data.put("title", title);
        data.put("content", content);
        payJson.put("data", data);
        object.put("payload", payJson);
        object.put("production_mode", "false");
        object.put("description", "IOS");
        String url = "http://msg.umeng.com/api/send?sign=" + getSign(object.toString(), masterkey);
        String result = post(url, object.toString());
        JSONObject obj =JSONObject.fromObject(result);
        if (obj.optString("ret").equalsIgnoreCase("SUCCESS")) {
            return true;
        } else
            return false;
    }
 
    public static boolean sendBroadcastIos(String title, String content, JSONObject extraVal) {
        SystemService service = new SystemService();
        List<DetailSystem> list = service.getDetailSystemList();
 
        for (int i = 0; i < list.size(); i++) {
            if (list.get(i).getAppName().contains("神器看片")) {
                sendBroadcastIOS(title, content, "",
                        new String[] { "1b0ab38ea422f582eda3b04739b0007ac9bdddc5566092f675b05f55787d3452" }, extraVal,
                        list.get(i).getIosUmengAppKey(), list.get(i).getIosUmengMasterKey());
            }
        }
        return true;
    }
 
    public static String post(String url, String body, Map<String, String> map) {
        HttpClient client = new HttpClient();
        PostMethod method = new PostMethod(url);
        try {
            RequestEntity entity = new StringRequestEntity(body, "", "UTF-8");
            if (map != null) {
                Set<String> set = map.keySet();
                Iterator<String> its = set.iterator();
                while (its.hasNext()) {
                    String key = its.next();
                    method.setRequestHeader(key, map.get(key));
                }
            }
 
            method.setRequestEntity(entity);
            client.executeMethod(method);
            String responseBodyAsString = method.getResponseBodyAsString();
            LogUtil.i(responseBodyAsString);
            return responseBodyAsString;
        } catch (HttpException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
 
    public static String post(String url, String body) {
        return post(url, body, null);
    }
 
    public static void testIos() {
        sendBroadcastIos("测试标题123", "测试内容456", null);
    }
 
    /**
     * 推送通知
     * 
     * @param type
     *            2-视频 3-网页链接 4-合集
     * @param title
     * @param target
     */
    public static void sendBroadCastIOS(int type, String title, Object target, DetailSystem ds) {
        if (type == 2) {
            VideoInfo video = (VideoInfo) target;
            JSONObject obj = new JSONObject();
            obj.put("Id", video.getId());
            obj.put("ThirdType", video.getThirdType());
            obj.put("Share", video.getShare());
            // obj.put("Picture", video.getPicture());
            sendIosNotification(1, title, null, type + "", obj.toString(), ds);
        } else if (type == 3) {
            JSONObject obj = new JSONObject();
            obj.put("url", target.toString());
            sendIosNotification(1, title, null, type + "", obj.toString(), ds);
        } else if (type == 4) {
            VideoIntersection vi = (VideoIntersection) target;
            JSONObject obj = new JSONObject();
            obj.put("id", vi.getId());
            obj.put("title", vi.getName());
            sendIosNotification(1, title, null, type + "", obj.toString(), ds);
        }
 
    }
 
    public static void sendBroadCastIOS(String title, String content, Object target, DetailSystem ds) {
 
    }
 
    public static boolean sendText(String title, String content, List<String> versionList,
            List<DetailSystem> systemList) {
 
        List<DetailSystem> androidList = new ArrayList<DetailSystem>();
        List<DetailSystem> iosList = new ArrayList<DetailSystem>();
        for (DetailSystem sy : systemList) {
            if (sy.getPlatform() == 1) {// Android
                androidList.add(sy);
            } else {// IOS
                iosList.add(sy);
            }
        }
        if (androidList.size() > 0)
            sendBroadcastAndroid(title, content, null, versionList, androidList);
 
        if (iosList.size() > 0) {
            for (DetailSystem ds : iosList) {
                sendIosNotification(2, title, content, null, null, ds);
            }
        }
        return true;
    }
 
    public static boolean sendVideoInfo(String title, String content, List<String> versionList,
            List<DetailSystem> systemList, Object target) {
 
        List<DetailSystem> androidList = new ArrayList<DetailSystem>();
        List<DetailSystem> iosList = new ArrayList<DetailSystem>();
        for (DetailSystem sy : systemList) {
            if (sy.getPlatform() == 1) {// Android
                androidList.add(sy);
            } else {// IOS
                iosList.add(sy);
            }
        }
        if (androidList.size() > 0)
            sendAndroidVideoInfo(title, content, (VideoInfo) target, versionList, androidList);
 
        if (iosList.size() > 0) {
            for (DetailSystem ds : iosList) {
                sendBroadCastIOS(2, title, target, ds);
            }
        }
        return true;
    }
 
    public static boolean sendUrl(String title, String content, List<String> versionList, List<DetailSystem> systemList,
            Object target) {
        List<DetailSystem> androidList = new ArrayList<DetailSystem>();
        List<DetailSystem> iosList = new ArrayList<DetailSystem>();
        for (DetailSystem sy : systemList) {
            if (sy.getPlatform() == 1) {// Android
                androidList.add(sy);
            } else {// IOS
                iosList.add(sy);
            }
        }
        if (androidList.size() > 0)
            sendAndroidUrl(title, content, target.toString(), versionList, androidList);
 
        if (iosList.size() > 0) {
            for (DetailSystem ds : iosList) {
                sendBroadCastIOS(3, title, target, ds);
            }
        }
 
        return true;
    }
 
    public static boolean sendIntersection(String title, String content, List<String> versionList,
            List<DetailSystem> systemList, Object target) {
 
        List<DetailSystem> androidList = new ArrayList<DetailSystem>();
        List<DetailSystem> iosList = new ArrayList<DetailSystem>();
        for (DetailSystem sy : systemList) {
            if (sy.getPlatform() == 1) {// Android
                androidList.add(sy);
            } else {// IOS
                iosList.add(sy);
            }
        }
        if (androidList.size() > 0) {
            VideoIntersection vi = (VideoIntersection) target;
            sendAndroidIntersection(title, content, vi.getName(), vi.getId(), versionList, androidList);
        }
        if (iosList.size() > 0) {
            for (DetailSystem ds : iosList) {
                sendBroadCastIOS(4, title, target, ds);
            }
        }
        return true;
    }
 
    public static void sendIosNotification(int pushtype, String title, String content, String type, String paramsStr,
            DetailSystem ds) {
        JSONObject object = new JSONObject();
        object.put("platform", "all");
        object.put("audience", "all");
 
        if (pushtype == 1) {// 通知发送
            JSONObject ios = new JSONObject();
            ios.put("alert", title);
            ios.put("sound", "default");
            JSONObject data = new JSONObject();
            data.put("type", type);
            JSONObject params = new JSONObject();
            params.put("thirdType", "1");
            data.put("params", StringUtil.getBase64(paramsStr));
            ios.put("extras", data);
            JSONObject notification = new JSONObject();
            notification.put("ios", ios);
            object.put("notification", notification);
        } else if (pushtype == 2) {// 自定义消息发送
            JSONObject msgContent = new JSONObject();
            JSONObject in = new JSONObject();
            in.put("title", title);
            in.put("content", content);
            in.put("createtime", TimeUtil.getAllTime(System.currentTimeMillis()));
            msgContent.put("data", in);
 
            JSONObject message = new JSONObject();
            message.put("msg_content", msgContent);
            message.put("content_type", "text");
            message.put("title", title);
            object.put("message", message);
        }
        JSONObject options = new JSONObject();
        options.put("apns_production", false);
        object.put("options", options);
        Map<String, String> map = new HashMap<String, String>();
 
        map.put("Authorization",
                "Basic " + StringUtil.getBase64(ds.getUmengMasterKey() + ":" + ds.getIosUmengMasterKey()));
        String result = post("https://api.jpush.cn/v3/push", object.toString(), map);
        System.out.println(result);
    }
}