小米:
添加依赖或者jar包
jar包下载地址:http://admin.xmpush.xiaomi.com/zh_CN/mipush/downpage/java-http2
工具类:
package com.xxx.admin.util;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaomi.xmpush.server.Constants;
import com.xiaomi.xmpush.server.Message;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;
import org.apache.log4j.Logger;
import java.util.Map;
public class XMiPushUtil {
private static Logger logger = Logger.getLogger(XMiPushUtil.class);
/**
* 小米生产环境常量
*/
public static boolean xiaomiProd=false;
public static String xiaomiPackageName="me.gaoshou1.ssh";
public static String xiaomiAppSecretKey="/IqKJWX3vatE+Fk3gRyP4A==";
/**
* 指定alias推送(单个或多个)
*
* @param messagePayload 消息
* @param title 消息标题
* @param description 消息描述
* @param aliasList 指定alias
*/
public static Result xiaomiBatchPush(String messagePayload, String title, String description, String aliasList,String targetPath, String action) {
logger.info("小米设备:{}"+aliasList.toString());
try {
JSONObject result = builderMessageAndSender(title, description,action);
Message message = (Message) result.get("message");
Sender sender = (Sender) result.get("sender");
Result pushResult = sender.send(message, aliasList, 3);
logger.info("++++推送到小米结果为:{}"+pushResult.toString());
if (pushResult != null && pushResult.getErrorCode().getValue()==0) {
return pushResult;
}
} catch (Exception e) {
e.printStackTrace();
logger.info("小米指定alias失败:", e);
}
return null;
}
public static JSONObject builderMessageAndSender(String title,String description,String action) {
boolean isProd = XMiPushUtil.xiaomiProd;
Constants.useOfficial();
// 构建消息 //Constants.EXTRA_PARAM_NOTIFY_EFFECT默认不设置,就可以正常点击事件
JSONObject actionJson= JSON.parseObject(action);
Map map = (Map<String, Object>)actionJson;
Message message = new Message.Builder()
.title(title)
.description(description)
.payload(action)
.extra(Constants.EXTRA_PARAM_NOTIFY_FOREGROUND, "1")//是否开启弹窗 1开启0关闭
//.extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_ACTIVITY) //打开aap内任意ui
.extra(Constants.EXTRA_PARAM_INTENT_URI, "intent:#Intent;component=me.gaoshou1.ssh.ui.activity.TaskEvaluateCompleteActivity;end")//ui 地址
//.extra(Constants.EXTRA_PARAM_NOTIFY_EFFECT, Constants.NOTIFY_LAUNCHER_ACTIVITY) //打开点击
.extra(map)
.passThrough(0)
.restrictedPackageName(XMiPushUtil.xiaomiPackageName)
// 使用默认提示音提示
.notifyType(1)
.build();
// 构建发送
Sender sender = new Sender(XMiPushUtil.xiaomiAppSecretKey);
JSONObject result = new JSONObject();
result.put("message", message);
result.put("sender", sender);
return result;
}
public static void sendMessage() throws Exception {
Constants.useOfficial();
//Constants.useSandbox();
Sender sender = new Sender(XMiPushUtil.xiaomiAppSecretKey);
String messagePayload="This is a message";
String title = "notification title";
String description = "notification description";
Message message = new Message.Builder()
.title(title)
.description(description).payload(messagePayload)
.restrictedPackageName(XMiPushUtil.xiaomiPackageName)
.notifyType(1) // 使用默认提示音提示
.build();
Result pushResult = sender.send(message, "t3yYJeEMLhr12+CQHiEBDjSFFUqywQ6rdqlZYwDUyqgeEt/DjxM5FIi3L+WdOJmg", 3); //根据regID,发送消息到指定设备上
logger.info("++++推送到小米结果为:{}"+pushRes