小米推送Java代码
maven
<dependency> <groupId>com.xiaomi</groupId> <artifactId>json-simple</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>com.xiaomi</groupId> <artifactId>MiPush_SDK_Server</artifactId> <version>2.2.18</version> </dependency>
java util
package com.tjg99.commons.util; import com.xiaomi.xmpush.server.Constants; import com.xiaomi.xmpush.server.Message; import com.xiaomi.xmpush.server.Sender; import com.xiaomi.xmpush.server.Sender.BROADCAST_TOPIC_OP; import net.sf.json.JSONObject; import org.json.simple.parser.ParseException; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; /** * 小米推送工具类 **/ public class MiPushUtil { private static String APP_SECRET_KEY_ANDROID = "asdasdasdasdasdasd"; private static String APP_SECRET_KEY_IOS = "asdasdasdasdasd=="; private static String MY_PACKAGE_NAME = "com.tjg99"; /** * 向所有设备发送推送 * @param messagePayload 消息 * @param title 消息标题 * @param description 消息描述 * @param ads_type 消息类型 * @param ads_links 消息链接 * @throws IOException * @throws ParseException **/ public static void sendAllBroadcast(String messagePayload, String title, String description, String ads_type, String ads_links) throws IOException, ParseException { Constants.useOfficial(); Map map = new HashMap(); map.put("ads_type", ads_type); map.put("ads_links", ads_links); Message message = new Message.Builder().title(title).description(description).payload(messagePayload) .extra("data", JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME) .notifyType(1).passThrough(0) // 使用默认提示音提示 .build(); //安卓推送 Sender sender_android = new Sender(APP_SECRET_KEY_ANDROID); // 根据topicList做并集运算, 发送消息到指定一组设备上 sender_android.broadcastAll(message, 3); //ios推送 Sender sender_ios = new Sender(APP_SECRET_KEY_IOS); // 根据topicList做并集运算, 发送消息到指定一组设备上 sender_ios.broadcastAll(message, 3); } /** * 指定标签推送(推送指定类型用户) * @param messagePayload 消息 * @param title 消息标题 * @param description 消息描述 * @param ads_type 消息类型 * @param ads_links 消息链接 * @param topicList 指定推送类型 * @throws IOException * @throws ParseException **/ public static void sendBroadcast(String messagePayload, String title, String description, String ads_type, String ads_links, List<String> topicList) throws IOException, ParseException { Constants.useOfficial(); Map map = new HashMap(); map.put("ads_type", ads_type); map.put("ads_links", ads_links); Message message = new Message.Builder().title(title).description(description).payload(messagePayload) .extra("data", JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME) .notifyType(1).passThrough(0) // 使用默认提示音提示 .build(); // 安卓推送 Sender sender_android = new Sender(APP_SECRET_KEY_ANDROID); // 根据topicList做并集运算, 发送消息到指定一组设备上 sender_android.multiTopicBroadcast(message, topicList, BROADCAST_TOPIC_OP.UNION, 3); // ios推送 //Constants.useSandbox(); Sender sender_ios = new Sender(APP_SECRET_KEY_IOS); // 根据topicList做并集运算, 发送消息到指定一组设备上 sender_ios.multiTopicBroadcast(message, topicList, BROADCAST_TOPIC_OP.UNION, 3); } /** * 指定alias推送(单个或多个) * @param messagePayload 消息 * @param title 消息标题 * @param description 消息描述 * @param ads_type 消息类型 * @param ads_links 消息链接 * @param aliasList 指定alias * @throws IOException * @throws ParseException **/ public static void sendMessageToAliases(String messagePayload, String title, String description, String ads_type, String ads_links, List<String> aliasList) throws IOException, ParseException { Constants.useOfficial(); Map map = new HashMap(); map.put("ads_type", ads_type); map.put("ads_links", ads_links); Message message = new Message.Builder().title(title).description(description).payload(messagePayload) .extra("data", JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME) .notifyType(1).passThrough(0) // 使用默认提示音提示 .build(); //安卓推送 Sender sender_android = new Sender(APP_SECRET_KEY_ANDROID); sender_android.sendToAlias(message, aliasList, 3); //ios推送 //Constants.useSandbox(); Sender sender_ios = new Sender(APP_SECRET_KEY_IOS); sender_ios.sendToAlias(message, aliasList, 3); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接
赞 (0)