小米推送Java代码
程序员文章站
2024-02-22 12:46:28
maven
com.xiaomi
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); } }
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接
上一篇: Java动态调用类中方法代码