package com.huawei.push.util.push;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.google.gson.Gson;
|
import com.ks.push.utils.Constant;
|
import com.oppo.push.server.*;
|
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 org.yeshi.utils.push.exception.MeiZuPushException;
|
import org.yeshi.utils.push.exception.OppoPushException;
|
|
import java.io.IOException;
|
import java.util.*;
|
|
/**
|
* OPPO推送
|
* 推送文档:https://open.oppomobile.com/wiki/doc#id=10203
|
*/
|
public class OppoPushUtil {
|
|
static Logger logger = LoggerFactory.getLogger(OppoPushUtil.class);
|
|
private static Notification getNotification(PushMessage message) {
|
Notification notification = new Notification();
|
//推送回执
|
notification.setCallBackUrl(Constant.PUSH_CALLBACK_OPPO_URL);
|
String title = message.getTitle();
|
String content = message.getContent();
|
if (title.length() > 50) {
|
title = title.substring(0, 50 - 3) + "...";
|
}
|
if (content.length() > 50) {
|
content = content.substring(0, 50 - 3) + "...";
|
}
|
/**
|
* 以下参数必填项
|
*/
|
//字数限制1~32
|
notification.setTitle(title);
|
//字数限制200以内,中英文均以一个计算
|
notification.setContent(content);
|
/**
|
* 以下参数非必填项, 如果需要使用可以参考OPPO push服务端api文档进行设置
|
*/
|
//通知栏样式 1. 标准样式 2. 长文本样式 3. 大图样式 【非必填,默认1-标准样式】
|
notification.setStyle(1);
|
// App开发者自定义消息Id,OPPO推送平台根据此ID做去重处理,对于广播推送相同appMessageId只会保存一次,对于单推相同appMessageId只会推送一次
|
notification.setAppMessageId(UUID.randomUUID().toString());
|
// 应用接收消息到达回执的回调URL,字数限制200以内,中英文均以一个计算
|
// notification.setCallBackUrl("http://www.test.com");
|
// App开发者自定义回执参数,字数限制50以内,中英文均以一个计算
|
// notification.setCallBackParameter("");
|
// 点击动作类型0,启动应用;1,打开应用内页(activity的intent action);2,打开网页;4,打开应用内页(activity);【非必填,默认值为0】;5,Intent scheme URL
|
notification.setClickActionType(4);
|
// 应用内页地址【click_action_type为1或4时必填,长度500】
|
notification.setClickActionActivity(message.getActivity());
|
// 网页地址【click_action_type为2必填,长度500】
|
// notification.setClickActionUrl("http://www.test.com");
|
|
if (message.getActivityParams() != null && message.getActivityParams().size() > 0) {
|
JSONObject params = new JSONObject();
|
for (Iterator<String> its = message.getActivityParams().keySet().iterator(); its.hasNext(); ) {
|
String key = its.next();
|
params.put(key, message.getActivityParams().get(key));
|
}
|
// 动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
|
notification.setActionParameters(params.toJSONString());
|
}
|
|
|
// 展示类型 (0, “即时”),(1, “定时”)
|
notification.setShowTimeType(0);
|
// 定时展示开始时间(根据time_zone转换成当地时间),时间的毫秒数
|
// notification.setShowStartTime(System.currentTimeMillis() + 1000 * 60 * 3);
|
// 定时展示结束时间(根据time_zone转换成当地时间),时间的毫秒数
|
// notification.setShowEndTime(System.currentTimeMillis() + 1000 * 60 * 5);
|
// 是否进离线消息,【非必填,默认为True】
|
notification.setOffLine(true);
|
// 离线消息的存活时间(time_to_live) (单位:秒), 【off_line值为true时,必填,最长3天】
|
notification.setOffLineTtl(24 * 3600);
|
// 时区,默认值:(GMT+08:00)北京,香港,新加坡
|
// notification.setTimeZone("GMT+08:00");
|
// 0:不限联网方式, 1:仅wifi推送
|
notification.setNetworkType(0);
|
//若推送消息时不带通道推送, Android 8及以上的手机将收不到消息
|
notification.setChannelId("channel_android");
|
return notification;
|
}
|
|
/**
|
* 推送通知
|
*
|
* @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, OppoPushException {
|
//推送标题限制字数1-32,推送内容限制字数1-100
|
Sender sender = new Sender(appInfo.getAppKey(), appInfo.getAppSecret());
|
Result result = null;
|
if (regIds.size() > 1) {
|
Map batch = new HashMap(); // batch最大为1000
|
for (String regId : regIds) {
|
batch.put(Target.build(regId), getNotification(message));
|
}
|
result = sender.unicastBatchNotification(batch); //发送批量单推消息
|
} else {
|
Target target = Target.build(regIds.get(0)); //创建发送对象
|
result = sender.unicastNotification(getNotification(message), target); //发送单推消息
|
}
|
logger.info("OPPO推送响应结果:{}", new Gson().toJson(result));
|
if (result.getStatusCode() == 200) {
|
if (result.getReturnCode().getCode() == 0) {
|
return result.getMessageId();
|
} else {
|
throw new OppoPushException(result.getReturnCode().getCode() + "", result.getReturnCode().getMessage());
|
}
|
} else {
|
throw new OppoPushException("网络请求出错");
|
}
|
}
|
|
/**
|
* 推送所有
|
*
|
* @param appInfo
|
* @param message
|
* @return
|
* @throws IOException
|
* @throws MeiZuPushException
|
*/
|
public static String pushNotificationAll(PushAppInfo appInfo, PushMessage message, List<String> regIds) throws Exception, OppoPushException {
|
Sender sender = new Sender(appInfo.getAppKey(), appInfo.getAppSecret());
|
Notification broadNotification = getNotification(message);// 创建通知栏消息体
|
Result saveResult = sender.saveNotification(broadNotification); // 发送保存消息体请求
|
saveResult.getStatusCode(); // 获取http请求状态码
|
saveResult.getReturnCode(); // 获取平台返回码
|
String messageId = saveResult.getMessageId(); //获取messageId
|
Target target = new Target(); // 创建广播目标
|
target.setTargetValue(StringUtil.concat(regIds, ";"));
|
Result broadResult = sender.broadcastNotification(messageId, target); // 发送广播消息
|
broadResult.getTaskId(); // 获取广播taskId
|
List<Result.BroadcastErrorResult> errorList = broadResult.getBroadcastErrorResults();
|
if (errorList.size() > 0) { // 如果大小为0,代表所有目标发送成功
|
for (Result.BroadcastErrorResult error : errorList) {
|
error.getErrorCode(); // 错误码
|
error.getTargetValue(); // 目标
|
}
|
return broadResult.getTaskId();
|
} else {
|
return broadResult.getTaskId();
|
}
|
}
|
|
|
public static void main(String[] args) {
|
PushAppInfo appInfo = new PushAppInfo("30514447", "78b911aa08e1468baf24ddea6c7ca23e", "fda37b51cc7f4d4d8b3d326455ec58c0");
|
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("你好啊", "下午好,hello world", "com.weikou.beibeivideo.ui.push.OpenClickActivity", null, null, activityParams);
|
|
String[] ids = new String[]{
|
"OPPO_CN_fbaf9e7431c18d4ac4a08089d124c6ca"
|
};
|
|
try {
|
OppoPushUtil.pushNotificationAll(appInfo, message, Arrays.asList(ids));
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
|
}
|
|
|
}
|