GtPush.java 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. package com.huimv.gtpush;
  2. import java.io.IOException;
  3. import java.util.ArrayList;
  4. import java.util.Date;
  5. import java.util.List;
  6. import java.util.Map;
  7. import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;
  8. import com.gexin.rp.sdk.base.IPushResult;
  9. import com.gexin.rp.sdk.base.impl.AppMessage;
  10. import com.gexin.rp.sdk.base.impl.ListMessage;
  11. import com.gexin.rp.sdk.base.impl.Target;
  12. import com.gexin.rp.sdk.base.uitls.AppConditions;
  13. import com.gexin.rp.sdk.http.IGtPush;
  14. import com.gexin.rp.sdk.template.TransmissionTemplate;
  15. import com.huimv.busi.xt.constant.XtAppConstant;
  16. import com.huimv.xx.dataobject.XxGttsjl;
  17. public class GtPush extends SqlMapClientDaoSupport{
  18. //首先定义一些常量, 修改成开发者平台获得的值
  19. private static String url = "http://sdk.open.api.igexin.com/serviceex";
  20. // //appDemo
  21. // private static String appId = "ryrmlQRgev7B0patik5yM1";
  22. // private static String appKey = "EBN73liO6N8L0ubna5JLc3";
  23. // private static String masterSecret = "qRTTLUjkU77NH0lbjOIz98";
  24. //haifm
  25. private static String appId = "WeAMRkV2md9kTcWM9qip83";
  26. private static String appKey = "X8AEg5K4ed62pZz2FyHBE4";
  27. private static String masterSecret = "FvTbm1WVGD8P9keD7op3KA";
  28. private static IGtPush push;
  29. static {
  30. push = new IGtPush(url, appKey, masterSecret);
  31. }
  32. /**
  33. * 更新消息回执
  34. * @param tsjlList
  35. */
  36. public void GetPushAppMsgResult(List<XxGttsjl> tsjlList) {
  37. try {
  38. if (tsjlList != null && tsjlList.size() > 0) {
  39. for (XxGttsjl xxGttsjl : tsjlList) {
  40. IPushResult pushResult = push.getPushResult(xxGttsjl.getTaskid());
  41. Map<String, Object> map = pushResult.getResponse();
  42. if (XtAppConstant.RESULT_OK.equals(map.get("result").toString())) {
  43. xxGttsjl.setMsgtotal((Integer) pushResult.getResponse().get("msgTotal"));
  44. // xxGttsjl.setClicknum((Integer) pushResult.getResponse().get("clickNum"));
  45. xxGttsjl.setMsgprocess((Integer) pushResult.getResponse().get("msgProcess"));
  46. getSqlMapClientTemplate().update("xx_gttsjl.ibatorgenerated_updateByPrimaryKeySelective", xxGttsjl);
  47. }
  48. }
  49. }
  50. } catch (Exception e) {
  51. e.printStackTrace();
  52. }
  53. }
  54. /**
  55. * 群发消息根据标签
  56. * @param hmGtDTO
  57. */
  58. public void PushtoAll(HmGtDTO hmGtDTO) {
  59. //配置返回每个用户返回用户状态
  60. System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
  61. AppMessage message = new AppMessage();
  62. TransmissionTemplate template = TransmissionTemplateDemo(hmGtDTO);
  63. message.setData(template);
  64. //设置消息离线,并设置离线时间
  65. message.setOffline(hmGtDTO.isOffline());
  66. //离线有效时间,单位为毫秒,可选
  67. message.setOfflineExpireTime(hmGtDTO.getOfflineExpireTime());
  68. //设置推送目标条件过滤
  69. List<String> appIdList = new ArrayList<String>();
  70. appIdList.add(appId);
  71. message.setAppIdList(appIdList);
  72. // List<String> phoneTypeList = new ArrayList<String>();
  73. // List<String> provinceList = new ArrayList<String>();
  74. List<String> tagList = new ArrayList<String>();
  75. AppConditions cdt = new AppConditions();
  76. // phoneTypeList.add("ANDROID");
  77. // cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList); //手机型号过滤
  78. // provinceList.add("浙江");
  79. // cdt.addCondition(AppConditions.REGION, provinceList); //省份过滤
  80. tagList.add(XtAppConstant.GT_ALIAS);
  81. cdt.addCondition(AppConditions.TAG,tagList); //标签过滤
  82. message.setConditions(cdt);
  83. //message.setPushNetWorkType(1); //是否在wifi下推送 0-不限制 1-wifi
  84. //message.setSpeed(1000); //推送速度 每秒多少条
  85. IPushResult ret = push.pushMessageToApp(message);
  86. Map<String, Object> map = ret.getResponse();
  87. if (map != null && XtAppConstant.RESULT_OK.equals(map.get("result").toString())) {
  88. XxGttsjl xxGttsjl = new XxGttsjl();
  89. if (hmGtDTO.getXxid() != null && !"".equals(hmGtDTO.getXxid())) {
  90. xxGttsjl.setXxid(Integer.parseInt(hmGtDTO.getXxid()));
  91. xxGttsjl.setTsfs(XtAppConstant.tsfs[2]);
  92. xxGttsjl.setFsxxmc(XtAppConstant.GT_ALIAS);
  93. xxGttsjl.setContent(hmGtDTO.getContent());
  94. insert(ret, xxGttsjl);
  95. }
  96. }
  97. // System.out.println(ret.getResponse().toString());
  98. }
  99. /**
  100. * 根据mcid透传消息
  101. * @param mcid
  102. * @param content
  103. * @return
  104. */
  105. public IPushResult PushtoListByMcid(int mcid, HmGtDTO hmGtDTO) {
  106. List<String> aliasList = new ArrayList<String>();
  107. //别名只能绑定十个因此遍历
  108. String alias = XtAppConstant.GT_ALIAS + "_" + mcid + "_";
  109. int i = 1;
  110. if (setClientTag(alias + i) != null) {
  111. while (setClientTag(alias + i) != null) {
  112. aliasList.add(alias + i);
  113. i++;
  114. }
  115. if (XtAppConstant.ADMIN_MCID != mcid) {
  116. alias = XtAppConstant.GT_ALIAS + "_" + XtAppConstant.ADMIN_MCID + "_";
  117. int j = 1;
  118. if (setClientTag(alias + j) != null) {
  119. while (setClientTag(alias + j) != null) {
  120. aliasList.add(alias + j);
  121. j++;
  122. }
  123. }
  124. }
  125. hmGtDTO.setAlias(aliasList);
  126. return PushtoList(hmGtDTO);
  127. }
  128. return null;
  129. }
  130. /**
  131. * 根据别名群体透传推送
  132. * @param hmGtDTO 需要传入content、alias
  133. * @return
  134. */
  135. public IPushResult PushtoList(HmGtDTO hmGtDTO) {
  136. //配置返回每个用户返回用户状态
  137. System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true");
  138. //配置推送目标
  139. List<Target> targets = new ArrayList<Target>();
  140. List<String> aliasList = hmGtDTO.getAlias();
  141. String aliasStr = "";
  142. if (aliasList != null && aliasList.size() > 0) {
  143. for (String alias : aliasList) {
  144. Target target = new Target();
  145. target.setAppId(appId);
  146. target.setAlias(alias);
  147. targets.add(target);
  148. aliasStr += alias + ",";
  149. }
  150. }
  151. //获取taskID
  152. TransmissionTemplate template = TransmissionTemplateDemo(hmGtDTO);
  153. ListMessage message = new ListMessage();
  154. message.setData(template);
  155. //设置消息离线,并设置离线时间
  156. message.setOffline(hmGtDTO.isOffline());
  157. //离线有效时间,单位为毫秒,可选
  158. message.setOfflineExpireTime(hmGtDTO.getOfflineExpireTime());
  159. String taskId = push.getContentId(message);
  160. //使用taskID对目标进行推送
  161. IPushResult ret = push.pushMessageToList(taskId, targets);
  162. Map<String, Object> map = ret.getResponse();
  163. if (map != null && XtAppConstant.RESULT_OK.equals(map.get("result").toString())) {
  164. XxGttsjl xxGttsjl = new XxGttsjl();
  165. if (hmGtDTO.getXxid() != null && !"".equals(hmGtDTO.getXxid())) {
  166. xxGttsjl.setXxid(Integer.parseInt(hmGtDTO.getXxid()));
  167. xxGttsjl.setTsfs(XtAppConstant.tsfs[1]);
  168. xxGttsjl.setFsxxmc(aliasStr);
  169. xxGttsjl.setContent(hmGtDTO.getContent());
  170. insert(ret, xxGttsjl);
  171. }
  172. }
  173. //打印服务器返回信息
  174. // System.out.println(ret.getResponse().toString());
  175. return ret;
  176. }
  177. /**
  178. * 插入
  179. * @param ret
  180. * @param xxGttsjl
  181. */
  182. private void insert(IPushResult ret,XxGttsjl xxGttsjl) {
  183. Map<String, Object> map = ret.getResponse();
  184. if (map != null && XtAppConstant.RESULT_OK.equals(map.get("result").toString())) {
  185. xxGttsjl.setTaskid(map.get("contentId").toString());
  186. String details = map.get("details").toString();
  187. String detailsOn = details.replaceAll(XtAppConstant.SUCCESSED_ONLINE, "");
  188. xxGttsjl.setZxyhtotal((details.length() - detailsOn.length()) / XtAppConstant.SUCCESSED_ONLINE.length());
  189. String detailsOff = details.replaceAll(XtAppConstant.SUCCESSED_OFFLINE, "");
  190. xxGttsjl.setLxyhtotal((details.length() - detailsOff.length()) / XtAppConstant.SUCCESSED_OFFLINE.length());
  191. xxGttsjl.setFssj(new Date());
  192. getSqlMapClientTemplate().insert("xx_gttsjl.ibatorgenerated_insert", xxGttsjl);
  193. }
  194. }
  195. /**
  196. * 透传消息模板 返回taskID
  197. * @return
  198. */
  199. public static TransmissionTemplate TransmissionTemplateDemo(HmGtDTO hmGtDTO) {
  200. TransmissionTemplate template = new TransmissionTemplate();
  201. template.setAppId(appId);
  202. template.setAppkey(appKey);
  203. template.setTransmissionType(hmGtDTO.getMandatory());
  204. template.setTransmissionContent(hmGtDTO.getContent());
  205. return template;
  206. }
  207. /**
  208. * 根据别名获取绑定的所有CID
  209. * @param alias
  210. * @return
  211. */
  212. public List<String> setClientTag(String alias) {
  213. return push.queryClientId(appId, alias).getClientIdList();
  214. }
  215. /**
  216. * 绑定别名
  217. * @param targetList
  218. * @return
  219. */
  220. public void bindAlias(String alias, String cid) {
  221. push.bindAlias(appId, alias, cid);
  222. }
  223. /**
  224. * 根据mcid 绑定别名
  225. * @param mcid
  226. * @param cid
  227. */
  228. public void bindAlias(int mcid, String cid) {
  229. //别名只能绑定十个因此遍历
  230. String alias = XtAppConstant.GT_ALIAS + "_" + mcid + "_";
  231. int i = 1;
  232. while (setClientTag(alias + i) != null && setClientTag(alias + i).size() > 10) {
  233. i++;
  234. }
  235. push.bindAlias(appId, alias + i, cid);
  236. }
  237. /**
  238. * 批量绑定别名
  239. * @param targetList
  240. * @return
  241. */
  242. public void bindAlias(List<Target> targetList) {
  243. push.bindAlias(appId, targetList);
  244. }
  245. /**
  246. * 获取用户绑定的标签
  247. * @param clientId
  248. * @return
  249. */
  250. public String getUserTags(String clientId) {
  251. IPushResult ir = push.getUserTags(appId, clientId);
  252. return ir.getResponse().get("tags").toString();
  253. }
  254. /**
  255. * 根据CID绑定标签
  256. * @param clientId
  257. * @param tags
  258. */
  259. public void setClientTag(String clientId, List<String> tags) {
  260. push.setClientTag(appId, clientId, tags);
  261. }
  262. public static void main(String[] args) throws IOException {
  263. // GtPush gp = new GtPush();
  264. // gp.GetPushAppMsgResult("OSL-0317_YInravc5pv8G0gvauRGJ3");
  265. // HmGtDTO hmGtDTO = new HmGtDTO();
  266. // hmGtDTO.setContent("cccccccccccc");
  267. // gp.PushtoAll(hmGtDTO);
  268. //
  269. // gp.PushtoListByMcid(5, "222");
  270. // HmGtDTO hmGtDTO = new HmGtDTO();
  271. // List<String> alias = new ArrayList<String>();
  272. // alias.add("HUIMV_5_1");
  273. // hmGtDTO.setAlias(alias);
  274. // hmGtDTO.setContent("别名测试");
  275. // gp.PushtoList(hmGtDTO);
  276. // List<Target> targetList = new ArrayList<Target>();
  277. // Target t1 = new Target();
  278. // t1.setAppId(appId);
  279. // t1.setAlias("hzhm");
  280. // t1.setClientId("b227b5dc54eb141ab87d84c6f0143109");
  281. // targetList.add(t1);
  282. //
  283. // Target t2 = new Target();
  284. // t2.setAppId(appId);
  285. // t2.setAlias("hzhm");
  286. // t2.setClientId("33807e41ae225dcf77ccb408b92e904d");
  287. // targetList.add(t2);
  288. //
  289. // gp.bindAlias(targetList);
  290. //
  291. // System.out.println(gp.setClientTag("hzhm").toString());
  292. // List<String> cIds = gp.setClientTag("huimv");
  293. // for (String cid : cIds) {
  294. // System.out.println(cid);
  295. // }
  296. // // 新建一个IGtPush实例,传入调用接口网址,appkey和masterSecret
  297. // IGtPush push = new IGtPush(url, appKey, masterSecret);
  298. // push.connect();
  299. // // 新建一个消息类型,根据app推送的话要使用AppMessage
  300. // AppMessage message = new AppMessage();
  301. // // 新建一个推送模版, 以链接模板为例子,就是说在通知栏显示一条含图标、标题等的通知,用户点击可打开您指定的网页
  302. // LinkTemplate template = new LinkTemplate();
  303. // template.setAppId(appId);
  304. // template.setAppkey(appKey);
  305. // template.setTitle("欢迎使用个推!");
  306. // template.setText("这是一条推送消息~");
  307. // template.setUrl("http://getui.com");
  308. // List<String> appIds = new ArrayList<String>();
  309. // appIds.add(appId);
  310. // // 模板设置好后塞进message里并设置,同时设置推送的app id,还可以配置这条message是否支持离线,以及过期时间等
  311. // message.setData(template);
  312. // message.setAppIdList(appIds);
  313. // message.setOffline(true);
  314. // message.setOfflineExpireTime(1000 * 600);
  315. // // 调用IGtPush实例的pushMessageToApp接口,参数就是上面我们配置的message
  316. // IPushResult ret = push.pushMessageToApp(message);
  317. // System.out.println(ret.getResponse().toString());
  318. }
  319. }