package com.yeshi.fanli.util.push;
|
|
import java.util.HashMap;
|
import java.util.Iterator;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.Properties;
|
import java.util.UUID;
|
|
import javax.annotation.PostConstruct;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.yeshi.utils.annotation.MapUtil;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.oppo.push.server.Notification;
|
import com.oppo.push.server.Result;
|
import com.oppo.push.server.Sender;
|
import com.oppo.push.server.Target;
|
import com.yeshi.fanli.entity.config.push.HWPushConfig;
|
import com.yeshi.fanli.exception.push.PushException;
|
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.taobao.TaoBaoUtil;
|
|
@Component
|
public class OPPOPushUtil {
|
|
private static OPPOPushUtil hWPushUtil;
|
|
@Autowired
|
private BusinessSystemService businessSystemService;
|
|
@PostConstruct
|
public void init() {
|
hWPushUtil = this;
|
hWPushUtil.businessSystemService = this.businessSystemService;
|
}
|
|
private static String appkey = "";
|
private static String masterSecret = "";
|
|
static {
|
Properties ps = org.yeshi.utils.PropertiesUtil
|
.getProperties(TaoBaoUtil.class.getClassLoader().getResourceAsStream("push_hw.properties"));
|
HWPushConfig config = (HWPushConfig) MapUtil.parseMap(HWPushConfig.class, ps);
|
appkey = config.getAppId();
|
masterSecret = config.getAppSecret();
|
}
|
|
private static Map<String, String> getParams(String type, String activity, String url, String webUrl, Long id) {
|
Map<String, String> data = new HashMap<>();
|
if (type != null)
|
data.put("type", type);
|
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 data;
|
}
|
|
/**
|
* 推送商品
|
*
|
* @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() > 100)
|
throw new PushException(1, "设备数不能超过100个");
|
|
String activity = Constant.systemCommonConfig.getAndroidBaseactivityName()
|
+ ".ui.recommend.GoodsBrowserActivity";
|
Map<String, String> params = getParams("goodsdetail", activity, null, null, goodsId);
|
|
try {
|
boolean success = push(registerIds, title, content, params);
|
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() > 100)
|
throw new PushException(1, "设备数不能超过1000个");
|
|
String activity = Constant.systemCommonConfig.getAndroidBaseactivityName() + ".ui.invite.ShareBrowserActivity";
|
Map<String, String> params = getParams("url", activity, null, url, null);
|
try {
|
boolean success = push(registerIds, title, content, params);
|
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() > 100)
|
throw new PushException(1, "设备数不能超过100个");
|
Map<String, String> params = getParams("ZNX", null, null, null, null);
|
if (params == null)
|
return;
|
|
try {
|
boolean success = push(registerIds, title, content, params);
|
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";
|
Map<String, String> params = getParams("weex", activity, weexUrl, weexUrl, null);
|
if (params == null)
|
return;
|
|
try {
|
boolean success = push(registerIds, title, content, params);
|
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 {
|
Map<String, String> params = getParams("baichuan", null, url, null, null);
|
if (params == null)
|
return;
|
|
try {
|
boolean success = push(registerIds, title, content, params);
|
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";
|
Map<String, String> params = getParams("welfare", activity, null, null, null);
|
if (params == null)
|
return;
|
|
try {
|
boolean success = push(registerIds, title, content, params);
|
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";
|
Map<String, String> params = getParams("signin", activity, null, null, null);
|
if (params == null)
|
return;
|
|
try {
|
boolean success = push(registerIds, title, content, params);
|
if (!success)
|
throw new PushException(2, "推送失败");
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
|
/**
|
*
|
* @param list
|
* 最大为1000
|
*/
|
public static boolean push(List<String> list, String title, String content, Map<String, String> dataParams) {
|
Map<String, String> params = null;
|
if (dataParams != null) {
|
params = new HashMap<>();
|
JSONObject json = new JSONObject();
|
for (Iterator<String> its = dataParams.keySet().iterator(); its.hasNext();) {
|
String key = its.next();
|
json.put(key, dataParams.get(key));
|
}
|
params.put("data", json.toJSONString());
|
}
|
|
try {
|
Sender sender = new Sender("f14S6P4sCrW4gs0WCO80w80cW", "E7976fe3d1758bf0c3480d8DcE4236e2");
|
Notification notifyCation = getNotification(title, content,
|
"com.yeshi.ec.rebate.myapplication.ui.OPPOPushMessageActivity", params);
|
if (list.size() == 1) {
|
Result result = sender.unicastNotification(notifyCation, Target.build(list.get(0)));
|
result.getStatusCode(); // 获取http请求状态码
|
result.getReturnCode(); // 获取平台返回码
|
System.out.println(result.getStatusCode() + "-" + result.getReturnCode());
|
if (result.getReturnCode().getCode() == 0)
|
return true;
|
else
|
return false;
|
} else {
|
Map<Target, Notification> batch = new HashMap<Target, Notification>(); // batch最大为1000
|
for (String registerId : list) {
|
batch.put(Target.build(registerId), notifyCation);
|
}
|
Result result = sender.unicastBatchNotification(batch); // 发送批量单推消息
|
result.getStatusCode(); // 获取http请求状态码
|
result.getReturnCode(); // 获取平台返回码
|
System.out.println(result.getStatusCode() + "-" + result.getReturnCode());
|
if (result.getReturnCode().getCode() == 0)
|
return true;
|
else
|
return false;
|
}
|
|
// List<Result.UnicastBatchResult> batchResult =
|
// result.getUnicastBatchResults(); // 批量单推结果
|
// for (Result.UnicastBatchResult record : batchResult) {
|
// record.getErrorCode();
|
// record.getErrorMessage();
|
// record.getMessageId();
|
// record.getTargetValue();
|
// }
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
return false;
|
}
|
|
private static Notification getNotification(String title, String content, String activity,
|
Map<String, String> params) {
|
|
Notification notification = new Notification();
|
|
/**
|
*
|
* 以下参数必填项
|
*
|
*/
|
|
notification.setTitle(title);
|
notification.setSubTitle(title);
|
notification.setContent(content);
|
|
/**
|
*
|
* 以下参数非必填项, 如果需要使用可以参考OPPO push服务端api文档进行设置
|
*
|
*/
|
|
// 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(activity);
|
|
// 网页地址【click_action_type为2必填,长度500】
|
// notification.setClickActionUrl("http://www.test.com");
|
|
// 动作参数,打开应用内页或网页时传递给应用或网页【JSON格式,非必填】,字符数不能超过4K,示例:{"key1":"value1","key2":"value2"}
|
|
JSONObject paramsData = new JSONObject();
|
if (params != null)
|
for (Iterator<String> its = params.keySet().iterator(); its.hasNext();) {
|
String key = its.next();
|
paramsData.put(key, params.get(key));
|
}
|
|
notification.setActionParameters(paramsData.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);
|
return notification;
|
}
|
|
}
|