package com.ks.push.utils;
|
|
import com.google.gson.Gson;
|
import com.huawei.push.util.push.*;
|
import com.ks.push.pojo.DO.BPushMessage;
|
import com.ks.push.pojo.DO.PushPlatform;
|
import org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.yeshi.utils.StringUtil;
|
import org.yeshi.utils.push.entity.PushAppInfo;
|
import org.yeshi.utils.push.entity.PushMessage;
|
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
|
public class PushUtil {
|
|
static Logger logger = LoggerFactory.getLogger(PushUtil.class);
|
|
/**
|
* 推送通知
|
*
|
* @param platform
|
* @param appInfo
|
* @param pushMessage
|
* @param tokenList
|
* @throws Exception
|
*/
|
public static void pushNotifyCation(PushPlatform platform, PushAppInfo appInfo, BPushMessage pushMessage, String identifyCode, List<String> tokenList) throws Exception {
|
if (appInfo == null) {
|
throw new Exception("应用参数为空");
|
}
|
|
if (pushMessage == null) {
|
throw new Exception("消息为空");
|
}
|
|
if (tokenList == null || tokenList.size() == 0) {
|
throw new Exception("token为空");
|
}
|
|
logger.info("{}推送应用信息:{}", platform.name(), new Gson().toJson(appInfo));
|
logger.info("{}推送内容信息:{}", platform.name(), new Gson().toJson(pushMessage));
|
logger.info("{}推送token信息:{}", platform.name(), new Gson().toJson(tokenList));
|
|
|
Map<String, String> extras = null;
|
if (pushMessage.getExtras() != null) {
|
extras = new HashMap<>();
|
for (Iterator<String> its = pushMessage.getExtras().keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
String value = pushMessage.getExtras().get(key);
|
if (!StringUtil.isNullOrEmpty(key) && !StringUtil.isNullOrEmpty(value)) {
|
extras.put(key, value);
|
}
|
}
|
}
|
|
PushMessage message = new PushMessage(pushMessage.getTitle(), pushMessage.getContent(), pushMessage.getAndroidActivity(), pushMessage.getAndroidActivityScheme(), pushMessage.getAndroidHostPath(), extras);
|
|
if (platform == PushPlatform.xm) {
|
pushXM(appInfo, message, tokenList);
|
} else if (platform == PushPlatform.hw) {
|
pushHW(appInfo, message, identifyCode, tokenList);
|
} else if (platform == PushPlatform.oppo) {
|
pushOPPO(appInfo, message, tokenList);
|
} else if (platform == PushPlatform.vivo) {
|
pushVIVO(appInfo, message, tokenList);
|
} else if (platform == PushPlatform.mz) {
|
pushMZ(appInfo, message, tokenList);
|
}
|
}
|
|
/**
|
* 小米推送
|
*
|
* @param appInfo
|
* @param message
|
* @param tokenList
|
* @throws Exception
|
*/
|
private static void pushXM(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
|
XiaoMiPushUtil.pushNotificationByRegIds(appInfo, message, tokenList);
|
}
|
|
/**
|
* 华为推送
|
*
|
* @param appInfo
|
* @param message
|
* @param bigTag
|
* @param tokenList
|
* @throws Exception
|
*/
|
private static void pushHW(PushAppInfo appInfo, PushMessage message, String bigTag, List<String> tokenList) throws Exception {
|
HuaWeiPushUtil.pushNotificationByTokens(appInfo, message, bigTag, tokenList);
|
}
|
|
/**
|
* OPPO推送
|
*
|
* @param appInfo
|
* @param message
|
* @param tokenList
|
* @throws Exception
|
*/
|
private static void pushOPPO(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
|
OppoPushUtil.pushNotificationByRegIds(appInfo, message, tokenList);
|
}
|
|
/**
|
* VIVO推送
|
*
|
* @param appInfo
|
* @param message
|
* @param tokenList
|
* @throws Exception
|
*/
|
private static void pushVIVO(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
|
VIVOPushUtil.pushNotificationByRegIds(appInfo, message, tokenList);
|
}
|
|
/**
|
* 魅族推送
|
*
|
* @param appInfo
|
* @param message
|
* @param tokenList
|
* @throws Exception
|
*/
|
private static void pushMZ(PushAppInfo appInfo, PushMessage message, List<String> tokenList) throws Exception {
|
MeiZuPushUtil.pushNotificationByPushId(appInfo, message, tokenList);
|
}
|
|
|
}
|