消息推送

消息推送

以下是几种消息推送的方法:
AppID:应用的唯一标识:由IGetui管理页面生成,是您的应用与SDK通信的标识之一,每个应用都对应一个唯一的AppID
AppKey:公匙(相当于账号):预先分配的第三方应用对应的Key,是您的应用与SDK通信的标识之一。
masterSecret:私匙(相当于密码):个推服务端API鉴权码,用于验证调用方合法性。在调用个推服务端API时需要提供
以上几种id百度一下获取流程!
获取cid代码

    mui.init()  
    mui.plusReady(function() {  
        alert(plus.push.getClientInfo().clientid);  
package com.kwy.main.Util;

import java.util.ArrayList;
import java.util.List;
import com.gexin.rp.sdk.base.IAliasResult;
import com.gexin.rp.sdk.base.IBatch;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
public class GetuiUtil {
    //AppID:应用的唯一标识:由IGetui管理页面生成,是您的应用与SDK通信的标识之一,每个应用都对应一个唯一的AppID
    //AppKey:公匙(相当于账号):预先分配的第三方应用对应的Key,是您的应用与SDK通信的标识之一。
    //masterSecret:私匙(相当于密码):个推服务端API鉴权码,用于验证调用方合法性。在调用个推服务端API时需要提供
//    AppID: mm2SECp5fN9UlI6GvKeWu2
//    AppKey: AM8Tc8hXiy9POqhhXrfCfA
//    AppSecret: rmlF0YcR2B6HLJAwFzLL02
//    MasterSecret: OJYnNXoH1JA0sQ3foakdy7
    static String appId = "";
    static String appKey = "";
    static String masterSecret = "";
    static String host = "http://sdk.open.api.igexin.com/apiex.htm";//个推服务地址
    static IGtPush push;
    static {
        // 此处true为https域名,false为http,默认为false。Java语言推荐使用此方式
        // IGtPush push = new IGtPush(host, appkey, master);
        // host为域名,根据域名区分是http协议/https协议
        push = new IGtPush(host, appKey, masterSecret);
    }
    /**
     * 绑定用户cid 别名: CID 个推业务层中的对外用户标识,用于标识客户端身份,由第三方客户端获取并保存到第三方服务端,是个推SDK的唯一识别号,简称CID。
     *
     * @param
     * @param CID
     * @return
     */
    public static boolean bindAlias(String alias, String CID) {
        IAliasResult bindSCid = push.bindAlias(appId, alias, CID);
        if (bindSCid.getResult()) {
            return true;
        }
        return false;
    }
    /**
     * 对单个用户推送消息
     * @param
     * @param
     * @return
     */
//    cid获取方法:
//         cid = PushManager.getInstance().getClientid(this);plus.push.getClientInfo().clientid 这个就是获取cid的方法
    public  boolean pushMessageToSingle(String cid, String text, String transMsg) {
        //title:标题 transMsg:内容
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        //获取透传模板
       // TransmissionTemplate template = getTemplate(appId, appKey,obj);
        NotificationTemplate template = notificationTemplate("title", text, transMsg);
        SingleMessage message = new SingleMessage();
        //是否支持离线推送(true:支持,false:不支持)
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        //根据CID推送(CID和用户别名只能2者选其一)
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        //别名推送
        // String alias = "张三";
        //target.setAlias(Alias);
        IPushResult ret = null;
        try {
            //业务场景:一般适用于指定某个特定用户的推送,比如你要告诉某个用户他租的域名再不续费就要到期啦,或者有个好友@你啦,这种和独立用户紧密相关的个人消息。
            // STEP4:执行推送
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
            System.out.println(ret.getResponse().toString());
            if(ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")){
                return true;
            }
        }
        return false;
    }

    /**
     * 指定应用的所有用户群发推送消息
     * @param
     */
    public  boolean pushtoAPP (String text, String transMsg){
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        NotificationTemplate template = notificationTemplate("title", text, transMsg);
        AppMessage message = new AppMessage();
        message.setData(template);
        message.setOffline(true);
        //离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 1000 * 3600);
        //推送给App的目标用户需要满足的条件
        AppConditions cdt = new AppConditions();
        List<String> appIdList = new ArrayList<String>();
        appIdList.add(appId);
        message.setAppIdList(appIdList);
        //手机类型
        List<String> phoneTypeList = new ArrayList<String>();
        //省份
        List<String> provinceList = new ArrayList<String>();
        //自定义tag
        List<String> tagList = new ArrayList<String>();
        cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
        cdt.addCondition(AppConditions.REGION, provinceList);
        cdt.addCondition(AppConditions.TAG,tagList);
        message.setConditions(cdt);
        IPushResult ret = push.pushMessageToApp(message,"msg_toApp");

        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
            System.out.println(ret.getResponse().toString());
            if(ret.getResponse().get("result").toString().equals("ok")){
                return true;
            }
        }
        return false;
    }

    /**
     * 对单个用户推送透传消息
     *
     * @param
     * @param
     * @param
     * @return
     */
    public  boolean pushTransMessageToSingle(String cid, String msg) {
        TransmissionTemplate template = transTemplate(cid, msg);
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        message.setPushNetWorkType(0);
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(cid);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null && ret.getResponse() != null && ret.getResponse().containsKey("result")) {
            System.out.println(ret.getResponse().toString());
            if(ret.getResponse().get("result").toString().equals("ok") && ret.getResponse().containsKey("status")){
                return true;
            }
        }
        return false;
    }
    public  void pushMessageToIBatch(List<String> alias, String msg) {
        IBatch batch = push.getBatch();
        IPushResult ret = null;
        try {
            for (int i = 0; i < alias.size(); i++) {
                // 构建客户a的透传消息a
                constructClientTransMsg(alias.get(i), msg, batch);
            }
            // 构建客户B的点击通知打开网页消息b
            // constructClientLinkMsg(CID_B,"msgB",batch);
            ret = batch.submit();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(ret.getResponse().toString());
    }
    private  NotificationTemplate notificationTemplate(String title, String text, String obj) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        template.setTransmissionType(1);
        template.setTransmissionContent(obj);
        // 设置定时展示时间
        // template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00")
        Style0 style = new Style0();
        // 设置通知栏标题与内容
        style.setTitle(title);
        style.setText(text);
        // 配置通知栏图标
        style.setLogo("XXX");
        // 配置通知栏网络图标
        //style.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        style.setRing(true);
        style.setVibrate(true);
        style.setClearable(true);
        template.setStyle(style);
        return template;
    }

    private  TransmissionTemplate transTemplate(String cid, String msg) {
        TransmissionTemplate template = new TransmissionTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(msg);
        template.setTransmissionType(0); // 这个Type为int型,填写1则自动启动app
        return template;
    }
    private  void constructClientTransMsg(String alias, String msg, IBatch batch) throws Exception {
        SingleMessage message = new SingleMessage();
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(msg);
        template.setTransmissionType(0); // 这个Type为int型,填写1则自动启动app
        message.setPushNetWorkType(0);
        message.setData(template);
        message.setOffline(true);
        message.setOfflineExpireTime(24 * 3600 * 1000);
        // 设置推送目标,填入appid和alias
        Target target = new Target();
        target.setAppId(appId);
        target.setAlias(alias);
        batch.add(message, target);
    }
}![Alt](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hdmF0YXIuY3Nkbi5uZXQvNy83L0IvMV9yYWxmX2h4MTYzY29tLmpwZw =30x30)

居中的图片: ![Alt](https://imgconvert.csdnimg.cn/aHR0cHM6Ly9hdmF0YXIuY3Nkbi5uZXQvNy83L0IvMV9yYWxmX2h4MTYzY29tLmpwZw#pic_center)

以选择导入功能进行对应扩展名的文件导入,
继续你的创作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值