package com.yeshi.fanli.util.push;
|
|
import java.io.IOException;
|
import java.util.List;
|
import java.util.Map;
|
|
import javax.annotation.PostConstruct;
|
|
import org.json.simple.parser.ParseException;
|
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.stereotype.Component;
|
import org.yeshi.utils.JsonUtil;
|
|
import com.xiaomi.xmpush.server.Constants;
|
import com.xiaomi.xmpush.server.Message;
|
import com.xiaomi.xmpush.server.Message.Builder;
|
import com.xiaomi.xmpush.server.Result;
|
import com.xiaomi.xmpush.server.Sender;
|
import com.yeshi.fanli.entity.xinge.MessageInfo;
|
import com.yeshi.fanli.entity.xinge.PushRecord;
|
import com.yeshi.fanli.log.LogHelper;
|
import com.yeshi.fanli.log.PushLogHelper;
|
import com.yeshi.fanli.service.inter.config.BusinessSystemService;
|
import com.yeshi.fanli.util.Constant;
|
import com.yeshi.fanli.util.PropertiesUtil;
|
|
import net.sf.json.JSONObject;
|
|
@Component
|
public class XiaoMiPushUtil {
|
|
private static XiaoMiPushUtil xiaoMiPushUtil;
|
|
@Autowired
|
private BusinessSystemService businessSystemService;
|
|
@PostConstruct
|
public void init() {
|
xiaoMiPushUtil = this;
|
xiaoMiPushUtil.businessSystemService = this.businessSystemService;
|
}
|
|
// AppKey
|
private static String XIAOMI_F_ANDROID_APP_KEY = PropertiesUtil.getMap().get("xiaomi_f_android_app_key");
|
|
// AppSecert
|
private static String XIAOMI_F_ANDROID_APP_SECERT = PropertiesUtil.getMap().get("xiaomi_f_android_app_secert");
|
|
/**
|
*
|
* 方法说明: 小米推送安卓(全推)
|
*
|
* @author mawurui createTime 2018年3月7日 上午9:57:56
|
* @param info
|
* @param params
|
* @param system
|
* @return
|
*/
|
public static String allPushAndroidForXM(MessageInfo info, Map<String, Object> map) {
|
if (Constant.IS_TEST)
|
return null;
|
|
String name = info.getPackageName();
|
if (!Constant.systemCommonConfig.getAndroidPackageName().equalsIgnoreCase(name)
|
&& !Constant.systemCommonConfig.getIosBundleId().equalsIgnoreCase(name)) {
|
return null;
|
}
|
|
// 从info中取页面传来的值
|
Constants.useOfficial(); // 启动推送方法
|
String PackageName = info.getPackageName();
|
Sender sender = new Sender(XIAOMI_F_ANDROID_APP_SECERT); // 申请的AppSecert
|
String title = info.getTitle();
|
String payload = info.getContent();
|
String description = info.getContent();
|
|
Message message = null; // 做循环传来的key
|
Builder builder = new Message.Builder().restrictedPackageName(PackageName).title(title).payload(payload)
|
.description(description).passThrough(0) // 设置消息是否通过透传方式至App,
|
// 1表示透传,0表示通知栏消息(默认)
|
.notifyType(1); // 设置通知类型, type类型(1-默认提示音, 2-使用默认震动提示,
|
// 3-使用默认led灯光提示)
|
// 将extra放入MessageInfo对象中, 循环遍历extra中的key 和 value
|
if (map != null) {
|
for (String key : map.keySet()) {
|
builder.extra(key, map.get(key).toString());
|
}
|
}
|
message = builder.build();
|
try {
|
Result result = sender.broadcastAll(message, 3);
|
return result.getMessageId();// 成功 返回消息的Id, 失败返回null
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
* 批量推送
|
*
|
* @param info
|
* @param map
|
* @param regList
|
* 最大个数为1000
|
* @return
|
*/
|
public static String pushBatchAndroidForXM(MessageInfo info, Map<String, Object> map, List<String> regList) {
|
if (Constant.IS_TEST)
|
return null;
|
if (regList == null || regList.size() == 0)
|
return "";
|
|
String name = info.getPackageName();
|
if (!Constant.systemCommonConfig.getAndroidPackageName().equalsIgnoreCase(name)
|
&& !Constant.systemCommonConfig.getIosBundleId().equalsIgnoreCase(name)) {
|
return null;
|
}
|
|
// 从info中取页面传来的值
|
Constants.useOfficial(); // 启动推送方法
|
String PackageName = info.getPackageName();
|
Sender sender = new Sender(XIAOMI_F_ANDROID_APP_SECERT); // 申请的AppSecert
|
String title = info.getTitle();
|
String payload = info.getContent();
|
String description = info.getContent();
|
|
Message message = null; // 做循环传来的key
|
Builder builder = new Message.Builder().restrictedPackageName(PackageName).title(title).payload(payload)
|
.description(description).passThrough(0) // 设置消息是否通过透传方式至App,
|
// 1表示透传,0表示通知栏消息(默认)
|
.notifyType(1); // 设置通知类型, type类型(1-默认提示音, 2-使用默认震动提示,
|
// 3-使用默认led灯光提示)
|
// 将extra放入MessageInfo对象中, 循环遍历extra中的key 和 value
|
if (map != null) {
|
for (String key : map.keySet()) {
|
builder.extra(key, map.get(key).toString());
|
}
|
}
|
message = builder.build();
|
try {
|
Result result = sender.send(message, regList, 3);
|
return result.getMessageId();// 成功 返回消息的Id, 失败返回null
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
*
|
* 方法说明: 小米推送安卓(单推)
|
*
|
* @author mawurui createTime 2018年3月7日 下午5:09:18
|
* @param info
|
* @param params
|
* @param system
|
* @return
|
*/
|
public static String singlePushAndroidForXM(MessageInfo info, Map<String, Object> map, List<String> regIds,
|
System system) {
|
String name = info.getPackageName();
|
if (!Constant.systemCommonConfig.getAndroidPackageName().equalsIgnoreCase(name)
|
&& !Constant.systemCommonConfig.getIosBundleId().equalsIgnoreCase(name)) {
|
return null;
|
}
|
Constants.useOfficial(); // 启动推送方法
|
String PackageName = info.getPackageName();
|
Sender sender = new Sender(XIAOMI_F_ANDROID_APP_SECERT);
|
String title = info.getTitle();
|
String content = info.getContent();
|
String uId = info.getAlias();
|
|
Message message = null; // 做循环传来的key
|
Builder builder = new Message.Builder().restrictedPackageName(PackageName).title(title).description(content)
|
.passThrough(0) // 设置消息是否通过透传方式至App, 1表示透传,0表示通知栏消息(默认)
|
.notifyType(1); // 设置通知类型, type类型(1-默认提示音, 2-使用默认震动提示,
|
// 3-使用默认led灯光提示)
|
// 将extra放入MessageInfo对象中, 循环遍历extra中的key 和 value
|
if (map != null) {
|
for (String key : map.keySet()) {
|
builder.extra(key, map.get(key).toString());
|
}
|
}
|
message = builder.build();
|
try {
|
Result result = sender.send(message, regIds, 3); // sender.sendToAlias(message,
|
// uId, 3);
|
PushLogHelper.xmInfo("推送结果:" + result.getReason());
|
return result.getMessageId();
|
} catch (IOException e) {
|
e.printStackTrace();
|
} catch (ParseException e) {
|
e.printStackTrace();
|
}
|
return null;
|
}
|
|
/**
|
*
|
* 方法说明: 小米推送红包
|
*
|
* @author mawurui createTime 2018年3月8日 上午9:55:14
|
* @param info
|
*/
|
public static void pushByHongBao(List<String> regIds, System system) {
|
MessageInfo info = new MessageInfo();
|
info.setTitle("新到红包");
|
info.setDescription("红包到来了...");
|
JSONObject jsonObject = new JSONObject();
|
jsonObject.put("type", "hongbao");
|
XiaoMiPushUtil.singlePushAndroidForXM(info, JsonUtil.parseData(jsonObject), regIds, system);
|
}
|
|
/**
|
*
|
* 方法说明: 小米推送提现申请
|
*
|
* @author mawurui createTime 2018年3月8日 上午10:00:16
|
* @param info
|
*/
|
public static void pushByApply(List<String> regIds, System system) {
|
MessageInfo info = new MessageInfo();
|
info.setTitle("提现申请");
|
info.setDescription("提现审核中...");
|
JSONObject jsonObject = new JSONObject();
|
jsonObject.put("type", "cash");
|
XiaoMiPushUtil.singlePushAndroidForXM(info, JsonUtil.parseData(jsonObject), regIds, system);
|
}
|
|
/**
|
*
|
* 方法说明: 推送提现状态
|
*
|
* @author mawurui createTime 2018年3月8日 上午10:26:34
|
* @param alias
|
* @param state
|
* @param system
|
*/
|
public static void pushByExtract(List<String> regIds, int state, System system) {
|
MessageInfo info = new MessageInfo();
|
if (state == 0) {
|
info.setTitle("提现成功");
|
info.setDescription("您有一笔交易,已提现成功!");
|
} else if (state == 1) {
|
info.setTitle("提现失败");
|
info.setDescription("您有一笔交易,提现失败!");
|
} else {
|
info.setTitle("提现失败");
|
info.setDescription("提现被拒绝");
|
}
|
JSONObject jsonObject = new JSONObject();
|
jsonObject.put("type", "cash");
|
XiaoMiPushUtil.singlePushAndroidForXM(info, JsonUtil.parseData(jsonObject), regIds, system);
|
}
|
|
/**
|
*
|
* @param info
|
* @param params
|
* @param pushRecord
|
* @return 1:都成功 2:仅android 成功 3.仅IOS成功 4.都失败
|
*/
|
private static int pushApp(MessageInfo info, JSONObject json, PushRecord pushRecord, System b_android) {
|
// jsonXm 转换为 map
|
JSONObject mapXm = JSONObject.fromObject(json);
|
String android = allPushAndroidForXM(info, mapXm);
|
LogHelper.userInfo("小米推送:" + android);
|
pushRecord.setAndroidPushId(android);
|
if (android != null) {
|
return 1;
|
} else {
|
return 4;
|
}
|
}
|
|
public static void pushZNX(List<String> regIds, String title, String content, String msgId) {
|
// 小米开始推送
|
MessageInfo info = new MessageInfo();
|
info.setActivty(
|
String.format("%s.ui.BrowserActivity", Constant.systemCommonConfig.getAndroidBaseactivityName()));
|
info.setTitle(title);
|
info.setContent(content);
|
info.setPackageName(Constant.systemCommonConfig.getAndroidPackageName());
|
JSONObject json = new JSONObject();
|
json.put("type", "ZNX");
|
json.put("miPushUrl", String.format("%s.ui.mine.AppMailDetailActivity",
|
Constant.systemCommonConfig.getAndroidBaseactivityName()));
|
|
JSONObject contentJson = new JSONObject();
|
contentJson.put("id", msgId);
|
contentJson.put("title", title);
|
contentJson.put("content", content);
|
contentJson.put("isOpen", 0);
|
contentJson.put("createTime", java.lang.System.currentTimeMillis());
|
json.put("content", contentJson);
|
JSONObject mapXm = JSONObject.fromObject(json);
|
XiaoMiPushUtil.singlePushAndroidForXM(info, mapXm,regIds, null);
|
}
|
|
}
|