PushUtils.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package com.szwl.model.utils;
  2. import com.gexin.rp.sdk.base.impl.SingleMessage;
  3. import com.gexin.rp.sdk.base.impl.Target;
  4. import com.gexin.rp.sdk.base.payload.APNPayload;
  5. import com.gexin.rp.sdk.exceptions.RequestException;
  6. import com.gexin.rp.sdk.http.IGtPush;
  7. import com.gexin.rp.sdk.template.TransmissionTemplate;
  8. import org.json.JSONObject;
  9. import java.util.List;
  10. /**
  11. * Created by dinfeng on 2017/12/21
  12. * 个推工具类
  13. */
  14. public class PushUtils {
  15. public final static String appId = "GKa6qa12heALjEXZlAn1U3";
  16. public final static String appKey = "fLvPjR8hni7VFMkgjh8lx2";
  17. public final static String masterSecret = "KjxrC6vTLr5wiZu55cCnS8";
  18. public final static String host = "http://sdk.open.api.igexin.com/apiex.htm";
  19. // public static IPushResult rets =null;
  20. public static String test_clientId = "c69869085580f3b77f2972403fbdd5b3";
  21. public static String test_clientId2 = "200cb24ae7af3c4096cc2c46569ed530";
  22. final static IGtPush push = new IGtPush(host, appKey, masterSecret);
  23. // private static EquipmentService equipmentService;
  24. //
  25. // @Autowired
  26. // public PushUtils(EquipmentService equipmentService) {
  27. // PushUtils.equipmentService = equipmentService;
  28. // }
  29. // private static RabbitTemplate rabbitTemplate;
  30. //
  31. // @Autowired
  32. // public PushUtils(RabbitTemplate rabbitTemplate) {
  33. // PushUtils.rabbitTemplate = rabbitTemplate;
  34. // }
  35. /**
  36. * 个推发送信息
  37. *
  38. * @param clientIds 设备号数组
  39. * @param title
  40. * @param information 信息
  41. * @param json 业务id
  42. */
  43. public static void push(List<String> clientIds, String title, String information, String json) {
  44. TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
  45. //推送ios
  46. final SingleMessage iMessage = new SingleMessage();
  47. iMessage.setOffline(true);
  48. // 离线有效时间,单位为毫秒,可选
  49. iMessage.setOfflineExpireTime(24 * 3600 * 1000);
  50. iMessage.setData(transmissionTemplate);
  51. // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
  52. iMessage.setPushNetWorkType(0);
  53. //个推
  54. for (String clientId : clientIds) {
  55. final Target target = new Target();
  56. target.setAppId(appId);
  57. target.setClientId(clientId);
  58. try {
  59. new Thread(new Runnable() {
  60. // IPushResult ret = null;
  61. @Override
  62. public void run() {
  63. // ret = push.pushMessageToSingle(iMessage, target);
  64. push.pushMessageToSingle(iMessage, target);
  65. // rets = ret;
  66. }
  67. }).start();
  68. } catch (RequestException e) {
  69. e.printStackTrace();
  70. push.pushMessageToSingle(iMessage, target, e.getRequestId());
  71. }
  72. }
  73. }
  74. /**
  75. * 个推发送信息
  76. *
  77. * @param clientId 设备号数组
  78. * @param information 信息
  79. * @param json 业务id
  80. */
  81. public static void push(String clientId, String title, String information, String json) {
  82. // String type = "1";
  83. // if(StringUtils.isNotEmpty(title)){
  84. // type = title;
  85. // }
  86. //// List<Filter> filters = new ArrayList<>();
  87. //// filters.add(Filter.eq("gtClientId", clientId));
  88. //// List<Equipment> list = equipmentService.findList(null, filters, null);
  89. //// if(list.size()>0){
  90. //// for(Equipment equipment:list){
  91. //// String channel = equipment.getChannel();
  92. //// String equimentType = equipment.getEquimentType();
  93. //// String client = equipment.getClientId();
  94. //// String substring = client.substring(client.length() - 1);
  95. //// if(substring.equals("x")){
  96. //// break;
  97. //// }
  98. //// if(StringUtils.isEmpty(channel)||channel.equals("1")||StringUtils.isEmpty(equimentType)){
  99. //// //用个推
  100. //// type = "1";
  101. //// }
  102. //// if(channel.equals("2")||StringUtils.isNotEmpty(equimentType)){
  103. //// //用mq
  104. //// type = "2";
  105. //// //1 创建消息
  106. //// MessageProperties messageProperties = new MessageProperties();
  107. //// messageProperties.setContentType("text/plain");
  108. //// //设置消息的过期时间,60秒
  109. ////// messageProperties.setExpiration("60000");
  110. //// Message message = new Message(json.getBytes(), messageProperties);
  111. ////
  112. //// rabbitTemplate.send(equimentType, client, message);
  113. //// }
  114. //// }
  115. //// }
  116. // if(type.equals("2")){
  117. // String[] split = information.split(":");
  118. // String equimentType = split[0];
  119. // String client = split[1];
  120. // //1 创建消息
  121. // MessageProperties messageProperties = new MessageProperties();
  122. // messageProperties.setContentType("text/plain");
  123. // //设置消息的过期时间,60秒
  124. //// messageProperties.setExpiration("60000");
  125. // Message message = new Message(json.getBytes(), messageProperties);
  126. // rabbitTemplate.convertAndSend(equimentType, client, message);
  127. //// rabbitTemplate.send(equimentType, client, message);
  128. // }
  129. //
  130. // if(type.equals("1")){
  131. // TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
  132. // //推送ios
  133. // final SingleMessage iMessage = new SingleMessage();
  134. // iMessage.setOffline(true);
  135. // // 离线有效时间,单位为毫秒,可选
  136. // iMessage.setOfflineExpireTime(24 * 3600 * 1000);
  137. // iMessage.setData(transmissionTemplate);
  138. // // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
  139. // iMessage.setPushNetWorkType(0);
  140. //
  141. //
  142. // //个推
  143. // final Target target = new Target();
  144. // target.setAppId(appId);
  145. // target.setClientId(clientId);
  146. // try {
  147. //
  148. // new Thread(new Runnable() {
  149. //
  150. // @Override
  151. // public void run() {
  152. // push.pushMessageToSingle(iMessage, target);
  153. // }
  154. //
  155. //
  156. // }).start();
  157. //
  158. // } catch (RequestException e) {
  159. // e.printStackTrace();
  160. // push.pushMessageToSingle(iMessage, target, e.getRequestId());
  161. // }
  162. // }
  163. TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
  164. //推送ios
  165. final SingleMessage iMessage = new SingleMessage();
  166. iMessage.setOffline(true);
  167. // 离线有效时间,单位为毫秒,可选
  168. iMessage.setOfflineExpireTime(24 * 3600 * 1000);
  169. iMessage.setData(transmissionTemplate);
  170. // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
  171. iMessage.setPushNetWorkType(0);
  172. //个推
  173. final Target target = new Target();
  174. target.setAppId(appId);
  175. target.setClientId(clientId);
  176. try {
  177. new Thread(new Runnable() {
  178. @Override
  179. public void run() {
  180. push.pushMessageToSingle(iMessage, target);
  181. }
  182. }).start();
  183. } catch (RequestException e) {
  184. e.printStackTrace();
  185. push.pushMessageToSingle(iMessage, target, e.getRequestId());
  186. }
  187. }
  188. /**
  189. * 苹果使用透传消息,通过设置payload信息,实现待机时的推送提醒;
  190. * 对安卓来说就是没有payload的概念,所以需要去解析透传的消息,把title和body放在透传消息中
  191. */
  192. private static TransmissionTemplate getTemplate(String title, String msg, String json) {
  193. title = title == null ? "" : title;
  194. msg = msg == null ? "" : msg;
  195. TransmissionTemplate template = new TransmissionTemplate();
  196. template.setAppId(appId);
  197. template.setAppkey(appKey);
  198. template.setTransmissionContent(json);
  199. template.setTransmissionType(2);
  200. APNPayload payload = new APNPayload();
  201. //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
  202. payload.setAutoBadge("+1");
  203. payload.setContentAvailable(0);
  204. payload.setSound("default");
  205. payload.setCategory("$由客户端定义");
  206. payload.addCustomMsg("payload",json);//好假
  207. //字典模式使用APNPayload.DictionaryAlertMsg
  208. //payload.setAlertMsg(getDictionaryAlertMsg(msg, title));
  209. template.setAPNInfo(payload);
  210. return template;
  211. }
  212. private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String msg, String title) {
  213. APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
  214. alertMsg.setBody(msg);//通知小内容
  215. alertMsg.setActionLocKey("ActionLockey");
  216. alertMsg.setLocKey("LocKey");
  217. alertMsg.addLocArg("loc-args");
  218. alertMsg.setLaunchImage("launch-image");
  219. // iOS8.2以上版本支持
  220. alertMsg.setTitle(title);//通知标题
  221. alertMsg.setTitleLocKey("");
  222. alertMsg.addTitleLocArg("");
  223. return alertMsg;
  224. }
  225. public static JSONObject buildJson(String kind, String kindData) {
  226. JSONObject jsonObject = new JSONObject();
  227. if (kind != null) {
  228. jsonObject.put("kind", kind);
  229. jsonObject.put("kind_data", kindData);
  230. }else{
  231. jsonObject.put("kind", "");
  232. jsonObject.put("kind_data", "");
  233. }
  234. return jsonObject;
  235. }
  236. public static JSONObject buildJson(String kind, String kindData,String netTime,String webTime) {
  237. JSONObject jsonObject = new JSONObject();
  238. if (kind != null) {
  239. jsonObject.put("kind", kind);
  240. jsonObject.put("kind_data", kindData);
  241. jsonObject.put("netTime", netTime);
  242. jsonObject.put("webTime", webTime);
  243. }else{
  244. jsonObject.put("kind", "");
  245. jsonObject.put("kind_data", "");
  246. jsonObject.put("netTime", "");
  247. jsonObject.put("webTime", "");
  248. }
  249. return jsonObject;
  250. }
  251. // public static void main(String[] args) {
  252. // push("71963777bd63e0f0a5b6490f04bf1ae4", "niai", "123", buildJson("editNo", "123456789").toString());
  253. // }
  254. }