package com.ks.push.utils.push;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.gson.Gson;
|
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 org.slf4j.Logger;
|
import org.slf4j.LoggerFactory;
|
import org.yeshi.utils.push.entity.PushAppInfo;
|
import org.yeshi.utils.push.entity.PushMessage;
|
import org.yeshi.utils.push.exception.MeiZuPushException;
|
import org.yeshi.utils.push.exception.VIVOPushException;
|
|
import java.io.IOException;
|
import java.io.UnsupportedEncodingException;
|
import java.net.URLEncoder;
|
import java.util.*;
|
|
/**
|
* vivo 推送
|
* 推送文档:https://dev.vivo.com.cn/documentCenter/doc/363
|
*/
|
public class VIVOPushUtil {
|
|
private static Map<String, AuthTokenInfo> tokenMap = new HashMap<>();
|
|
static Logger logger= LoggerFactory.getLogger(VIVOPushUtil.class);
|
|
private static int computeContentLength(String content) {
|
int count = 0;
|
for (int i = 0; i < content.length(); i++) {
|
//英文和数字算一个字符,其他算2个字符
|
if (content.charAt(i) >= 48 && content.charAt(i) <= 57) {//数字
|
count++;
|
} else if (content.charAt(i) >= 65 && content.charAt(i) <= 90) {//大写字母
|
count++;
|
} else if (content.charAt(i) >= 97 && content.charAt(i) <= 122) {//小写字母
|
count++;
|
} else {
|
count += 2;
|
}
|
}
|
return count;
|
}
|
|
|
private static Message.Builder createMessage(PushMessage message) {
|
String title = message.getTitle();
|
String content = message.getContent();
|
while (computeContentLength(title) > 40 - 3) {
|
title = title.substring(0, title.length() - 1);
|
}
|
if (computeContentLength(title) == 40 - 3) {
|
title += "...";
|
}
|
|
while (computeContentLength(content) > 100 - 3) {
|
content = content.substring(0, content.length() - 1);
|
}
|
|
if (computeContentLength(content) == 100 - 3) {
|
title += "...";
|
}
|
String uri = String.format("intent://%s?#Intent;scheme=%s;launchFlags=0x4000000;", message.getActivityHostPath(), message.getActivityScheme());
|
//增加Activity的跳转参数
|
for (Iterator<String> its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) {
|
String k = its.next();
|
try {
|
if (message.getActivityParams().get(k) != null) {
|
uri += String.format("S.%s=%s;", k, URLEncoder.encode(message.getActivityParams().get(k), "UTF-8"));
|
}
|
} catch (UnsupportedEncodingException e) {
|
e.printStackTrace();
|
}
|
}
|
|
uri += "end";
|
|
Message.Builder builder = new Message.Builder();
|
builder = builder
|
//必填项,设置通知标题(用于通知栏消息),最大20个汉字(一个汉字等于两个英文字符,即最大不超过40个英文字符)
|
.title(title)
|
//必填项,设置通知内容(用于通知栏消息) 最大50个汉字(一个汉字等于两个英文字符,即最大不超过100个英文字符)
|
.content(content)
|
//,设置点击跳转类型,value类型支持以下值 1:打开APP首页 2:打开链接 3:自定义 4:打开app内指定页面
|
.skipType(4)
|
//跳转类型为2时,跳转内容最大1000个字符,
|
//跳转类型为3或4时,跳转内容最大1024个字符
|
.skipContent(uri)
|
// 1:无 2:响铃 3:振动 4:响铃和振动
|
.notifyType(4)
|
//推送模式 0:正式推送;1:测试推送,不填默认为0
|
.pushMode(message.isTest() ? 1 : 0)
|
//消息类型 0:运营类消息,1:系统类消息
|
.classification(1)
|
.requestId(UUID.randomUUID().toString());
|
if (message.getActivityParams() != null) {
|
builder = builder.clientCustomMap(message.getActivityParams());
|
}
|
return builder.timeToLive(3600);
|
}
|
|
private static Sender getSender(PushAppInfo appInfo) throws Exception {
|
Sender sender = new Sender(appInfo.getAppSecret());//注册登录开发平台网站获取到的appSecret
|
sender.initPool(20, 10);//设置连接池参数,可选项
|
String appId = appInfo.getAppId();
|
String token = null;
|
if (tokenMap.get(appId) == null || tokenMap.get(appId).expireTime < System.currentTimeMillis()) {
|
Result result = sender.getToken(Integer.parseInt(appInfo.getAppId()), appInfo.getAppKey());//注册登录开发平台网站获取到的appId和appKey
|
token = result.getAuthToken();
|
if (token != null) {
|
AuthTokenInfo tokenInfo = new AuthTokenInfo();
|
tokenInfo.expireTime = System.currentTimeMillis() + 1000 * 60 * 20L;
|
tokenInfo.token = token;
|
tokenMap.put(appId, tokenInfo);
|
}
|
}
|
sender.setAuthToken(tokenMap.get(appId).token);
|
|
return sender;
|
}
|
|
/**
|
* 推送通知
|
*
|
* @param appInfo
|
* @param message
|
* @param regIds 不能超过1000个
|
* @return
|
* @throws IOException
|
* @throws MeiZuPushException
|
*/
|
public static String pushNotificationByRegIds(PushAppInfo appInfo, PushMessage message, List<String> regIds) throws Exception, VIVOPushException {
|
Sender sender = getSender(appInfo);
|
Message.Builder builder = createMessage(message);
|
Message saveList = builder.build();// 构建要保存的批量推送消息体
|
Result result = null;
|
if (regIds.size() > 1) {
|
result = sender.saveListPayLoad(saveList);// 发送保存群推消息请求
|
String taskId = result.getTaskId();
|
|
TargetMessage targetMessage = new TargetMessage.Builder()
|
.requestId(UUID.randomUUID().toString())
|
.regIds(new HashSet<>(regIds)).taskId(taskId).build();// 构建批量推送的消息体
|
result = sender.sendToList(targetMessage);// 批量推送给用户
|
} else {
|
result = sender.sendSingle(builder.regId(regIds.get(0)).build());
|
}
|
|
logger.info("VIVO推送响应结果:{}", new Gson().toJson(result));
|
|
if (result.getResult() == 0)// 成功
|
{
|
return result.getTaskId();
|
} else {
|
throw new VIVOPushException(result.getResult() + "", result.getDesc());
|
}
|
}
|
|
/**
|
* 推送所有(暂未对外开放)
|
* // *
|
* // * @param appInfo
|
* // * @param message
|
* // * @return
|
* // * @throws IOException
|
* // * @throws MeiZuPushException
|
* //
|
*/
|
// public static String pushNotificationAll(PushAppInfo appInfo, PushMessage message) throws Exception, OppoPushException {
|
// Sender sender = getSender(appInfo);
|
// Message.Builder builder = createMessage(message);
|
// Result result = sender.sendToAll(builder.build());
|
// if (result.getResult() == 0)// 成功
|
// {
|
// return result.getTaskId();
|
// } else {
|
// throw new VIVOPushException(result.getResult() + "", result.getDesc());
|
// }
|
// }
|
public static void main(String[] args) {
|
PushAppInfo appInfo = new PushAppInfo("105477463", "8a622972d8857cd29df47a0754be73be", "14bf9175-bc72-4d9d-bbcd-ba4ca78dc8fe");
|
Map<String, String> activityParams = new HashMap<>();
|
activityParams.put("activity", "com.tejia.lijin.app.ui.invite.ShareBrowserActivity");
|
activityParams.put("type", "web");
|
JSONObject jumpParams = new JSONObject();
|
jumpParams.put("url", "http://www.baidu.com");
|
activityParams.put("params", jumpParams.toJSONString());
|
|
PushMessage message = new PushMessage("祝大家春节快乐", "下午好,hello world", "", "tejiapush", "com.huawei.codelabpush/deeplink", activityParams);
|
|
String[] ids = new String[]{
|
"16239830400127746325913"
|
};
|
|
try {
|
VIVOPushUtil.pushNotificationByRegIds(appInfo, message, Arrays.asList(ids));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
private static class AuthTokenInfo {
|
public String token;
|
public long expireTime;
|
}
|
|
|
}
|