123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346 |
- 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<XxGttsjl> tsjlList) {
- try {
- if (tsjlList != null && tsjlList.size() > 0) {
- for (XxGttsjl xxGttsjl : tsjlList) {
- IPushResult pushResult = push.getPushResult(xxGttsjl.getTaskid());
- Map<String, Object> 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<String> appIdList = new ArrayList<String>();
- appIdList.add(appId);
- message.setAppIdList(appIdList);
-
- // List<String> phoneTypeList = new ArrayList<String>();
- // List<String> provinceList = new ArrayList<String>();
- List<String> tagList = new ArrayList<String>();
-
- 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<String, Object> 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<String> aliasList = new ArrayList<String>();
- //别名只能绑定十个因此遍历
- 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<Target> targets = new ArrayList<Target>();
- List<String> 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<String, Object> 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<String, Object> 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<String> 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<Target> 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<String> 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<String> alias = new ArrayList<String>();
- // alias.add("HUIMV_5_1");
- // hmGtDTO.setAlias(alias);
- // hmGtDTO.setContent("别名测试");
- // gp.PushtoList(hmGtDTO);
-
- // List<Target> targetList = new ArrayList<Target>();
- // 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<String> 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<String> appIds = new ArrayList<String>();
- // 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());
- }
- }
|