package org.yeshi.utils.push;
|
|
import com.alibaba.fastjson.JSONObject;
|
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.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.util.*;
|
|
/**
|
* OPPO推送
|
* 推送文档:https://open.oppomobile.com/wiki/doc#id=10203
|
*/
|
public class VIVOPushUtil {
|
|
private static Map<String, AuthTokenInfo> tokenMap = new HashMap<>();
|
|
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 += "...";
|
}
|
|
|
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(message.getActivity())
|
// 1:无 2:响铃 3:振动 4:响铃和振动
|
.notifyType(4)
|
.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());
|
}
|
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("100061268", "e9add1dfed7086061119d407857628ef", "8e27162d-8d30-4e54-8da4-db4809c0bc7e");
|
Map<String, String> activityParams = new HashMap<>();
|
activityParams.put("activity", "test");
|
JSONObject jumpParams = new JSONObject();
|
jumpParams.put("id", 123123);
|
activityParams.put("params", jumpParams.toJSONString());
|
|
PushMessage message = new PushMessage("你好啊123458922235,", "下午好,hello world", "com.weikou.beibeivideo.ui.push.OpenClickActivity", activityParams);
|
|
String[] ids = new String[]{
|
"16139879500386126892292"
|
};
|
|
try {
|
VIVOPushUtil.pushNotificationByRegIds(appInfo, message, Arrays.asList(ids));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
private static class AuthTokenInfo {
|
public String token;
|
public long expireTime;
|
}
|
|
|
}
|