admin
2022-04-16 04f09e52ffd4681bdfd85e51acd3da0d1280c3d3
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
package com.yeshi.buwan.util;
 
import com.tencent.xinge.Message;
import com.tencent.xinge.MessageIOS;
import com.tencent.xinge.Style;
import com.tencent.xinge.XingeApp;
import com.yeshi.buwan.domain.VideoInfo;
import org.apache.log4j.Logger;
import org.json.JSONArray;
import org.json.JSONObject;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
public class XingePushUtil {
    static Logger log = Logger.getLogger(XingePushUtil.class);
 
    public static long ACCESS_ID_ANDROID = 2100218527L;
    public static String ACCESS_KEY_ANDROID = "A5H4W4C3RG8T";
    public static String SECRET_KEY_ANDROID = "bf6073e3af796391ab8642a3ed69b627";
 
    public static long ACCESS_ID_IOS = 2200218652L;
    public static String ACCESS_KEY_IOS = "I17UVY2C2S9Y";
    public static String SECRET_KEY_IOS = "eac2eacbdebafaabafeca3a6535bd3f8";
 
    public static int IOS_ENV = XingeApp.IOSENV_PROD;
 
    static XingeApp androidPush = new XingeApp(ACCESS_ID_ANDROID, SECRET_KEY_ANDROID);
    static XingeApp iosPush = new XingeApp(ACCESS_ID_IOS, SECRET_KEY_IOS);
 
    // 按账号推送视频更新
    public static boolean pushVideoUpdateByAccount(String resourceId, String detailid, VideoInfo info,
            List<String> iosUserList, List<String> androidUserList) {
        if (iosUserList != null && iosUserList.size() > 0)
            pushVideoUpdateByAccountIos(resourceId, detailid, info, iosUserList);
 
        if (androidUserList != null && androidUserList.size() > 0)
            pushVideoUpdateByAccountAndroid(resourceId, detailid, info, androidUserList);
        return true;
    }
 
    private static boolean pushVideoUpdateByAccountAndroid(String resourceId, String detailid, VideoInfo info,
            List<String> androidUserList) {
        Message message = new Message();
        message.setExpireTime(60 * 60 * 24);// 存放一天
        message.setTitle("布丸君提醒:您关注的视频更新啦!");
        message.setContent("《" + info.getName() + "》" + info.getTag());
        message.setType(Message.TYPE_NOTIFICATION);
        message.setMultiPkg(1);
        // #含义:样式编号0,响铃,震动,不可从通知栏清除,不影响先前通知
        message.setStyle(new Style(0, 1, 1, 0, 0));
        // ClickAction action = new ClickAction();
        // action.setActionType(ClickAction.TYPE_INTENT);
        // //action.setActivity("com.weikou.beibeivideo.ui.media.VideoDetailActivity");
        // // action.set
        // message.setAction(action);
        // 跳转Activity的参数
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("Activity", "com.weikou.beibeivideo.ui.media.VideoDetailActivity");
        custom.put("Id", info.getId());
        custom.put("ResourceId", resourceId);
        custom.put("DetailId", detailid);
        custom.put("Share", "0");
        custom.put("ThirdType", "0");
        custom.put("PushType", "VideoUpdate");
        message.setCustom(custom);
        message.setSendTime(TimeUtil.getGernalTime(System.currentTimeMillis() + 1000, "yyyy-MM-dd HH:mm:ss"));
        JSONObject result = androidPush.createMultipush(message);
        log.info(result.toString());
        long pushId = 0;
        if (result.optInt("ret_code") == 0) {
            pushId = Long.parseLong(result.optJSONObject("result").optString("push_id"));
        }
 
        if (pushId > 0) {
            if (androidUserList != null && androidUserList.size() > 0) {
                result = androidPush.pushAccountListMultiple(pushId, androidUserList);
                log.info(result.toString());
                System.out.println(result.toString());
                return result.optInt("ret_code") == 0;
            }
        }
        return false;
    }
 
    private static boolean pushVideoUpdateByAccountIos(String resourceId, String detailid, VideoInfo info,
            List<String> iosUserList) {
        MessageIOS messageIOS = new MessageIOS();
 
        messageIOS.setAlert("《" + info.getName() + "》" + info.getTag());
        // messageIOS.setBadge(1);
        // messageIOS.setSound("beep.wav");
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("PushType", "pushVideoUpdateByAccount");
        JSONObject params = new JSONObject();
        params.put("Id", info.getId());
        params.put("ResourceId", resourceId);
        params.put("DetailId", detailid);
        params.put("ThirdType", "0");
        custom.put("Params", params.toString());
        messageIOS.setCustom(custom);
        messageIOS.setExpireTime(60 * 60 * 24);
        messageIOS.setCategory("Category");
        JSONObject result = iosPush.createMultipush(messageIOS, IOS_ENV);// 暂时开发环境推送
        log.info(result.toString());
        long pushId = 0;
        if (result.optInt("ret_code") == 0) {
            pushId = Long.parseLong(result.optJSONObject("result").optString("push_id"));
        }
        if (pushId > 0) {
            if (iosUserList != null && iosUserList.size() > 0) {
                result = iosPush.pushAccountListMultiple(pushId, iosUserList);
                log.info(result.toString());
                System.out.println(result.toString());
                return result.optInt("ret_code") == 0;
            }
        }
        return false;
    }
 
    // 推送系统消息
    public static boolean pushSystemMessage(String title, String content, List<String> iosTags,
            List<String> androidTag) {
        Message message = new Message();
        message.setMultiPkg(1);
        message.setContent(content);
        message.setTitle(title);
        message.setType(Message.TYPE_MESSAGE);
 
        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setAlert(title);
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("PushType", "pushSystemMessage");
        custom.put("Title", title);
        custom.put("Content", content);
        messageIOS.setCustom(custom);
 
        JSONObject result = androidPush.pushTags(0, androidTag, "OR", message);
        log.info(result);
        result = iosPush.pushTags(0, iosTags, "OR", messageIOS, IOS_ENV);
        log.info(result);
 
        return true;
    }
 
    // 推送网页链接消息
    public static boolean pushUrlMessage(String title, String content, String url) {
        Message message = new Message();
        message.setMultiPkg(1);
        message.setContent(content);
        message.setTitle(title);
        message.setType(Message.TYPE_NOTIFICATION);
        message.setStyle(new Style(0, 1, 1, 0, 0));
        // 跳转Activity的参数
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("url", url);
        custom.put("Activity", "com.weikou.beibeivideo.ui.mine.BrowserActivity");
        message.setCustom(custom);
        log.info(androidPush.pushAllDevice(0, message));
 
        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setAlert(title);
        custom = new HashMap<String, Object>();
        custom.put("PushType", "pushUrlMessage");
        custom.put("Url", url);
        messageIOS.setCustom(custom);
        log.info(iosPush.pushAllDevice(0, messageIOS, IOS_ENV));
        return true;
    }
 
    // 推送合辑
 
    public static boolean pushSpecialMessage(String content, String specialId) {
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle("布丸君提醒您:");
        message.setContent(content);
        message.setType(Message.TYPE_NOTIFICATION);
        message.setStyle(new Style(0, 1, 1, 0, 0));
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("Activity", "com.weikou.beibeivideo.ui.category.SpecificDetailActivity");
        custom.put("id", specialId);
        message.setCustom(custom);
        JSONObject result = androidPush.pushAllDevice(0, message);
        log.info(result);
 
        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setAlert(content);
        messageIOS.setBadge(1);
        messageIOS.setSound("beep.wav");
        custom = new HashMap<String, Object>();
        custom.put("PushType", "pushSpecialMessage");
        custom.put("Id", specialId);
        messageIOS.setCustom(custom);
        result = iosPush.pushAllDevice(0, messageIOS, IOS_ENV);
        log.info(result);
 
        return true;
    }
 
    // 推送评论回复消息
    public static boolean pushCommentMessage(String messageStr, String account) {
        boolean androidSuccess = false;
        boolean iosSuccess = false;
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle("布丸君提醒您:");
        message.setContent(messageStr);
        message.setType(Message.TYPE_NOTIFICATION);
        message.setStyle(new Style(0, 1, 1, 0, 0));
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("Activity", "com.weikou.beibeivideo.ui.mine.SystemMessageActivity");
        message.setCustom(custom);
        JSONObject result = androidPush.pushSingleAccount(0, account, message);
        log.info(result);
        androidSuccess = result.optInt("ret_code") == 0;
 
        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setAlert(messageStr);
        messageIOS.setBadge(1);
        messageIOS.setSound("beep.wav");
        custom = new HashMap<String, Object>();
        custom.put("PushType", "pushCommentMessage");
        messageIOS.setCustom(custom);
        result = iosPush.pushSingleAccount(0, account, messageIOS, IOS_ENV);
        log.info(result);
        iosSuccess = result.optInt("ret_code") == 0;
        return (androidSuccess || iosSuccess);
    }
 
    // 获取Android Tag
    public static List<String> getAndroidTags() {
        List<String> tagList = new ArrayList<String>();
        JSONObject obj = androidPush.queryTags();
        if (obj.optInt("ret_code") == 0) {
            JSONArray array = obj.optJSONObject("result").optJSONArray("tags");
            for (int i = 0; i < array.length(); i++) {
                tagList.add(array.optString(i));
            }
        }
        return tagList;
    }
 
    // 获取IOS Tag
    public static List<String> getIosTags() {
        List<String> tagList = new ArrayList<String>();
        JSONObject obj = iosPush.queryTags();
        if (obj.optInt("ret_code") == 0) {
            JSONArray array = obj.optJSONObject("result").optJSONArray("tags");
            for (int i = 0; i < array.length(); i++) {
                tagList.add(array.optString(i));
            }
        }
        return tagList;
    }
 
    public static String pushVideoForAndriod(String title,String content,VideoInfo vi){
        Message message = new Message();
        message.setMultiPkg(1);
        message.setTitle(title);
        message.setContent(content);
        message.setType(Message.TYPE_NOTIFICATION);
        message.setStyle(new Style(0, 1, 1, 0, 0));
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("Activity", "com.weikou.beibeivideo.ui.media.VideoDetailActivity");
        custom.put("Id", vi.getId());
        custom.put("ResourceId", vi.getResourceList().get(0).getId());
        custom.put("DetailId", "1");
        custom.put("Share", "0");
        custom.put("ThirdType", "0");
        custom.put("PushType", "VideoUpdate");
        message.setCustom(custom);
        JSONObject result = androidPush.pushAllDevice(0,message);
//        JSONObject result = androidPush.pushSingleAccount(0, "376133", message);
        Integer code = (Integer) result.get("ret_code");
        if(0==code){
            String pushId = String.valueOf(result.getJSONObject("result").get("push_id"));
            return pushId;
        }
        return null;
    }
    
    public static String pushVideoForIOS(String content,VideoInfo vi){
        MessageIOS messageIOS = new MessageIOS();
        messageIOS.setAlert(content);
        messageIOS.setBadge(1);
        messageIOS.setSound("beep.wav");
        Map<String, Object> custom = new HashMap<String, Object>();
        custom.put("PushType", "pushVideoUpdateByAccount");
        JSONObject params = new JSONObject();
        params.put("Id", vi.getId());
        params.put("ResourceId", vi.getResourceList().get(0).getId());
        params.put("DetailId", "1");
        params.put("ThirdType", "0");
        custom.put("Params", params.toString());
        messageIOS.setCustom(custom);
        messageIOS.setExpireTime(60 * 60 * 24);
        messageIOS.setCategory("Category");
        org.json.JSONObject result = iosPush.pushAllDevice(0,messageIOS,XingeApp.IOSENV_DEV);
        Integer code = (Integer) result.get("ret_code");
        if(0==code){
            String pushId = String.valueOf(result.getJSONObject("result").get("push_id"));
            return pushId;
        }
        return null;
    }
    
    public static int pushVideo(String title,String content,VideoInfo vi){
        String andriod = pushVideoForAndriod(title, content, vi);
        String ios = pushVideoForIOS(content, vi);
        if(andriod == null && ios==null){
            return 0;
        }else if(andriod == null && ios !=null){
            return 1;
        }else if(andriod != null && ios==null){
            return 2;
        }else{
            return 3;
        }
    }
    
}