| | |
| | | package com.yeshi.fanli.util.push;
|
| | |
|
| | | import java.util.HashMap;
|
| | | import java.util.HashSet;
|
| | | import java.util.List;
|
| | | import java.util.Map;
|
| | | import java.util.Set;
|
| | | import java.util.UUID;
|
| | |
|
| | | 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;
|
| | |
|
| | | /**
|
| | | * VIVO的推送帮助
|
| | | * |
| | | * @author Administrator
|
| | | *
|
| | | */
|
| | | public class VIVOPushUtil {
|
| | |
|
| | | 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;
|
| | |
|
| | | /**
|
| | | * 获取授权码
|
| | | * |
| | | * @param appId
|
| | | * @param appKey
|
| | | * @param appSecret
|
| | | * @return
|
| | | * @throws Exception
|
| | | */
|
| | | 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("todo").notifyType(3)
|
| | | .requestId(UUID.randomUUID().toString());
|
| | | 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.timeToLive(3600).build();// 构建要保存的批量推送消息体
|
| | | Result result = null;
|
| | | if (regIdList.size() > 1) {
|
| | | 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().requestId(UUID.randomUUID().toString())
|
| | | .regIds(regIds).taskId(taskId).build();// 构建批量推送的消息体
|
| | | result = sender.sendToList(targetMessage);// 批量推送给用户
|
| | | } else
|
| | | {
|
| | | result = sender.sendSingle(builder.regId(regIdList.get(0)).timeToLive(3600).build());
|
| | | }
|
| | | if (result.getResult() == 0)// 成功
|
| | | {
|
| | | return true;
|
| | | }
|
| | | } catch (Exception e) {
|
| | | e.printStackTrace();
|
| | | }
|
| | | 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();
|
| | | }
|
| | | }
|
| | |
|
| | | }
|
| | | package com.yeshi.fanli.util.push; |
| | | |
| | | import java.util.HashMap; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Map; |
| | | import java.util.Set; |
| | | import java.util.UUID; |
| | | |
| | | 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; |
| | | |
| | | /** |
| | | * VIVO的推送帮助 |
| | | * |
| | | * @author Administrator |
| | | * |
| | | */ |
| | | public class VIVOPushUtil { |
| | | |
| | | 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; |
| | | |
| | | /** |
| | | * 获取授权码 |
| | | * |
| | | * @param appId |
| | | * @param appKey |
| | | * @param appSecret |
| | | * @return |
| | | * @throws Exception |
| | | */ |
| | | 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("todo").notifyType(3) |
| | | .requestId(UUID.randomUUID().toString()); |
| | | buidler = buidler.clientCustomMap(customParams); |
| | | return buidler; |
| | | } |
| | | |
| | | private static Message.Builder getMessageBuidler(PushTypeEnum type, String title, String content, String activity, |
| | | String url, String webUrl, String 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.timeToLive(3600).build();// 构建要保存的批量推送消息体 |
| | | Result result = null; |
| | | if (regIdList.size() > 1) { |
| | | 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().requestId(UUID.randomUUID().toString()) |
| | | .regIds(regIds).taskId(taskId).build();// 构建批量推送的消息体 |
| | | result = sender.sendToList(targetMessage);// 批量推送给用户 |
| | | } else |
| | | { |
| | | result = sender.sendSingle(builder.regId(regIdList.get(0)).timeToLive(3600).build()); |
| | | } |
| | | if (result.getResult() == 0)// 成功 |
| | | { |
| | | return true; |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | 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, String goodsId,String androidBaseActivity) |
| | | throws PushException { |
| | | if (registerIds != null && registerIds.size() > 500) |
| | | throw new PushException(1, "设备数不能超过500个"); |
| | | |
| | | String activity = androidBaseActivity |
| | | + ".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,String androidBaseActivity) |
| | | throws PushException { |
| | | if (registerIds != null && registerIds.size() > 500) |
| | | throw new PushException(1, "设备数不能超过500个"); |
| | | String activity = androidBaseActivity + ".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,String androidBaseActivity) |
| | | throws PushException { |
| | | String activity = androidBaseActivity |
| | | + ".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,String androidBaseActivity) throws PushException { |
| | | String activity = androidBaseActivity + ".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,String androidBaseActivity) |
| | | throws PushException { |
| | | String activity = androidBaseActivity + ".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(); |
| | | } |
| | | } |
| | | |
| | | } |