小米推送server服務端
阿新 • • 發佈:2018-12-26
工具類:
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_ALL; private String ANDROID_APP_SECRET = ""; private String IOS_APP_SECRET = ""; private 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 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(PASS_THROUGH) // 訊息使用透傳方式 .notifyType(NOTIFY_TYPE) // 使用預設提示音提示 .enableFlowControl(true) // 控制訊息是否需要進行平緩傳送 .timeToSend(timeToSend) // 定時推送時間 .build(); return message; } /** * 構建ios推送資訊 * * @param content * @param jsonObjectPayload * @param timeToSend * @return */ private Message buildMessage2IOS(String content, JSONObject jsonObjectPayload, long timeToSend) throws Exception { Message message = new Message.IOSBuilder().description(content).badge(1) // 數字角標 .extra("payload", jsonObjectPayload.toJSONString()).timeToSend(timeToSend).build(); return message; } /** * 構建傳送資訊 * * @param title * @param content * @param jsonObjectPayload * @param deviceType * @param timeToSend * @return Message */ private 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, 0);// 推送訊息給所有裝置,不重試 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, 0); // 根據regID,傳送訊息到指定裝置上,不重試。 return result; } /** * 根據alias傳送一條簡訊 * * @param title * @param content * @param jsonObjectPayload * @param userMobile * @param deviceType * @throws Exception */ @SuppressWarnings("unused") private 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, 0); // 根據alias,傳送訊息到指定裝置上,不重試。 return result; } }