小米推送Java代码
maven
com.xiaomi json-simple 1.1.1 com.xiaomi MiPush_SDK_Server 2.2.18
javautil
packagecom.tjg99.commons.util;
importcom.xiaomi.xmpush.server.Constants;
importcom.xiaomi.xmpush.server.Message;
importcom.xiaomi.xmpush.server.Sender;
importcom.xiaomi.xmpush.server.Sender.BROADCAST_TOPIC_OP;
importnet.sf.json.JSONObject;
importorg.json.simple.parser.ParseException;
importjava.io.IOException;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
/**
*小米推送工具类
**/
publicclassMiPushUtil{
privatestaticStringAPP_SECRET_KEY_ANDROID="asdasdasdasdasdasd";
privatestaticStringAPP_SECRET_KEY_IOS="asdasdasdasdasd==";
privatestaticStringMY_PACKAGE_NAME="com.tjg99";
/**
*向所有设备发送推送
*@parammessagePayload消息
*@paramtitle消息标题
*@paramdescription消息描述
*@paramads_type消息类型
*@paramads_links消息链接
*@throwsIOException
*@throwsParseException
**/
publicstaticvoidsendAllBroadcast(StringmessagePayload,Stringtitle,Stringdescription,Stringads_type,
Stringads_links)throwsIOException,ParseException{
Constants.useOfficial();
Mapmap=newHashMap();
map.put("ads_type",ads_type);
map.put("ads_links",ads_links);
Messagemessage=newMessage.Builder().title(title).description(description).payload(messagePayload)
.extra("data",JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1).passThrough(0)//使用默认提示音提示
.build();
//安卓推送
Sendersender_android=newSender(APP_SECRET_KEY_ANDROID);
//根据topicList做并集运算,发送消息到指定一组设备上
sender_android.broadcastAll(message,3);
//ios推送
Sendersender_ios=newSender(APP_SECRET_KEY_IOS);
//根据topicList做并集运算,发送消息到指定一组设备上
sender_ios.broadcastAll(message,3);
}
/**
*指定标签推送(推送指定类型用户)
*@parammessagePayload消息
*@paramtitle消息标题
*@paramdescription消息描述
*@paramads_type消息类型
*@paramads_links消息链接
*@paramtopicList指定推送类型
*@throwsIOException
*@throwsParseException
**/
publicstaticvoidsendBroadcast(StringmessagePayload,Stringtitle,Stringdescription,Stringads_type,
Stringads_links,ListtopicList)throwsIOException,ParseException{
Constants.useOfficial();
Mapmap=newHashMap();
map.put("ads_type",ads_type);
map.put("ads_links",ads_links);
Messagemessage=newMessage.Builder().title(title).description(description).payload(messagePayload)
.extra("data",JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1).passThrough(0)//使用默认提示音提示
.build();
//安卓推送
Sendersender_android=newSender(APP_SECRET_KEY_ANDROID);
//根据topicList做并集运算,发送消息到指定一组设备上
sender_android.multiTopicBroadcast(message,topicList,BROADCAST_TOPIC_OP.UNION,3);
//ios推送
//Constants.useSandbox();
Sendersender_ios=newSender(APP_SECRET_KEY_IOS);
//根据topicList做并集运算,发送消息到指定一组设备上
sender_ios.multiTopicBroadcast(message,topicList,BROADCAST_TOPIC_OP.UNION,3);
}
/**
*指定alias推送(单个或多个)
*@parammessagePayload消息
*@paramtitle消息标题
*@paramdescription消息描述
*@paramads_type消息类型
*@paramads_links消息链接
*@paramaliasList指定alias
*@throwsIOException
*@throwsParseException
**/
publicstaticvoidsendMessageToAliases(StringmessagePayload,Stringtitle,Stringdescription,Stringads_type,
Stringads_links,ListaliasList)throwsIOException,ParseException{
Constants.useOfficial();
Mapmap=newHashMap();
map.put("ads_type",ads_type);
map.put("ads_links",ads_links);
Messagemessage=newMessage.Builder().title(title).description(description).payload(messagePayload)
.extra("data",JSONObject.fromObject(map).toString()).restrictedPackageName(MY_PACKAGE_NAME)
.notifyType(1).passThrough(0)//使用默认提示音提示
.build();
//安卓推送
Sendersender_android=newSender(APP_SECRET_KEY_ANDROID);
sender_android.sendToAlias(message,aliasList,3);
//ios推送
//Constants.useSandbox();
Sendersender_ios=newSender(APP_SECRET_KEY_IOS);
sender_ios.sendToAlias(message,aliasList,3);
}
}
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对毛票票的支持。如果你想了解更多相关内容请查看下面相关链接
声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:czq8825#qq.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。