1.获取SDK,并写入常量表
2.引入MiPush_SDK_Sever.jar文件和json-simple-1.1.1.jar
3.开发服务端代码(这里是用别名进行推送)
/*===================================小米推送=================================*/
/**
* 指定alias推送(单个或多个)
*
* @param messagePayload 消息
* @param title 消息标题
* @param description 消息描述
* @param adsType 消息类型
* @param adsLinks 消息链接
* @param aliasList 指定alias
*/
public static boolean xiaomiBatchPush(String messagePayload, String title, String description, String adsType,
String adsLinks, List<String> aliasList, Map<String,String> parm) {
log.info("小米设备:{}",aliasList);
try {
JSONObject result = builderMessageAndSender(messagePayload, title, description, adsType, adsLinks,parm);
Message message = (Message) result.get("message");
Sender sender = (Sender) result.get("sender");
Result pushResult = sender.sendToAlias(message, aliasList, 3);
log.debug("++++推送到小米结果为:{}",pushResult);
if (pushResult != null) {
return true;
}
} catch (Exception e) {
e.printStackTrace();
log.error("小米指定alias失败:", e);
}
return false;
}
public static JSONObject builderMessageAndSender(String messagePayload, String title, String description, String adsType, String adsLinks,Map<String,String> parm) {
//boolean isProd = myProperties.getPush().isProd();
boolean isProd = ConstantsUnit.xiaomiProd;
// 设置环境 正式环境下使用Push服务,启动时需要调用如下代码
Constants.useOfficial();
Map<String, String> map = new HashMap<>();
map.put("ads_type", adsType);
map.put("ads_links", adsLinks);
// 构建消息
Message message = new Message.Builder()
.title(title)
.description(description)
.payload(messagePayload)
.extra(Constants.EXTRA_PARAM_NOTIFY_FOREGROUND,Constants.NOTIFY_LAUNCHER_ACTIVITY)
//.extra(parm)
.restrictedPackageName(ConstantsUnit.xiaomiPackageName)
// 使用默认提示音提示
.notifyType(1)
.build();
// 构建发送
Sender sender = new Sender(ConstantsUnit.xiaomiAppSecretKey);
JSONObject result = new JSONObject();
result.put("message", message);
result.put("sender", sender);
return result;
}