小米推送

首先要去官方网站下载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 {
    
     private final static int PASS_THROUGH_MESSAGE = 0;// 1表示透传消息
        private final static int NOTIFICATION_MESSAGE = 0;// 0表示通知栏消息
        private final static int PASS_THROUGH = NOTIFICATION_MESSAGE;// 1表示透传消息,0表示通知栏消息

        private final static int DEFAULT_ALL = -1;
        private final static int DEFAULT_SOUND = 1; // 使用默认提示音提
        private final static int DEFAULT_VIBRATE = 2; // 使用默认震动提示
        private final static int DEFAULT_LIGHTS = 4; // 使用默认led灯光提示
        private final static int NOTIFY_TYPE = DEFAULT_SOUND;

        private static String ANDROID_APP_SECRET = "...";//填写自己的安卓APP_SECRET
        private static String IOS_APP_SECRET = ".....";//填写自己的IOS APP_SECRET
        private static String ANDROID_PACKAGE_NAME = ".....";
        private static int DEVELOPMENT_MODE = 1; // 1=正式环境/0=测试

        public final static int TYPE_ANDROID = 0;
        public final static int TYPE_IOS = 1;

        /**
         * 构造函数
         */
        public MiPushUtils(String androidAppSecret, String androidPackageName, String iosAppSecret, int developmentMode) {
            this.ANDROID_APP_SECRET = androidAppSecret;
            this.ANDROID_PACKAGE_NAME = androidPackageName;
            this.IOS_APP_SECRET = iosAppSecret;
            MiPushUtils.DEVELOPMENT_MODE = developmentMode;
        }

        /**
         * 调用小米推送
         */
        public static void reStartPush(int deviceType) {
            // 如果为测试环境            
            if (DEVELOPMENT_MODE == 0) {
                // 测试环境只提供对IOS支持,不支持Android
                Constants.useSandbox();
                if (deviceType == TYPE_ANDROID) {
                    Constants.useOfficial();
                }
            } else {
                // 正式环境
                Constants.useOfficial();
            }
        }

        /**
         * 构建android推送信息
         *
         * @param title
         * @param content
         * @param jsonObjectPayload
         * @param timeToSend
         * @return
         */
        private static Message buildMessage2Android(String title, String content, JSONObject jsonObjectPayload, long timeToSend)
                throws Exception {
            Message message = new Message.Builder().title(title).description(content)
                    .payload(jsonObjectPayload.toJSONString()).restrictedPackageName(ANDROID_PACKAGE_NAME)// 设置包名
                    .passThrough(NOTIFICATION_MESSAGE) // 通知栏消息
                    .notifyType(NOTIFY_TYPE) // 使用默认提示音提示
                    .enableFlowControl(true) // 控制消息是否需要进行平缓发送
                    .timeToSend(timeToSend) // 定时推送时间
                    .build();
            return message;
        }

        /**
         * 构建ios推送信息
         *
         * @param content
         * @param jsonObjectPayload
         * @param timeToSend
         * @return
         */
        private static Message buildMessage2IOS(String content, JSONObject jsonObjectPayload, long timeToSend) throws Exception {
            Message message = new Message.IOSBuilder().description(content).soundURL("").badge(1) // 数字角标
                    .extra("payload", jsonObjectPayload.toJSONString()).timeToSend(timeToSend).build();
            return message;
        }

        /**
         * 构建发送信息
         *
         * @param title
         * @param content
         * @param jsonObjectPayload
         * @param deviceType
         * @param timeToSend
         * @return Message
         */
        private static Message buildMessage(String title, String content, JSONObject jsonObjectPayload, int deviceType,
                long timeToSend) throws Exception {
            Message message = null;
            if (deviceType == TYPE_ANDROID) {
                message = buildMessage2Android(title, content, jsonObjectPayload, timeToSend);
            } else if (deviceType == TYPE_IOS) {
                message = buildMessage2IOS(content, jsonObjectPayload, timeToSend);
            }
            return message;
        }

        /**
         * 向所有设备发送推送信息
         *
         * @param title
         * @param content
         * @param jsonObjectPayload
         * @param deviceType
         * @param timeToSend
         * @throws Exception
         */
        public Result sendBroadcastAll(String title, String content, JSONObject jsonObjectPayload, int deviceType,
                long timeToSend) throws Exception {
            reStartPush(deviceType);// 准备小米推送

            Sender sender = null;
            if (deviceType == TYPE_ANDROID) {
                sender = new Sender(ANDROID_APP_SECRET); // 需要根据appSecert来发送
            } else if (deviceType == TYPE_IOS) {
                sender = new Sender(IOS_APP_SECRET); // 需要根据appSecert来发送
            }
            Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
            Result result = sender.broadcastAll(message, 1);// 推送消息给所有设备,不重试

            return result;
        }

        /**
         * 根据regid发送一条短信
         *
         * @param title
         * @param content
         * @param jsonObjectPayload
         * @param regId
         * @param deviceType
         * @throws Exception
         */
        public Result sendMessageToRegId(String title, String content, JSONObject jsonObjectPayload, String regId, int deviceType,
                long timeToSend) throws Exception {
            reStartPush(deviceType);// 准备小米推送

            Sender sender = null;
            if (deviceType == TYPE_ANDROID) {
                sender = new Sender(ANDROID_APP_SECRET); // 需要根据appSecert来发送
            } else if (deviceType == TYPE_IOS) {
                sender = new Sender(IOS_APP_SECRET); // 需要根据appSecert来发送
            }
            Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
            Result result = sender.send(message, regId,1); // 根据regID,发送消息到指定设备上,不重试。
            return result;
        }

        /**
         * 根据alias发送一条短信
         *
         * @param title
         * @param content
         * @param jsonObjectPayload
         * @param userMobile
         * @param deviceType
         * @throws Exception
         */
        public static   Result sendMessageToAlias(String title, String content, JSONObject jsonObjectPayload, String userMobile,
                int deviceType, long timeToSend) throws Exception{
            reStartPush(deviceType);// 准备小米推送

            Sender sender = null;
            if (deviceType == TYPE_ANDROID) {
                sender = new Sender(ANDROID_APP_SECRET); // 需要根据appSecert来发送
            } else if (deviceType == TYPE_IOS) {
                sender = new Sender(IOS_APP_SECRET); // 需要根据appSecert来发送
            }
            Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
            Result result = sender.sendToAlias(message, userMobile, 3); // 根据alias,发送消息到指定设备上,不重试。
            return result;
        }
        
        /**
         * 根据alias发送多条短信
         *
         * @param title
         * @param content
         * @param jsonObjectPayload
         * @param userMobile
         * @param deviceType
         * @throws Exception
         */
        public static   Result sendMessagesToAlias(String title, String content, JSONObject jsonObjectPayload, List<String> aliasList,
                int deviceType, long timeToSend) throws Exception{
            System.out.println("准备启动小米推送环境");
            reStartPush(deviceType);// 准备小米推送
            System.out.println("准备小米推送");
            Sender sender = null;
            if (deviceType == TYPE_ANDROID) {
                sender = new Sender(ANDROID_APP_SECRET); // 需要根据appSecert来发送
                System.out.println("ANDROID_APP_SECRET需要根据appSecert来发送");
            } else if (deviceType == TYPE_IOS) {
                sender = new Sender(IOS_APP_SECRET); // 需要根据appSecert来发送
                System.out.println("IOS_APP_SECRET需要根据appSecert来发送");
            }
            Message message = buildMessage(title, content, jsonObjectPayload, deviceType, timeToSend);
            Result result = sender.sendToAlias(message, aliasList, 3); // 根据alias,发送消息到指定设备上,不重试。
            return result;
        }
        
       
        
        public static void main(String[] args) throws Exception {
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
            df.format(new Date());// new Date()为获取当前系统时间
            String title="时间"+df.format(new Date());
            String content="收到了吗请关注!";

            
            Map<String, String> map = new HashMap<String, String>();
            map.put("url", "http://www.baidu.com");            
        
            
            Map<String,  Object> params = new HashMap<String,  Object>();
            params.put("actionType", "openUrl");
            params.put("intentData", map);    
            

            JSONObject itemJSONObj = JSONObject.parseObject(JSON.toJSONString(params));
            
             List<String> aliasList = new ArrayList<String>();
            // aliasList.add("123460");  //alias非空白,不能包含逗号
             aliasList.add("123482");  //alias非空白,不能包含逗号
            //安卓推送
        /*    Result result=MiPushUtils.sendMessageToAlias(title,content,itemJSONObj,"123482",0,0);//123460
            System.out.println("Server response: "+"MessageId: " + result.getMessageId()
            + " 推送结果: " + result.getErrorCode().getDescription()
            + " Reason: " + result.getReason());*/
            
            Result result2=MiPushUtils.sendMessagesToAlias(title,content,itemJSONObj,aliasList,0,0);//123460
            System.out.println("Server response: "+"MessageId: " + result2.getMessageId()
            + " 安卓推送结果: " + result2.getErrorCode().getDescription()
            + " Reason: " + result2.getReason());
            
            //ios推送
            Result result1=MiPushUtils.sendMessageToAlias(title,content,itemJSONObj,"123484",1,0);//123460
            System.out.println("Server response: "+"MessageId: " + result1.getMessageId()
            + " IOS推送结果: " + result1.getErrorCode().getDescription()
            + " Reason: " + result1.getReason());
            
        }
    

}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值