admin
2019-09-27 3a84f708ddb9087993808bb491a2dc3453eaf08a
VIVO推送集成
2个文件已修改
1个文件已添加
305 ■■■■ 已修改文件
fanli/libs/vpush-server-sdk-2.0.jar 补丁 | 查看 | 原始文档 | blame | 历史
fanli/pom.xml 8 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/src/main/java/com/yeshi/fanli/util/push/VIVOPushUtil.java 297 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
fanli/libs/vpush-server-sdk-2.0.jar
Binary files differ
fanli/pom.xml
@@ -127,6 +127,14 @@
            <systemPath>${basedir}/libs/opush-server-sdk-1.0.3.jar</systemPath>
        </dependency>
        <dependency>
            <groupId>vpush-server-sdk-2.0</groupId>
            <artifactId>vpush-server-sdk-2.0</artifactId>
            <version>2.0</version>
            <scope>system</scope>
            <systemPath>${basedir}/libs/vpush-server-sdk-2.0.jar</systemPath>
        </dependency>
    </dependencies>
    <build>
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);// 实例化Sender
        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();
        }
    }
}