package com.newvideo.util; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.log4j.Logger; import org.json.JSONArray; import org.json.JSONObject; import com.newvideo.domain.VideoInfo; import com.tencent.xinge.ClickAction; import com.tencent.xinge.Message; import com.tencent.xinge.MessageIOS; import com.tencent.xinge.Style; import com.tencent.xinge.XingeApp; 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 iosUserList, List 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 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 custom = new HashMap(); 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 iosUserList) { MessageIOS messageIOS = new MessageIOS(); messageIOS.setAlert("《" + info.getName() + "》" + info.getTag()); // messageIOS.setBadge(1); // messageIOS.setSound("beep.wav"); Map custom = new HashMap(); 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 iosTags, List 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 custom = new HashMap(); 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 custom = new HashMap(); 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(); 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 custom = new HashMap(); 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(); 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 custom = new HashMap(); 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(); 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 getAndroidTags() { List tagList = new ArrayList(); 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 getIosTags() { List tagList = new ArrayList(); 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; } }