From 3a84f708ddb9087993808bb491a2dc3453eaf08a Mon Sep 17 00:00:00 2001 From: admin <2780501319@qq.com> Date: 星期五, 27 九月 2019 00:04:46 +0800 Subject: [PATCH] VIVO推送集成 --- fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java | 301 +++++++++++++++++++++++++++++++++++++++---------- 1 files changed, 239 insertions(+), 62 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java index ed4abcd..21b4662 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java @@ -1,21 +1,19 @@ package com.yeshi.fanli.util.push; -import java.io.IOException; -import java.io.UnsupportedEncodingException; import java.util.HashMap; -import java.util.Iterator; +import java.util.HashSet; +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.StringRequestEntity; -import org.yeshi.utils.HttpUtil; - -import com.yeshi.fanli.exception.push.VIVOPushException; +import com.vivo.push.sdk.notofication.Message; +import com.vivo.push.sdk.notofication.Result; +import com.vivo.push.sdk.notofication.TargetMessage; +import com.vivo.push.sdk.server.Sender; +import com.yeshi.fanli.dto.push.PushTypeEnum; +import com.yeshi.fanli.exception.push.PushException; +import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; - -import net.sf.json.JSONObject; /** * VIVO鐨勬帹閫佸府鍔� @@ -25,41 +23,11 @@ */ public class VIVOPushUtil { - /** - * POST璇锋眰 - * - * @param url - * @param body - * @param headers - * @return - */ - private static String post(String url, String body, Map<String, String> headers) { - HttpClient httpClient = new HttpClient(); - PostMethod pm = new PostMethod(url); - try { - pm.setRequestEntity(new StringRequestEntity(body, "application/json", "UTF-8")); - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); - } - - if (headers != null) { - Iterator<String> its = headers.keySet().iterator(); - while (its.hasNext()) { - String key = its.next(); - pm.setRequestHeader(key, headers.get(key)); - } - } - - try { - httpClient.executeMethod(pm); - return pm.getResponseBodyAsString(); - } catch (HttpException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - return null; - } + private static final int APP_ID = 15206; + private static final String APP_KEY = "996c07d6-ec2b-41ef-b7c9-abb51ac63abf"; + private static final String APP_SECRET = "882548de-8183-4d29-9769-48b2bb4169c8"; + static String authToken = null; + static long authTokenTime = 0L; /** * 鑾峰彇鎺堟潈鐮� @@ -68,23 +36,232 @@ * @param appKey * @param appSecret * @return - * @throws VIVOPushException + * @throws Exception */ - public static String getAuthToken(String appId, String appKey, String appSecret) throws VIVOPushException { - JSONObject params = new JSONObject(); - long timestamp = System.currentTimeMillis(); - params.put("appId", appId); - params.put("appKey", appKey); - params.put("timestamp", timestamp); - params.put("sign", StringUtil.Md5(appId + appKey + timestamp + appSecret)); - String result = post("https://host:port/message/auth", params.toString(), null); - JSONObject resultJson = JSONObject.fromObject(result); - if (resultJson.optInt("result") != 0) - throw new VIVOPushException(resultJson.optInt("result"), resultJson.optString("desc")); - return resultJson.optString("authToken"); + private static String getAuthToken(int appId, String appKey, String appSecret) throws Exception { + Sender sender = new Sender(appSecret);// 瀹炰緥鍖朣ender + sender.initPool(20, 10);// 璁剧疆杩炴帴姹犲弬鏁帮紝鍙�夐」 + Result result = sender.getToken(appId, appKey);// 鍙戦�侀壌鏉冭姹� + if (result.getResult() == 0) { + return result.getAuthToken(); + } + throw new Exception("token鑾峰彇澶辫触"); } - - + private static String getAuthToken() { + if (StringUtil.isNullOrEmpty(authToken) || (System.currentTimeMillis() - authTokenTime) > 5000 * 60 * 60L) { + try { + String token = getAuthToken(APP_ID, APP_KEY, APP_SECRET); + authToken = token; + authTokenTime = System.currentTimeMillis(); + } catch (Exception e) { + e.printStackTrace(); + } + } + return authToken; + } + + private static Message.Builder getBaseMessageBuidler(String title, String content, + Map<String, String> customParams) { + Message.Builder buidler = new Message.Builder(); + buidler = buidler.title(title).content(content).skipType(4).skipContent("").notifyType(3); + buidler = buidler.clientCustomMap(customParams); + return buidler; + } + + private static Message.Builder getMessageBuidler(PushTypeEnum type, String title, String content, String activity, + String url, String webUrl, Long id) { + Map<String, String> data = new HashMap<>(); + if (type != null) + data.put("type", type.name()); + if (activity != null) + data.put("activity", activity); + if (url != null) + data.put("url", url); + if (webUrl != null) + data.put("webUrl", webUrl); + if (id != null) + data.put("id", id + ""); + return getBaseMessageBuidler(title, content, data); + } + + // 鎺ㄩ�佹墍鏈夎澶� + private static void pushAll(Message.Builder builder) { + String authToken = getAuthToken(); + if (StringUtil.isNullOrEmpty(authToken)) + return; + try { + Sender sender = new Sender(APP_SECRET); + sender.initPool(20, 10);// 璁剧疆杩炴帴姹犲弬鏁帮紝鍙�夐」 + sender.setAuthToken(authToken);// 璁剧疆鎺ㄩ�佺殑蹇呰鍙傛暟authToken(璋冪敤閴存潈鏂规硶鑾峰緱) + Message allSendMessage = builder.build();// 鏋勫缓瑕佸叏閲忔帹閫佺殑娑堟伅浣� + Result result = sender.sendToAll(allSendMessage); + if (result.getResult() == 0)// 鎴愬姛 + { + + } + } catch (Exception e) { + + } + } + + // 鎺ㄩ�侀儴鍒嗚澶� + private static boolean push(List<String> regIdList, Message.Builder builder) { + String authToken = getAuthToken(); + if (StringUtil.isNullOrEmpty(authToken)) + return false; + try { + Sender sender = new Sender(APP_SECRET); + sender.initPool(20, 10);// 璁剧疆杩炴帴姹犲弬鏁帮紝鍙�夐」 + sender.setAuthToken(authToken);// 璁剧疆鎺ㄩ�佺殑蹇呰鍙傛暟authToken(璋冪敤閴存潈鏂规硶鑾峰緱) + Message saveList = builder.build();// 鏋勫缓瑕佷繚瀛樼殑鎵归噺鎺ㄩ�佹秷鎭綋 + Result result = sender.saveListPayLoad(saveList);// 鍙戦�佷繚瀛樼兢鎺ㄦ秷鎭姹� + String taskId = result.getTaskId(); + + Set<String> regIds = new HashSet<>();// 鏋勫缓鎵归噺鎺ㄩ�佺敤鎴风兢 + for (String regId : regIdList) + regIds.add(regId); + TargetMessage targetMessage = new TargetMessage.Builder().regIds(regIds).taskId(taskId).build();// 鏋勫缓鎵归噺鎺ㄩ�佺殑娑堟伅浣� + result = sender.sendToList(targetMessage);// 鎵归噺鎺ㄩ�佺粰鐢ㄦ埛 + if (result.getResult() == 0)// 鎴愬姛 + { + return true; + } + } catch (Exception e) { + } + return false; + } + + /** + * 鎺ㄩ�佸晢鍝� + * + * @param registerIds + * -鏈�澶у��1000 + * @param title + * @param content + * @param goodsType + * @param goodsId + * @throws PushException + */ + public static void pushGoods(List<String> registerIds, String title, String content, int goodsType, Long goodsId) + throws PushException { + if (registerIds != null && registerIds.size() > 500) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�500涓�"); + + String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + + ".ui.recommend.GoodsBrowserActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.goodsdetail, title, content, activity, null, null, + goodsId); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 缃戦〉鎺ㄩ�� + * + * @param registerIds + * 鏈�澶у��1000 + * @param title + * @param content + * @param url + * -缃戦〉閾炬帴 + * @throws PushException + */ + public static void pushUrl(List<String> registerIds, String title, String content, String url) + throws PushException { + if (registerIds != null && registerIds.size() > 500) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�500涓�"); + String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.invite.ShareBrowserActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.url, title, content, activity, null, url, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * 绔欏唴淇℃帹閫� + * + * @param registerIds + * 鏈�澶у��1000 + * @param title + * @param content + * @param url + * -缃戦〉閾炬帴 + * @throws PushException + */ + public static void pushZNX(List<String> registerIds, String title, String content) throws PushException { + if (registerIds != null && registerIds.size() > 500) + throw new PushException(1, "璁惧鏁颁笉鑳借秴杩�500涓�"); + Message.Builder builder = getMessageBuidler(PushTypeEnum.ZNX, title, content, null, null, null, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushWEEX(List<String> registerIds, String title, String content, String weexUrl) + throws PushException { + String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + + ".ui.mine.weex.WeexApplicationActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.weex, title, content, activity, weexUrl, weexUrl, + null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushBaiChuanUrl(List<String> registerIds, String title, String content, String url) + throws PushException { + Message.Builder builder = getMessageBuidler(PushTypeEnum.baichuan, title, content, null, url, null, null); + + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushWelfareCenter(List<String> registerIds, String title, String content) throws PushException { + String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.mine.WelfareCenterActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.welfare, title, content, activity, null, null, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void pushUserSignInNotification(List<String> registerIds, String title, String content) + throws PushException { + String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.goldtask.GoldTaskActivity"; + Message.Builder builder = getMessageBuidler(PushTypeEnum.signin, title, content, activity, null, null, null); + try { + boolean success = push(registerIds, builder); + if (!success) + throw new PushException(2, "鎺ㄩ�佸け璐�"); + } catch (Exception e) { + e.printStackTrace(); + } + } } -- Gitblit v1.8.0