From 4f5a5b0a00c01491f009074351089f086f950d3e Mon Sep 17 00:00:00 2001 From: admin <weikou2014> Date: 星期二, 01 六月 2021 19:09:48 +0800 Subject: [PATCH] 系统配置改为数据库 --- fanli/src/main/java/com/yeshi/fanli/util/push/IOSPushUtil.java | 452 ++++++++++++++++++++++++++++---------------------------- 1 files changed, 224 insertions(+), 228 deletions(-) diff --git a/fanli/src/main/java/com/yeshi/fanli/util/push/IOSPushUtil.java b/fanli/src/main/java/com/yeshi/fanli/util/push/IOSPushUtil.java index 890d478..20123ae 100644 --- a/fanli/src/main/java/com/yeshi/fanli/util/push/IOSPushUtil.java +++ b/fanli/src/main/java/com/yeshi/fanli/util/push/IOSPushUtil.java @@ -33,253 +33,249 @@ @Component public class IOSPushUtil { - private static IOSPushUtil iosPushUtil; + private static IOSPushUtil iosPushUtil; - @Autowired - private BusinessSystemService businessSystemService; + @Autowired + private BusinessSystemService businessSystemService; - @PostConstruct - public void init() { - iosPushUtil = this; - iosPushUtil.businessSystemService = this.businessSystemService; - } + @PostConstruct + public void init() { + iosPushUtil = this; + iosPushUtil.businessSystemService = this.businessSystemService; + } - /** - * - * 鏂规硶璇存槑: IOS 澶氭帹 - * - * @author mawurui createTime 2018骞�5鏈�8鏃� 涓婂崍10:19:57 - * @param deviceToken - * @param packages - * @param alert - * @return - * @throws Exception - */ - public static String allPushIOS(List<String> deviceTokenList, MessageInfo info, String url, int type) - throws Exception { + /** + * 鏂规硶璇存槑: IOS 澶氭帹 + * + * @param deviceToken + * @param packages + * @param alert + * @return + * @throws Exception + * @author mawurui createTime 2018骞�5鏈�8鏃� 涓婂崍10:19:57 + */ + public static String allPushIOS(List<String> deviceTokenList, MessageInfo info, String url, int type, String pwd) + throws Exception { - // if (Constant.IS_TEST) - // return null; + // if (Constant.IS_TEST) + // return null; - Gson gson = new Gson(); - PushLogHelper.iosInfo("IOS鎺ㄩ�佽澶囨暟涓�:" + deviceTokenList.size()); - PushLogHelper.iosInfo("IOS鎺ㄩ�佺殑鍐呭涓猴細" + gson.toJson(info)); - List<String> tokenList = new ArrayList<String>(); - for (String deviceToken : deviceTokenList) { - tokenList.add(deviceToken); - } - // json鑷畾涔変紶鍊� + Gson gson = new Gson(); + PushLogHelper.iosInfo("IOS鎺ㄩ�佽澶囨暟涓�:" + deviceTokenList.size()); + PushLogHelper.iosInfo("IOS鎺ㄩ�佺殑鍐呭涓猴細" + gson.toJson(info)); + List<String> tokenList = new ArrayList<String>(); + for (String deviceToken : deviceTokenList) { + tokenList.add(deviceToken); + } + // json鑷畾涔変紶鍊� - JSONObject json = null; - if (type == PushTypeEnum.goodsdetail.getCode()) { - Long auctionId = null; - if (url.contains("id=")) { - String[] sts = url.split("\\?")[1].split("&"); - for (String st : sts) { - if (st.contains("id=")) { - auctionId = Long.parseLong(st.replace("id=", "").trim()); - } - } - } - if (auctionId == null) - throw new Exception("娣樺疂鍟嗗搧ID鎻愬彇鍑洪敊"); - json = IOSPushFactory.createGoodsPush(auctionId, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.url.getCode()) { - String shortUrl = HttpUtil.getShortLink(url); - if (StringUtil.isNullOrEmpty(shortUrl)) - throw new Exception("鑾峰彇鐭摼鍑洪敊"); - json = IOSPushFactory.createURLPush(shortUrl, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.ZNX.getCode()) { - json = IOSPushFactory.createZNXPush(info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.weex.getCode()) { - String shortUrl = HttpUtil.getShortLink(url); - if (StringUtil.isNullOrEmpty(shortUrl)) - throw new Exception("鑾峰彇鐭摼鍑洪敊"); - json = IOSPushFactory.createWEEXPush(shortUrl, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.baichuan.getCode()) { - String shortUrl = url; - // HttpUtil.getShortLink(url); - // if (StringUtil.isNullOrEmpty(shortUrl)) - // throw new Exception("鑾峰彇鐭摼鍑洪敊"); - json = IOSPushFactory.createBaiChuanPush(shortUrl, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.welfare.getCode()) { - json = IOSPushFactory.createWelfarePush(info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.signin.getCode()) { - json = IOSPushFactory.createUserSignInPush(info.getTitle(), info.getContent()); - } + JSONObject json = null; + if (type == PushTypeEnum.goodsdetail.getCode()) { + Long auctionId = null; + if (url.contains("id=")) { + String[] sts = url.split("\\?")[1].split("&"); + for (String st : sts) { + if (st.contains("id=")) { + auctionId = Long.parseLong(st.replace("id=", "").trim()); + } + } + } + if (auctionId == null) + throw new Exception("娣樺疂鍟嗗搧ID鎻愬彇鍑洪敊"); + json = IOSPushFactory.createGoodsPush(auctionId, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.url.getCode()) { + String shortUrl = HttpUtil.getShortLink(url); + if (StringUtil.isNullOrEmpty(shortUrl)) + throw new Exception("鑾峰彇鐭摼鍑洪敊"); + json = IOSPushFactory.createURLPush(shortUrl, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.ZNX.getCode()) { + json = IOSPushFactory.createZNXPush(info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.weex.getCode()) { + String shortUrl = HttpUtil.getShortLink(url); + if (StringUtil.isNullOrEmpty(shortUrl)) + throw new Exception("鑾峰彇鐭摼鍑洪敊"); + json = IOSPushFactory.createWEEXPush(shortUrl, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.baichuan.getCode()) { + String shortUrl = url; + // HttpUtil.getShortLink(url); + // if (StringUtil.isNullOrEmpty(shortUrl)) + // throw new Exception("鑾峰彇鐭摼鍑洪敊"); + json = IOSPushFactory.createBaiChuanPush(shortUrl, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.welfare.getCode()) { + json = IOSPushFactory.createWelfarePush(info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.signin.getCode()) { + json = IOSPushFactory.createUserSignInPush(info.getTitle(), info.getContent()); + } - // 鍒嗙粍鎺ㄩ�� 姣�50涓澶囦负涓�缁� + // 鍒嗙粍鎺ㄩ�� 姣�50涓澶囦负涓�缁� - InputStream certificate = IOSPushUtil.class.getClassLoader() - .getResourceAsStream("certificate/pushCertificate.p12"); // 璇诲彇.p12鏂囦欢 - String certificatePassword = Constant.systemCommonConfig.getIosPushCertificatePwd(); - if (tokenList != null && tokenList.size() > 0) { - int pageSize = 50; - int page = tokenList.size() % pageSize == 0 ? tokenList.size() / pageSize : tokenList.size() / pageSize + 1; - for (int i = 0; i < page; i++) { - int start = i * pageSize; - int end = start + pageSize; - if (end > tokenList.size()) - end = tokenList.size(); - pushIOS(tokenList.subList(start, end), json, certificate, certificatePassword); - } + InputStream certificate = IOSPushUtil.class.getClassLoader() + .getResourceAsStream("certificate/pushCertificate.p12"); // 璇诲彇.p12鏂囦欢 + String certificatePassword = pwd; + if (tokenList != null && tokenList.size() > 0) { + int pageSize = 50; + int page = tokenList.size() % pageSize == 0 ? tokenList.size() / pageSize : tokenList.size() / pageSize + 1; + for (int i = 0; i < page; i++) { + int start = i * pageSize; + int end = start + pageSize; + if (end > tokenList.size()) + end = tokenList.size(); + pushIOS(tokenList.subList(start, end), json, certificate, certificatePassword); + } - } + } - return null; - } + return null; + } - public static String pushIOS(List<String> deviceTokenList, JSONObject json, InputStream certificate, - String certificatePWD) throws Exception { - List<String> tokenList = new ArrayList<String>(); - for (String deviceToken : deviceTokenList) { - tokenList.add(deviceToken); - } - while (json.toString().getBytes().length > 256) { - // 棣栧厛鍓婂噺鍐呭锛岀劧鍚庡墛鍑忔爣棰� - String title = json.optJSONObject("aps").optJSONObject("alert").optString("title"); - String body = json.optJSONObject("aps").optJSONObject("alert").optString("body"); - if (StringUtil.isNullOrEmpty(body) || body.length() < 4) { - // 鍓婂噺鏍囬 - if (!StringUtil.isNullOrEmpty(title) && body.length() > 6) { - json.optJSONObject("aps").optJSONObject("alert").put("title", - title.substring(0, title.length() - 1)); - continue; - } - } else { - json.optJSONObject("aps").optJSONObject("alert").put("body", body.substring(0, body.length() - 1)); - continue; - } - } + public static String pushIOS(List<String> deviceTokenList, JSONObject json, InputStream certificate, + String certificatePWD) throws Exception { + List<String> tokenList = new ArrayList<String>(); + for (String deviceToken : deviceTokenList) { + tokenList.add(deviceToken); + } + while (json.toString().getBytes().length > 256) { + // 棣栧厛鍓婂噺鍐呭锛岀劧鍚庡墛鍑忔爣棰� + String title = json.optJSONObject("aps").optJSONObject("alert").optString("title"); + String body = json.optJSONObject("aps").optJSONObject("alert").optString("body"); + if (StringUtil.isNullOrEmpty(body) || body.length() < 4) { + // 鍓婂噺鏍囬 + if (!StringUtil.isNullOrEmpty(title) && body.length() > 6) { + json.optJSONObject("aps").optJSONObject("alert").put("title", + title.substring(0, title.length() - 1)); + continue; + } + } else { + json.optJSONObject("aps").optJSONObject("alert").put("body", body.substring(0, body.length() - 1)); + continue; + } + } - LogHelper.test("IOS鎺ㄩ�佺殑瀛楄妭鏁�" + json.toString().getBytes().length); - PushNotificationPayload payLoad = new PushNotificationPayload(); - payLoad = PushNotificationPayload.fromJSON(json.toString()); + LogHelper.test("IOS鎺ㄩ�佺殑瀛楄妭鏁�" + json.toString().getBytes().length); + PushNotificationPayload payLoad = new PushNotificationPayload(); + payLoad = PushNotificationPayload.fromJSON(json.toString()); - PushNotificationManager pushManager = new PushNotificationManager(); - // true锛氳〃绀虹殑鏄骇鍝佺嚎涓婂彂甯冩帹閫佹湇鍔� false锛氳〃绀虹殑鏄骇鍝佹祴璇曟帹閫佹湇鍔� - if (Constant.IS_TEST) - pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificate, certificatePWD, false)); - else - pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificate, certificatePWD, true)); + PushNotificationManager pushManager = new PushNotificationManager(); + // true锛氳〃绀虹殑鏄骇鍝佺嚎涓婂彂甯冩帹閫佹湇鍔� false锛氳〃绀虹殑鏄骇鍝佹祴璇曟帹閫佹湇鍔� + if (Constant.IS_TEST) + pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificate, certificatePWD, false)); + else + pushManager.initializeConnection(new AppleNotificationServerBasicImpl(certificate, certificatePWD, true)); - List<Device> deviceList = new ArrayList<Device>(); - for (String token : tokenList) { - if (!StringUtil.isNullOrEmpty(token)) - deviceList.add(new BasicDevice(token)); - } - // 寮�濮嬫帹閫� - List<PushedNotification> notificationList = pushManager.sendNotifications(payLoad, deviceList); - Gson gson = new Gson(); - if (notificationList != null) - for (PushedNotification notification : notificationList) { - PushLogHelper.iosInfo(gson.toJson(notification)); - } - pushManager.stopConnection(); - return null; - } + List<Device> deviceList = new ArrayList<Device>(); + for (String token : tokenList) { + if (!StringUtil.isNullOrEmpty(token)) + deviceList.add(new BasicDevice(token)); + } + // 寮�濮嬫帹閫� + List<PushedNotification> notificationList = pushManager.sendNotifications(payLoad, deviceList); + Gson gson = new Gson(); + if (notificationList != null) + for (PushedNotification notification : notificationList) { + PushLogHelper.iosInfo(gson.toJson(notification)); + } + pushManager.stopConnection(); + return null; + } - /** - * - * @author mawurui createTime 2018骞�5鏈�8鏃� 涓嬪崍12:12:19 - * @param info - * @param json - * @param pushRecord - * @return - * @throws Exception - */ - public static int allFanLiQuanDevice(System b_IOS, List<String> deviceTokenList, String PACKAGES, String title, - String content, String url, PushRecord pushRecord, int type) throws Exception { + /** + * @param info + * @param json + * @param pushRecord + * @return + * @throws Exception + * @author mawurui createTime 2018骞�5鏈�8鏃� 涓嬪崍12:12:19 + */ + public static int allFanLiQuanDevice(System b_IOS, List<String> deviceTokenList, String PACKAGES, String title, + String content, String url, PushRecord pushRecord, int type,String pwd) throws Exception { - return pushApp(deviceTokenList, PACKAGES, title, content, b_IOS, url, pushRecord, type); - } + return pushApp(deviceTokenList, PACKAGES, title, content, b_IOS, url, pushRecord, type,pwd); + } - /** - * - * @param info - * @param params - * @param pushRecord - * @return 1:閮芥垚鍔� 2锛氫粎android 鎴愬姛 3.浠匢OS鎴愬姛 4.閮藉け璐� - * @throws Exception - */ - private static int pushApp(List<String> deviceTokenList, String PACKAGES, String title, String content, - System b_IOS, String url, PushRecord pushRecord, int type) throws Exception { - MessageInfo info = new MessageInfo(); - info.setTitle(title); - info.setContent(content); - String IOS = allPushIOS(deviceTokenList, info, url, type); - LogHelper.userInfo("IOS鐨勬帹閫佹棩蹇楋細" + IOS); - pushRecord.setIosPushId(IOS); - if (IOS != null) { - return 1; - } else { - return 4; - } - } + /** + * @param info + * @param params + * @param pushRecord + * @return 1:閮芥垚鍔� 2锛氫粎android 鎴愬姛 3.浠匢OS鎴愬姛 4.閮藉け璐� + * @throws Exception + */ + private static int pushApp(List<String> deviceTokenList, String PACKAGES, String title, String content, + System b_IOS, String url, PushRecord pushRecord, int type, String pwd) throws Exception { + MessageInfo info = new MessageInfo(); + info.setTitle(title); + info.setContent(content); + String IOS = allPushIOS(deviceTokenList, info, url, type, pwd); + LogHelper.userInfo("IOS鐨勬帹閫佹棩蹇楋細" + IOS); + pushRecord.setIosPushId(IOS); + if (IOS != null) { + return 1; + } else { + return 4; + } + } - /** - * 鎵ц鎺ㄩ�� - * - * @param tokenList - * @param json - * @throws Exception - */ - public static void executePushIOS(List<String> tokenList, JSONObject json) throws Exception { - InputStream certificate = IOSPushUtil.class.getClassLoader() - .getResourceAsStream("certificate/pushCertificate.p12"); // 璇诲彇.p12鏂囦欢 - String certificatePassword = Constant.systemCommonConfig.getIosPushCertificatePwd(); - pushIOS(tokenList, json, certificate, certificatePassword); - } + /** + * 鎵ц鎺ㄩ�� + * + * @param tokenList + * @param json + * @throws Exception + */ + public static void executePushIOS(List<String> tokenList, JSONObject json, String pwd) throws Exception { + InputStream certificate = IOSPushUtil.class.getClassLoader() + .getResourceAsStream("certificate/pushCertificate.p12"); // 璇诲彇.p12鏂囦欢 + String certificatePassword = pwd; + pushIOS(tokenList, json, certificate, certificatePassword); + } - /** - * - * 鏂规硶璇存槑: IOS 澶氭帹 - * - * @author mawurui createTime 2018骞�5鏈�8鏃� 涓婂崍10:19:57 - * @param deviceToken - * @param packages - * @param alert - * @return - * @throws Exception - */ - public static JSONObject getJSONObject(MessageInfo info, String url, int type) throws Exception { + /** + * 鏂规硶璇存槑: IOS 澶氭帹 + * + * @param deviceToken + * @param packages + * @param alert + * @return + * @throws Exception + * @author mawurui createTime 2018骞�5鏈�8鏃� 涓婂崍10:19:57 + */ + public static JSONObject getJSONObject(MessageInfo info, String url, int type) throws Exception { - // json鑷畾涔変紶鍊� - JSONObject json = null; - if (type == PushTypeEnum.goodsdetail.getCode()) { - Long auctionId = null; - if (url.contains("id=")) { - String[] sts = url.split("\\?")[1].split("&"); - for (String st : sts) { - if (st.contains("id=")) { - auctionId = Long.parseLong(st.replace("id=", "").trim()); - } - } - } - if (auctionId == null) - throw new Exception("娣樺疂鍟嗗搧ID鎻愬彇鍑洪敊"); - json = IOSPushFactory.createGoodsPush(auctionId, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.url.getCode()) { - String shortUrl = HttpUtil.getShortLink(url); - if (StringUtil.isNullOrEmpty(shortUrl)) - throw new Exception("鑾峰彇鐭摼鍑洪敊"); - json = IOSPushFactory.createURLPush(shortUrl, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.ZNX.getCode()) { - json = IOSPushFactory.createZNXPush(info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.weex.getCode()) { - String shortUrl = HttpUtil.getShortLink(url); - if (StringUtil.isNullOrEmpty(shortUrl)) - shortUrl = url; - json = IOSPushFactory.createWEEXPush(shortUrl, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.baichuan.getCode()) { - String shortUrl = HttpUtil.getShortLink(url); - if (StringUtil.isNullOrEmpty(shortUrl)) - shortUrl = url; - json = IOSPushFactory.createBaiChuanPush(shortUrl, info.getTitle(), info.getContent()); - } else if (type == PushTypeEnum.signin.getCode()) { - json = IOSPushFactory.createUserSignInPush(info.getTitle(), info.getContent()); - } + // json鑷畾涔変紶鍊� + JSONObject json = null; + if (type == PushTypeEnum.goodsdetail.getCode()) { + Long auctionId = null; + if (url.contains("id=")) { + String[] sts = url.split("\\?")[1].split("&"); + for (String st : sts) { + if (st.contains("id=")) { + auctionId = Long.parseLong(st.replace("id=", "").trim()); + } + } + } + if (auctionId == null) + throw new Exception("娣樺疂鍟嗗搧ID鎻愬彇鍑洪敊"); + json = IOSPushFactory.createGoodsPush(auctionId, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.url.getCode()) { + String shortUrl = HttpUtil.getShortLink(url); + if (StringUtil.isNullOrEmpty(shortUrl)) + throw new Exception("鑾峰彇鐭摼鍑洪敊"); + json = IOSPushFactory.createURLPush(shortUrl, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.ZNX.getCode()) { + json = IOSPushFactory.createZNXPush(info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.weex.getCode()) { + String shortUrl = HttpUtil.getShortLink(url); + if (StringUtil.isNullOrEmpty(shortUrl)) + shortUrl = url; + json = IOSPushFactory.createWEEXPush(shortUrl, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.baichuan.getCode()) { + String shortUrl = HttpUtil.getShortLink(url); + if (StringUtil.isNullOrEmpty(shortUrl)) + shortUrl = url; + json = IOSPushFactory.createBaiChuanPush(shortUrl, info.getTitle(), info.getContent()); + } else if (type == PushTypeEnum.signin.getCode()) { + json = IOSPushFactory.createUserSignInPush(info.getTitle(), info.getContent()); + } - return json; - } + return json; + } } -- Gitblit v1.8.0