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 versionList, List 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 versionList, List 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 versionList, List 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 versionList, List 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 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 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 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 map) { HttpClient client = new HttpClient(); PostMethod method = new PostMethod(url); try { RequestEntity entity = new StringRequestEntity(body, "", "UTF-8"); if (map != null) { Set set = map.keySet(); Iterator 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 versionList, List systemList) { List androidList = new ArrayList(); List iosList = new ArrayList(); 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 versionList, List systemList, Object target) { List androidList = new ArrayList(); List iosList = new ArrayList(); 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 versionList, List systemList, Object target) { List androidList = new ArrayList(); List iosList = new ArrayList(); 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 versionList, List systemList, Object target) { List androidList = new ArrayList(); List iosList = new ArrayList(); 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 map = new HashMap(); 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); } }