小米推送
首先要去官方网站下载sdk,把那两个jar包导入到项目里边.
2项目中用到申请下来的APP_SECRET和PACKAGE_NAME
package com.xypt.utils;
import com.xiaomi.xmpush.server.Result;
import com.xiaomi.xmpush.server.Sender;
import com.xiaomi.xmpush.server.Message;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.json.simple.parser.ParseException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xiaomi.xmpush.server.Constants;
public class MiPushUtils {
private final static int PASS\_THROUGH\_MESSAGE = 0;// 1表示透传消息
private final static int NOTIFICATION\_MESSAGE = 0;// 0表示通知栏消息
private final static int PASS\_THROUGH = NOTIFICATION\_MESSAGE;// 1表示透传消息,0表示通知栏消息
private final static int DEFAULT\_ALL = -1;
private final static int DEFAULT\_SOUND = 1; // 使用默认提示音提
private final static int DEFAULT\_VIBRATE = 2; // 使用默认震动提示
private final static int DEFAULT\_LIGHTS = 4; // 使用默认led灯光提示
private final static int NOTIFY\_TYPE = DEFAULT\_SOUND;
private static String ANDROID\_APP\_SECRET = "...";//填写自己的安卓APP\_SECRET
private static String IOS\_APP\_SECRET = ".....";//填写自己的IOS APP\_SECRET
private static String ANDROID\_PACKAGE\_NAME = ".....";
private static int DEVELOPMENT\_MODE = 1; // 1=正式环境/0=测试
public final static int TYPE\_ANDROID = 0;
public final static int TYPE\_IOS = 1;
/\*\*
\* 构造函数
\*/
public MiPushUtils(String androidAppSecret, String androidPackageName, String iosAppSecret, int developmentMode) \{
this.ANDROID\_APP\_SECRET = androidAppSecret;
this.ANDROID\_PACKAGE\_NAME = androidPackageName;
this.IOS\_APP\_SECRET = iosAppSecret;
MiPushUtils.DEVELOPMENT\_MODE = developmentMode;
\}
/\*\*
\* 调用小米推送
\*/
public static void reStartPush(int deviceType) \{
// 如果为测试环境
if (DEVELOPMENT\_MODE == 0) \{
// 测试环境只提供对IOS支持,不支持Android
Constants.useSandbox();
if (deviceType == TYPE\_ANDROID) \{
Constants.useOfficial();
\}
\} else \{
// 正式环境
Constants.useOfficial();
\}
\}
/\*\*
\* 构建android推送信息
\*
\* @param title
\* @param content
\* @param jsonObjectPayload
\* @param timeToSend
\* @return
\*/
private static Message buildMessage2Android(String title, String content, JSONObject jsonObjectPayload, long timeToSend)
throws Exception \{
Message message = new Message.Builder().title(title).description(content)
.payload(jsonObjectPayload.toJSONString()).restrictedPackageName(ANDROID\_PACKAGE\_NAME)// 设置包名
.passThrough(NOTIFICATION\_MESSAGE) // 通知栏消息
.notifyType(NOTIFY\_TYPE) // 使用默认提示音提示
.enableFlowControl(true) // 控制消息是否需要进行平缓发送
.timeToSend(timeToSend) // 定时推送时间
.build();
return message;
\}
/\*\*
\* 构建ios推送信息
\*
\* @param content
\* @param jsonObjectPayload
\* @param timeToSend
\* @return
\*/
private static Message buildMessage2IOS(String content, JSONObject jsonObjectPayload, long timeToSend) throws Exception \{
Message message = new Message.IOSBuilder().description(content).soundURL("").badge(1) // 数字角标
.extra("payload", jsonObjectPayload.toJSONString()).timeToSend(timeToSend).build();
return message;
\}
/\*\*
\* 构建发送信息
\*
\* @param title
\* @param content
\* @param jsonObjectPayload
\* @param deviceType
\* @param timeToSend
\* @return Message
\*/
private static Message buildMessage(String title, String content, JSONObject jsonObjectPayload, int deviceType,
long timeToSend) throws Exception \{
Message message = null;
if (deviceType == TYPE\_ANDROID) \{
message = buildMessage2Android(title, content, jsonObjectPayload, timeToSend);
\} else if (deviceType == TYPE\_IOS) \{
message = buildMessage2IOS(content, jsonObjectPayload, timeToSend);
\}
return message;
\}
/\*\*
\* 向所有设备发送推送信息
\*
\* @param title
\* @param content
\* @param jsonObjectPayload
\* @param deviceType
\* @param timeToSend
\* @throws Exception
\*/
public Result sendBroadcastAll(String title, String content, JSONObject jsonObjectPayload, int deviceType,
long timeToSend) throws Exception \{
reStartPush(deviceType);// 准备小米推送
Sender sender = null;
if (deviceType == TYPE\_ANDROID) \{
sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
\} else if (deviceType == TYPE\_IOS) \{
sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
\}
Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
Result result = sender.broadcastAll(message, 1);// 推送消息给所有设备,不重试
return result;
\}
/\*\*
\* 根据regid发送一条短信
\*
\* @param title
\* @param content
\* @param jsonObjectPayload
\* @param regId
\* @param deviceType
\* @throws Exception
\*/
public Result sendMessageToRegId(String title, String content, JSONObject jsonObjectPayload, String regId, int deviceType,
long timeToSend) throws Exception \{
reStartPush(deviceType);// 准备小米推送
Sender sender = null;
if (deviceType == TYPE\_ANDROID) \{
sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
\} else if (deviceType == TYPE\_IOS) \{
sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
\}
Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
Result result = sender.send(message, regId,1); // 根据regID,发送消息到指定设备上,不重试。
return result;
\}
/\*\*
\* 根据alias发送一条短信
\*
\* @param title
\* @param content
\* @param jsonObjectPayload
\* @param userMobile
\* @param deviceType
\* @throws Exception
\*/
public static Result sendMessageToAlias(String title, String content, JSONObject jsonObjectPayload, String userMobile,
int deviceType, long timeToSend) throws Exception\{
reStartPush(deviceType);// 准备小米推送
Sender sender = null;
if (deviceType == TYPE\_ANDROID) \{
sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
\} else if (deviceType == TYPE\_IOS) \{
sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
\}
Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
Result result = sender.sendToAlias(message, userMobile, 3); // 根据alias,发送消息到指定设备上,不重试。
return result;
\}
/\*\*
\* 根据alias发送多条短信
\*
\* @param title
\* @param content
\* @param jsonObjectPayload
\* @param userMobile
\* @param deviceType
\* @throws Exception
\*/
public static Result sendMessagesToAlias(String title, String content, JSONObject jsonObjectPayload, List<String> aliasList,
int deviceType, long timeToSend) throws Exception\{
System.out.println("准备启动小米推送环境");
reStartPush(deviceType);// 准备小米推送
System.out.println("准备小米推送");
Sender sender = null;
if (deviceType == TYPE\_ANDROID) \{
sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
System.out.println("ANDROID\_APP\_SECRET需要根据appSecert来发送");
\} else if (deviceType == TYPE\_IOS) \{
sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
System.out.println("IOS\_APP\_SECRET需要根据appSecert来发送");
\}
Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
Result result = sender.sendToAlias(message, aliasList, 3); // 根据alias,发送消息到指定设备上,不重试。
return result;
\}
public static void main(String\[\] args) throws Exception \{
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
df.format(new Date());// new Date()为获取当前系统时间
String title="时间"+df.format(new Date());
String content="收到了吗请关注!";
Map<String, String> map = new HashMap<String, String>();
map.put("url", "http://www.baidu.com");
Map<String, Object> params = new HashMap<String, Object>();
params.put("actionType", "openUrl");
params.put("intentData", map);
JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(params));
List<String> aliasList = new ArrayList<String>();
// aliasList.add("123460"); //alias非空白,不能包含逗号
aliasList.add("123482"); //alias非空白,不能包含逗号
//安卓推送
/\* Result result=MiPushUtils.sendMessageToAlias(title,content,itemJSONObj,"123482",0,0);//123460
System.out.println("Server response: "+"MessageId: " + result.getMessageId()
+ " 推送结果: " + result.getErrorCode().getDescription()
+ " Reason: " + result.getReason());\*/
Result result2=MiPushUtils.sendMessagesToAlias(title,content,itemJSONObj,aliasList,0,0);//123460
System.out.println("Server response: "+"MessageId: " + result2.getMessageId()
+ " 安卓推送结果: " + result2.getErrorCode().getDescription()
+ " Reason: " + result2.getReason());
//ios推送
Result result1=MiPushUtils.sendMessageToAlias(title,content,itemJSONObj,"123484",1,0);//123460
System.out.println("Server response: "+"MessageId: " + result1.getMessageId()
+ " IOS推送结果: " + result1.getErrorCode().getDescription()
+ " Reason: " + result1.getReason());
\}
}
还没有评论,来说两句吧...