package com.yeshi.fanli.util.push; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.annotation.PostConstruct; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.google.gson.Gson; import com.yeshi.fanli.controller.admin.PushController; import com.yeshi.fanli.entity.system.System; import com.yeshi.fanli.entity.xinge.MessageInfo; import com.yeshi.fanli.entity.xinge.PushRecord; import com.yeshi.fanli.log.LogHelper; import com.yeshi.fanli.log.PushLogHelper; import com.yeshi.fanli.service.inter.config.SystemService; import com.yeshi.fanli.util.Constant; import com.yeshi.fanli.util.StringUtil; import com.yeshi.fanli.util.factory.IOSPushFactory; import org.yeshi.utils.HttpUtil; import javapns.devices.Device; import javapns.devices.implementations.basic.BasicDevice; import javapns.notification.AppleNotificationServerBasicImpl; import javapns.notification.PushNotificationManager; import javapns.notification.PushNotificationPayload; import javapns.notification.PushedNotification; import net.sf.json.JSONObject; @Component public class IOSPushUtil { private static IOSPushUtil iosPushUtil; @Autowired private SystemService systemService; @PostConstruct public void init() { iosPushUtil = this; iosPushUtil.systemService = this.systemService; } /** * * 方法说明: IOS 多推 * * @author mawurui createTime 2018年5月8日 上午10:19:57 * @param deviceToken * @param packages * @param alert * @return * @throws Exception */ public static String allPushIOS(List deviceTokenList, MessageInfo info, String url, int type) throws Exception { Gson gson = new Gson(); PushLogHelper.iosInfo("IOS推送设备数为:" + deviceTokenList.size()); PushLogHelper.iosInfo("IOS推送的内容为:" + gson.toJson(info)); List tokenList = new ArrayList(); for (String deviceToken : deviceTokenList) { tokenList.add(deviceToken); } // json自定义传值 JSONObject json = null; if (type == PushController.GOODS) { 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 == PushController.URL) { String shortUrl = HttpUtil.getShortLink(url); if (StringUtil.isNullOrEmpty(shortUrl)) throw new Exception("获取短链出错"); json = IOSPushFactory.createURLPush(shortUrl, info.getTitle(), info.getContent()); } else if (type == PushController.ZNX) { json = IOSPushFactory.createZNXPush(info.getTitle(), info.getContent()); } else if (type == PushController.WEEX) { String shortUrl = HttpUtil.getShortLink(url); if (StringUtil.isNullOrEmpty(shortUrl)) throw new Exception("获取短链出错"); json = IOSPushFactory.createWEEXPush(shortUrl, info.getTitle(), info.getContent()); } else if (type == PushController.BAICHUAN) { String shortUrl = HttpUtil.getShortLink(url); if (StringUtil.isNullOrEmpty(shortUrl)) throw new Exception("获取短链出错"); json = IOSPushFactory.createBaiChuanPush(shortUrl, info.getTitle(), info.getContent()); } // 分组推送 每50个设备为一组 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); } } return null; } private static String pushIOS(List deviceTokenList, JSONObject json) throws Exception { List tokenList = new ArrayList(); for (String deviceToken : deviceTokenList) { tokenList.add(deviceToken); } InputStream certificate = IOSPushUtil.class.getClassLoader() .getResourceAsStream("certificate/pushCertificate.p12"); // 读取.p12文件 String certificatePassword = Constant.systemCommonConfig.getIosPushCertificatePwd();// 此处注意导出的证书密码不能为空因为空密码会报错 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()); PushNotificationManager pushManager = new PushNotificationManager(); // true:表示的是产品线上发布推送服务 false:表示的是产品测试推送服务 if (Constant.IS_TEST) pushManager.initializeConnection( new AppleNotificationServerBasicImpl(certificate, certificatePassword, false)); else pushManager .initializeConnection(new AppleNotificationServerBasicImpl(certificate, certificatePassword, true)); List deviceList = new ArrayList(); for (String token : tokenList) { if (!StringUtil.isNullOrEmpty(token)) deviceList.add(new BasicDevice(token)); } // 开始推送 List 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 deviceTokenList, String PACKAGES, String title, String content, String url, PushRecord pushRecord, int type) throws Exception { return pushApp(deviceTokenList, PACKAGES, title, content, b_IOS, url, pushRecord, type); } /** * * @param info * @param params * @param pushRecord * @return 1:都成功 2:仅android 成功 3.仅IOS成功 4.都失败 * @throws Exception */ private static int pushApp(List 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 tokenList * @param json * @throws Exception */ public static void executePushIOS(List tokenList, JSONObject json) throws Exception { pushIOS(tokenList, json); } /** * * 方法说明: 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 { // json自定义传值 JSONObject json = null; if (type == PushController.GOODS) { 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 == PushController.URL) { String shortUrl = HttpUtil.getShortLink(url); if (StringUtil.isNullOrEmpty(shortUrl)) throw new Exception("获取短链出错"); json = IOSPushFactory.createURLPush(shortUrl, info.getTitle(), info.getContent()); } else if (type == PushController.ZNX) { json = IOSPushFactory.createZNXPush(info.getTitle(), info.getContent()); } else if (type == PushController.WEEX) { String shortUrl = HttpUtil.getShortLink(url); if (StringUtil.isNullOrEmpty(shortUrl)) shortUrl = url; json = IOSPushFactory.createWEEXPush(shortUrl, info.getTitle(), info.getContent()); } else if (type == PushController.BAICHUAN) { String shortUrl = HttpUtil.getShortLink(url); if (StringUtil.isNullOrEmpty(shortUrl)) shortUrl = url; json = IOSPushFactory.createBaiChuanPush(shortUrl, info.getTitle(), info.getContent()); } return json; } }