PushUtils.java 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package com.shawn.util;
  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 String test_clientId = "c69869085580f3b77f2972403fbdd5b3";
  20. public static String test_clientId2 = "200cb24ae7af3c4096cc2c46569ed530";
  21. final static IGtPush push = new IGtPush(host, appKey, masterSecret);
  22. /**
  23. * 个推发送信息
  24. *
  25. * @param clientIds 设备号数组
  26. * @param title
  27. * @param information 信息
  28. * @param json 业务id
  29. */
  30. public static void push(List<String> clientIds, String title, String information, String json) {
  31. TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
  32. //推送ios
  33. final SingleMessage iMessage = new SingleMessage();
  34. iMessage.setOffline(true);
  35. // 离线有效时间,单位为毫秒,可选
  36. iMessage.setOfflineExpireTime(24 * 3600 * 1000);
  37. iMessage.setData(transmissionTemplate);
  38. // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
  39. iMessage.setPushNetWorkType(0);
  40. //个推
  41. for (String clientId : clientIds) {
  42. final Target target = new Target();
  43. target.setAppId(appId);
  44. target.setClientId(clientId);
  45. try {
  46. new Thread(new Runnable() {
  47. @Override
  48. public void run() {
  49. push.pushMessageToSingle(iMessage, target);
  50. }
  51. }).start();
  52. } catch (RequestException e) {
  53. e.printStackTrace();
  54. push.pushMessageToSingle(iMessage, target, e.getRequestId());
  55. }
  56. }
  57. }
  58. /**
  59. * 个推发送信息
  60. *
  61. * @param clientId 设备号数组
  62. * @param information 信息
  63. * @param json 业务id
  64. */
  65. public static void push(String clientId, String title, String information, String json) {
  66. TransmissionTemplate transmissionTemplate = getTemplate(title, information, json);
  67. //推送ios
  68. final SingleMessage iMessage = new SingleMessage();
  69. iMessage.setOffline(true);
  70. // 离线有效时间,单位为毫秒,可选
  71. // iMessage.setOfflineExpireTime(24 * 3600 * 1000);
  72. iMessage.setOfflineExpireTime(5* 1000);
  73. iMessage.setData(transmissionTemplate);
  74. // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
  75. iMessage.setPushNetWorkType(0);
  76. //个推
  77. final Target target = new Target();
  78. target.setAppId(appId);
  79. target.setClientId(clientId);
  80. try {
  81. new Thread(new Runnable() {
  82. @Override
  83. public void run() {
  84. push.pushMessageToSingle(iMessage, target);
  85. }
  86. }).start();
  87. } catch (RequestException e) {
  88. e.printStackTrace();
  89. push.pushMessageToSingle(iMessage, target, e.getRequestId());
  90. }
  91. }
  92. /**
  93. * 苹果使用透传消息,通过设置payload信息,实现待机时的推送提醒;
  94. * 对安卓来说就是没有payload的概念,所以需要去解析透传的消息,把title和body放在透传消息中
  95. */
  96. private static TransmissionTemplate getTemplate(String title, String msg, String json) {
  97. title = title == null ? "" : title;
  98. msg = msg == null ? "" : msg;
  99. TransmissionTemplate template = new TransmissionTemplate();
  100. template.setAppId(appId);
  101. template.setAppkey(appKey);
  102. template.setTransmissionContent(json);
  103. template.setTransmissionType(2);
  104. APNPayload payload = new APNPayload();
  105. //在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
  106. payload.setAutoBadge("+1");
  107. payload.setContentAvailable(0);
  108. payload.setSound("default");
  109. payload.setCategory("$由客户端定义");
  110. payload.addCustomMsg("payload",json);//好假
  111. //字典模式使用APNPayload.DictionaryAlertMsg
  112. //payload.setAlertMsg(getDictionaryAlertMsg(msg, title));
  113. template.setAPNInfo(payload);
  114. return template;
  115. }
  116. private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(String msg, String title) {
  117. APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
  118. alertMsg.setBody(msg);//通知小内容
  119. alertMsg.setActionLocKey("ActionLockey");
  120. alertMsg.setLocKey("LocKey");
  121. alertMsg.addLocArg("loc-args");
  122. alertMsg.setLaunchImage("launch-image");
  123. // iOS8.2以上版本支持
  124. alertMsg.setTitle(title);//通知标题
  125. alertMsg.setTitleLocKey("");
  126. alertMsg.addTitleLocArg("");
  127. return alertMsg;
  128. }
  129. public static JSONObject buildJson(String kind, String kindData) {
  130. JSONObject jsonObject = new JSONObject();
  131. if (kind != null) {
  132. jsonObject.put("kind", kind);
  133. jsonObject.put("kind_data", kindData);
  134. }else{
  135. jsonObject.put("kind", "");
  136. jsonObject.put("kind_data", "");
  137. }
  138. return jsonObject;
  139. }
  140. public static JSONObject buildJson(String kind, String kindData,String netTime,String webTime) {
  141. JSONObject jsonObject = new JSONObject();
  142. if (kind != null) {
  143. jsonObject.put("kind", kind);
  144. jsonObject.put("kind_data", kindData);
  145. jsonObject.put("netTime", netTime);
  146. jsonObject.put("webTime", webTime);
  147. }else{
  148. jsonObject.put("kind", "");
  149. jsonObject.put("kind_data", "");
  150. jsonObject.put("netTime", "");
  151. jsonObject.put("webTime", "");
  152. }
  153. return jsonObject;
  154. }
  155. // public static void main(String[] args) {
  156. // push("71963777bd63e0f0a5b6490f04bf1ae4", "niai", "123", buildJson("editNo", "123456789").toString());
  157. // }
  158. }