小米推送

Love The Way You Lie 2022-03-26 13:36 391阅读 0赞

首先要去官方网站下载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 {

  1. private final static int PASS\_THROUGH\_MESSAGE = 0;// 1表示透传消息
  2. private final static int NOTIFICATION\_MESSAGE = 0;// 0表示通知栏消息
  3. private final static int PASS\_THROUGH = NOTIFICATION\_MESSAGE;// 1表示透传消息,0表示通知栏消息
  4. private final static int DEFAULT\_ALL = -1;
  5. private final static int DEFAULT\_SOUND = 1; // 使用默认提示音提
  6. private final static int DEFAULT\_VIBRATE = 2; // 使用默认震动提示
  7. private final static int DEFAULT\_LIGHTS = 4; // 使用默认led灯光提示
  8. private final static int NOTIFY\_TYPE = DEFAULT\_SOUND;
  9. private static String ANDROID\_APP\_SECRET = "...";//填写自己的安卓APP\_SECRET
  10. private static String IOS\_APP\_SECRET = ".....";//填写自己的IOS APP\_SECRET
  11. private static String ANDROID\_PACKAGE\_NAME = ".....";
  12. private static int DEVELOPMENT\_MODE = 1; // 1=正式环境/0=测试
  13. public final static int TYPE\_ANDROID = 0;
  14. public final static int TYPE\_IOS = 1;
  15. /\*\*
  16. \* 构造函数
  17. \*/
  18. public MiPushUtils(String androidAppSecret, String androidPackageName, String iosAppSecret, int developmentMode) \{
  19. this.ANDROID\_APP\_SECRET = androidAppSecret;
  20. this.ANDROID\_PACKAGE\_NAME = androidPackageName;
  21. this.IOS\_APP\_SECRET = iosAppSecret;
  22. MiPushUtils.DEVELOPMENT\_MODE = developmentMode;
  23. \}
  24. /\*\*
  25. \* 调用小米推送
  26. \*/
  27. public static void reStartPush(int deviceType) \{
  28. // 如果为测试环境
  29. if (DEVELOPMENT\_MODE == 0) \{
  30. // 测试环境只提供对IOS支持,不支持Android
  31. Constants.useSandbox();
  32. if (deviceType == TYPE\_ANDROID) \{
  33. Constants.useOfficial();
  34. \}
  35. \} else \{
  36. // 正式环境
  37. Constants.useOfficial();
  38. \}
  39. \}
  40. /\*\*
  41. \* 构建android推送信息
  42. \*
  43. \* @param title
  44. \* @param content
  45. \* @param jsonObjectPayload
  46. \* @param timeToSend
  47. \* @return
  48. \*/
  49. private static Message buildMessage2Android(String title, String content, JSONObject jsonObjectPayload, long timeToSend)
  50. throws Exception \{
  51. Message message = new Message.Builder().title(title).description(content)
  52. .payload(jsonObjectPayload.toJSONString()).restrictedPackageName(ANDROID\_PACKAGE\_NAME)// 设置包名
  53. .passThrough(NOTIFICATION\_MESSAGE) // 通知栏消息
  54. .notifyType(NOTIFY\_TYPE) // 使用默认提示音提示
  55. .enableFlowControl(true) // 控制消息是否需要进行平缓发送
  56. .timeToSend(timeToSend) // 定时推送时间
  57. .build();
  58. return message;
  59. \}
  60. /\*\*
  61. \* 构建ios推送信息
  62. \*
  63. \* @param content
  64. \* @param jsonObjectPayload
  65. \* @param timeToSend
  66. \* @return
  67. \*/
  68. private static Message buildMessage2IOS(String content, JSONObject jsonObjectPayload, long timeToSend) throws Exception \{
  69. Message message = new Message.IOSBuilder().description(content).soundURL("").badge(1) // 数字角标
  70. .extra("payload", jsonObjectPayload.toJSONString()).timeToSend(timeToSend).build();
  71. return message;
  72. \}
  73. /\*\*
  74. \* 构建发送信息
  75. \*
  76. \* @param title
  77. \* @param content
  78. \* @param jsonObjectPayload
  79. \* @param deviceType
  80. \* @param timeToSend
  81. \* @return Message
  82. \*/
  83. private static Message buildMessage(String title, String content, JSONObject jsonObjectPayload, int deviceType,
  84. long timeToSend) throws Exception \{
  85. Message message = null;
  86. if (deviceType == TYPE\_ANDROID) \{
  87. message = buildMessage2Android(title, content, jsonObjectPayload, timeToSend);
  88. \} else if (deviceType == TYPE\_IOS) \{
  89. message = buildMessage2IOS(content, jsonObjectPayload, timeToSend);
  90. \}
  91. return message;
  92. \}
  93. /\*\*
  94. \* 向所有设备发送推送信息
  95. \*
  96. \* @param title
  97. \* @param content
  98. \* @param jsonObjectPayload
  99. \* @param deviceType
  100. \* @param timeToSend
  101. \* @throws Exception
  102. \*/
  103. public Result sendBroadcastAll(String title, String content, JSONObject jsonObjectPayload, int deviceType,
  104. long timeToSend) throws Exception \{
  105. reStartPush(deviceType);// 准备小米推送
  106. Sender sender = null;
  107. if (deviceType == TYPE\_ANDROID) \{
  108. sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
  109. \} else if (deviceType == TYPE\_IOS) \{
  110. sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
  111. \}
  112. Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
  113. Result result = sender.broadcastAll(message, 1);// 推送消息给所有设备,不重试
  114. return result;
  115. \}
  116. /\*\*
  117. \* 根据regid发送一条短信
  118. \*
  119. \* @param title
  120. \* @param content
  121. \* @param jsonObjectPayload
  122. \* @param regId
  123. \* @param deviceType
  124. \* @throws Exception
  125. \*/
  126. public Result sendMessageToRegId(String title, String content, JSONObject jsonObjectPayload, String regId, int deviceType,
  127. long timeToSend) throws Exception \{
  128. reStartPush(deviceType);// 准备小米推送
  129. Sender sender = null;
  130. if (deviceType == TYPE\_ANDROID) \{
  131. sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
  132. \} else if (deviceType == TYPE\_IOS) \{
  133. sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
  134. \}
  135. Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
  136. Result result = sender.send(message, regId,1); // 根据regID,发送消息到指定设备上,不重试。
  137. return result;
  138. \}
  139. /\*\*
  140. \* 根据alias发送一条短信
  141. \*
  142. \* @param title
  143. \* @param content
  144. \* @param jsonObjectPayload
  145. \* @param userMobile
  146. \* @param deviceType
  147. \* @throws Exception
  148. \*/
  149. public static Result sendMessageToAlias(String title, String content, JSONObject jsonObjectPayload, String userMobile,
  150. int deviceType, long timeToSend) throws Exception\{
  151. reStartPush(deviceType);// 准备小米推送
  152. Sender sender = null;
  153. if (deviceType == TYPE\_ANDROID) \{
  154. sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
  155. \} else if (deviceType == TYPE\_IOS) \{
  156. sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
  157. \}
  158. Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
  159. Result result = sender.sendToAlias(message, userMobile, 3); // 根据alias,发送消息到指定设备上,不重试。
  160. return result;
  161. \}
  162. /\*\*
  163. \* 根据alias发送多条短信
  164. \*
  165. \* @param title
  166. \* @param content
  167. \* @param jsonObjectPayload
  168. \* @param userMobile
  169. \* @param deviceType
  170. \* @throws Exception
  171. \*/
  172. public static Result sendMessagesToAlias(String title, String content, JSONObject jsonObjectPayload, List<String> aliasList,
  173. int deviceType, long timeToSend) throws Exception\{
  174. System.out.println("准备启动小米推送环境");
  175. reStartPush(deviceType);// 准备小米推送
  176. System.out.println("准备小米推送");
  177. Sender sender = null;
  178. if (deviceType == TYPE\_ANDROID) \{
  179. sender = new Sender(ANDROID\_APP\_SECRET); // 需要根据appSecert来发送
  180. System.out.println("ANDROID\_APP\_SECRET需要根据appSecert来发送");
  181. \} else if (deviceType == TYPE\_IOS) \{
  182. sender = new Sender(IOS\_APP\_SECRET); // 需要根据appSecert来发送
  183. System.out.println("IOS\_APP\_SECRET需要根据appSecert来发送");
  184. \}
  185. Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
  186. Result result = sender.sendToAlias(message, aliasList, 3); // 根据alias,发送消息到指定设备上,不重试。
  187. return result;
  188. \}
  189. public static void main(String\[\] args) throws Exception \{
  190. SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
  191. df.format(new Date());// new Date()为获取当前系统时间
  192. String title="时间"+df.format(new Date());
  193. String content="收到了吗请关注!";
  194. Map<String, String> map = new HashMap<String, String>();
  195. map.put("url", "http://www.baidu.com");
  196. Map<String, Object> params = new HashMap<String, Object>();
  197. params.put("actionType", "openUrl");
  198. params.put("intentData", map);
  199. JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(params));
  200. List<String> aliasList = new ArrayList<String>();
  201. // aliasList.add("123460"); //alias非空白,不能包含逗号
  202. aliasList.add("123482"); //alias非空白,不能包含逗号
  203. //安卓推送
  204. /\* Result result=MiPushUtils.sendMessageToAlias(title,content,itemJSONObj,"123482",0,0);//123460
  205. System.out.println("Server response: "+"MessageId: " + result.getMessageId()
  206. + " 推送结果: " + result.getErrorCode().getDescription()
  207. + " Reason: " + result.getReason());\*/
  208. Result result2=MiPushUtils.sendMessagesToAlias(title,content,itemJSONObj,aliasList,0,0);//123460
  209. System.out.println("Server response: "+"MessageId: " + result2.getMessageId()
  210. + " 安卓推送结果: " + result2.getErrorCode().getDescription()
  211. + " Reason: " + result2.getReason());
  212. //ios推送
  213. Result result1=MiPushUtils.sendMessageToAlias(title,content,itemJSONObj,"123484",1,0);//123460
  214. System.out.println("Server response: "+"MessageId: " + result1.getMessageId()
  215. + " IOS推送结果: " + result1.getErrorCode().getDescription()
  216. + " Reason: " + result1.getReason());
  217. \}

}

发表评论

表情:
评论列表 (有 0 条评论,391人围观)

还没有评论,来说两句吧...

相关阅读

    相关 XPush 一个轻量级、可插拔的Android消息框架。一键集成(极光、友盟、华为、小米等),提供有效的保活机制,支持的拓展,充分解耦和业务逻辑

    XPush [项目地址][Link 1] 一个轻量级、可插拔的Android消息推送框架。一键集成推送(极光推送、友盟推送、华为、小米推送等),提供有效的保活机制,...