package com.huimv.gtpush; import java.io.IOException; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport; import com.gexin.rp.sdk.base.IPushResult; import com.gexin.rp.sdk.base.impl.AppMessage; import com.gexin.rp.sdk.base.impl.ListMessage; import com.gexin.rp.sdk.base.impl.Target; import com.gexin.rp.sdk.base.uitls.AppConditions; import com.gexin.rp.sdk.http.IGtPush; import com.gexin.rp.sdk.template.TransmissionTemplate; import com.huimv.busi.xt.constant.XtAppConstant; import com.huimv.xx.dataobject.XxGttsjl; public class GtPush extends SqlMapClientDaoSupport{ //首先定义一些常量, 修改成开发者平台获得的值 private static String url = "http://sdk.open.api.igexin.com/serviceex"; // //appDemo // private static String appId = "ryrmlQRgev7B0patik5yM1"; // private static String appKey = "EBN73liO6N8L0ubna5JLc3"; // private static String masterSecret = "qRTTLUjkU77NH0lbjOIz98"; //haifm private static String appId = "WeAMRkV2md9kTcWM9qip83"; private static String appKey = "X8AEg5K4ed62pZz2FyHBE4"; private static String masterSecret = "FvTbm1WVGD8P9keD7op3KA"; private static IGtPush push; static { push = new IGtPush(url, appKey, masterSecret); } /** * 更新消息回执 * @param tsjlList */ public void GetPushAppMsgResult(List tsjlList) { try { if (tsjlList != null && tsjlList.size() > 0) { for (XxGttsjl xxGttsjl : tsjlList) { IPushResult pushResult = push.getPushResult(xxGttsjl.getTaskid()); Map map = pushResult.getResponse(); if (XtAppConstant.RESULT_OK.equals(map.get("result").toString())) { xxGttsjl.setMsgtotal((Integer) pushResult.getResponse().get("msgTotal")); // xxGttsjl.setClicknum((Integer) pushResult.getResponse().get("clickNum")); xxGttsjl.setMsgprocess((Integer) pushResult.getResponse().get("msgProcess")); getSqlMapClientTemplate().update("xx_gttsjl.ibatorgenerated_updateByPrimaryKeySelective", xxGttsjl); } } } } catch (Exception e) { e.printStackTrace(); } } /** * 群发消息根据标签 * @param hmGtDTO */ public void PushtoAll(HmGtDTO hmGtDTO) { //配置返回每个用户返回用户状态 System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true"); AppMessage message = new AppMessage(); TransmissionTemplate template = TransmissionTemplateDemo(hmGtDTO); message.setData(template); //设置消息离线,并设置离线时间 message.setOffline(hmGtDTO.isOffline()); //离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(hmGtDTO.getOfflineExpireTime()); //设置推送目标条件过滤 List appIdList = new ArrayList(); appIdList.add(appId); message.setAppIdList(appIdList); // List phoneTypeList = new ArrayList(); // List provinceList = new ArrayList(); List tagList = new ArrayList(); AppConditions cdt = new AppConditions(); // phoneTypeList.add("ANDROID"); // cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList); //手机型号过滤 // provinceList.add("浙江"); // cdt.addCondition(AppConditions.REGION, provinceList); //省份过滤 tagList.add(XtAppConstant.GT_ALIAS); cdt.addCondition(AppConditions.TAG,tagList); //标签过滤 message.setConditions(cdt); //message.setPushNetWorkType(1); //是否在wifi下推送 0-不限制 1-wifi //message.setSpeed(1000); //推送速度 每秒多少条 IPushResult ret = push.pushMessageToApp(message); Map map = ret.getResponse(); if (map != null && XtAppConstant.RESULT_OK.equals(map.get("result").toString())) { XxGttsjl xxGttsjl = new XxGttsjl(); if (hmGtDTO.getXxid() != null && !"".equals(hmGtDTO.getXxid())) { xxGttsjl.setXxid(Integer.parseInt(hmGtDTO.getXxid())); xxGttsjl.setTsfs(XtAppConstant.tsfs[2]); xxGttsjl.setFsxxmc(XtAppConstant.GT_ALIAS); xxGttsjl.setContent(hmGtDTO.getContent()); insert(ret, xxGttsjl); } } // System.out.println(ret.getResponse().toString()); } /** * 根据mcid透传消息 * @param mcid * @param content * @return */ public IPushResult PushtoListByMcid(int mcid, HmGtDTO hmGtDTO) { List aliasList = new ArrayList(); //别名只能绑定十个因此遍历 String alias = XtAppConstant.GT_ALIAS + "_" + mcid + "_"; int i = 1; if (setClientTag(alias + i) != null) { while (setClientTag(alias + i) != null) { aliasList.add(alias + i); i++; } if (XtAppConstant.ADMIN_MCID != mcid) { alias = XtAppConstant.GT_ALIAS + "_" + XtAppConstant.ADMIN_MCID + "_"; int j = 1; if (setClientTag(alias + j) != null) { while (setClientTag(alias + j) != null) { aliasList.add(alias + j); j++; } } } hmGtDTO.setAlias(aliasList); return PushtoList(hmGtDTO); } return null; } /** * 根据别名群体透传推送 * @param hmGtDTO 需要传入content、alias * @return */ public IPushResult PushtoList(HmGtDTO hmGtDTO) { //配置返回每个用户返回用户状态 System.setProperty("gexin.rp.sdk.pushlist.needDetails", "true"); //配置推送目标 List targets = new ArrayList(); List aliasList = hmGtDTO.getAlias(); String aliasStr = ""; if (aliasList != null && aliasList.size() > 0) { for (String alias : aliasList) { Target target = new Target(); target.setAppId(appId); target.setAlias(alias); targets.add(target); aliasStr += alias + ","; } } //获取taskID TransmissionTemplate template = TransmissionTemplateDemo(hmGtDTO); ListMessage message = new ListMessage(); message.setData(template); //设置消息离线,并设置离线时间 message.setOffline(hmGtDTO.isOffline()); //离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(hmGtDTO.getOfflineExpireTime()); String taskId = push.getContentId(message); //使用taskID对目标进行推送 IPushResult ret = push.pushMessageToList(taskId, targets); Map map = ret.getResponse(); if (map != null && XtAppConstant.RESULT_OK.equals(map.get("result").toString())) { XxGttsjl xxGttsjl = new XxGttsjl(); if (hmGtDTO.getXxid() != null && !"".equals(hmGtDTO.getXxid())) { xxGttsjl.setXxid(Integer.parseInt(hmGtDTO.getXxid())); xxGttsjl.setTsfs(XtAppConstant.tsfs[1]); xxGttsjl.setFsxxmc(aliasStr); xxGttsjl.setContent(hmGtDTO.getContent()); insert(ret, xxGttsjl); } } //打印服务器返回信息 // System.out.println(ret.getResponse().toString()); return ret; } /** * 插入 * @param ret * @param xxGttsjl */ private void insert(IPushResult ret,XxGttsjl xxGttsjl) { Map map = ret.getResponse(); if (map != null && XtAppConstant.RESULT_OK.equals(map.get("result").toString())) { xxGttsjl.setTaskid(map.get("contentId").toString()); String details = map.get("details").toString(); String detailsOn = details.replaceAll(XtAppConstant.SUCCESSED_ONLINE, ""); xxGttsjl.setZxyhtotal((details.length() - detailsOn.length()) / XtAppConstant.SUCCESSED_ONLINE.length()); String detailsOff = details.replaceAll(XtAppConstant.SUCCESSED_OFFLINE, ""); xxGttsjl.setLxyhtotal((details.length() - detailsOff.length()) / XtAppConstant.SUCCESSED_OFFLINE.length()); xxGttsjl.setFssj(new Date()); getSqlMapClientTemplate().insert("xx_gttsjl.ibatorgenerated_insert", xxGttsjl); } } /** * 透传消息模板 返回taskID * @return */ public static TransmissionTemplate TransmissionTemplateDemo(HmGtDTO hmGtDTO) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId); template.setAppkey(appKey); template.setTransmissionType(hmGtDTO.getMandatory()); template.setTransmissionContent(hmGtDTO.getContent()); return template; } /** * 根据别名获取绑定的所有CID * @param alias * @return */ public List setClientTag(String alias) { return push.queryClientId(appId, alias).getClientIdList(); } /** * 绑定别名 * @param targetList * @return */ public void bindAlias(String alias, String cid) { push.bindAlias(appId, alias, cid); } /** * 根据mcid 绑定别名 * @param mcid * @param cid */ public void bindAlias(int mcid, String cid) { //别名只能绑定十个因此遍历 String alias = XtAppConstant.GT_ALIAS + "_" + mcid + "_"; int i = 1; while (setClientTag(alias + i) != null && setClientTag(alias + i).size() > 10) { i++; } push.bindAlias(appId, alias + i, cid); } /** * 批量绑定别名 * @param targetList * @return */ public void bindAlias(List targetList) { push.bindAlias(appId, targetList); } /** * 获取用户绑定的标签 * @param clientId * @return */ public String getUserTags(String clientId) { IPushResult ir = push.getUserTags(appId, clientId); return ir.getResponse().get("tags").toString(); } /** * 根据CID绑定标签 * @param clientId * @param tags */ public void setClientTag(String clientId, List tags) { push.setClientTag(appId, clientId, tags); } public static void main(String[] args) throws IOException { // GtPush gp = new GtPush(); // gp.GetPushAppMsgResult("OSL-0317_YInravc5pv8G0gvauRGJ3"); // HmGtDTO hmGtDTO = new HmGtDTO(); // hmGtDTO.setContent("cccccccccccc"); // gp.PushtoAll(hmGtDTO); // // gp.PushtoListByMcid(5, "222"); // HmGtDTO hmGtDTO = new HmGtDTO(); // List alias = new ArrayList(); // alias.add("HUIMV_5_1"); // hmGtDTO.setAlias(alias); // hmGtDTO.setContent("别名测试"); // gp.PushtoList(hmGtDTO); // List targetList = new ArrayList(); // Target t1 = new Target(); // t1.setAppId(appId); // t1.setAlias("hzhm"); // t1.setClientId("b227b5dc54eb141ab87d84c6f0143109"); // targetList.add(t1); // // Target t2 = new Target(); // t2.setAppId(appId); // t2.setAlias("hzhm"); // t2.setClientId("33807e41ae225dcf77ccb408b92e904d"); // targetList.add(t2); // // gp.bindAlias(targetList); // // System.out.println(gp.setClientTag("hzhm").toString()); // List cIds = gp.setClientTag("huimv"); // for (String cid : cIds) { // System.out.println(cid); // } // // 新建一个IGtPush实例,传入调用接口网址,appkey和masterSecret // IGtPush push = new IGtPush(url, appKey, masterSecret); // push.connect(); // // 新建一个消息类型,根据app推送的话要使用AppMessage // AppMessage message = new AppMessage(); // // 新建一个推送模版, 以链接模板为例子,就是说在通知栏显示一条含图标、标题等的通知,用户点击可打开您指定的网页 // LinkTemplate template = new LinkTemplate(); // template.setAppId(appId); // template.setAppkey(appKey); // template.setTitle("欢迎使用个推!"); // template.setText("这是一条推送消息~"); // template.setUrl("http://getui.com"); // List appIds = new ArrayList(); // appIds.add(appId); // // 模板设置好后塞进message里并设置,同时设置推送的app id,还可以配置这条message是否支持离线,以及过期时间等 // message.setData(template); // message.setAppIdList(appIds); // message.setOffline(true); // message.setOfflineExpireTime(1000 * 600); // // 调用IGtPush实例的pushMessageToApp接口,参数就是上面我们配置的message // IPushResult ret = push.pushMessageToApp(message); // System.out.println(ret.getResponse().toString()); } }