栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

小米推送Java代码

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

小米推送Java代码

maven

 
  com.xiaomi
  json-simple
  1.1.1
 
 
  com.xiaomi
  MiPush_SDK_Server
  2.2.18
 

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";
  
  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);
  }
  
  public static void sendBroadcast(String messagePayload, String title, String description, String ads_type,
     String ads_links, List 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);
  }
  
  public static void sendMessageToAliases(String messagePayload, String title, String description, String ads_type,
 String ads_links, List 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);
  }
}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对考高分网的支持。如果你想了解更多相关内容请查看下面相关链接

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/139040.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号