个推服务端对接

2019-04-15  本文已影响0人  YAOAORAN

个推开发者中心地址

服务端大致开发步骤

1.注册开发者账号;

2.创建应用;

3.查看应用配置, 包含AppID,AppSecret,AppKey,MasterSecret等信息;

4.引入官方依赖,进行服务端开发;

仓库

<repositories>
        <repository>
            <id>getui-nexus</id>
            <url>http://mvn.gt.igexin.com/nexus/content/repositories/releases/</url>
        </repository>
    </repositories>

依赖

<dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-http</artifactId>
            <version>4.0.1.17</version>
        </dependency>

        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-base</artifactId>
            <version>4.0.0.22</version>
        </dependency>

        <dependency>
            <groupId>com.gexin.platform</groupId>
            <artifactId>gexin-rp-sdk-template</artifactId>
            <version>4.0.0.16</version>
        </dependency>

数据透传推送工具类(引用自一篇博客,原文无从查找,侵删)

public class AppPushUtils {

    //定义常量, appId、appKey、masterSecret  在"个推控制台"中获得的应用配置
    // 由IGetui管理页面生成,是您的应用与SDK通信的标识之一,每个应用都对应一个唯一的AppID
    private static String appId = "";
    // 预先分配的第三方应用对应的Key,是您的应用与SDK通信的标识之一。
    private static String appKey = "";
    // 个推服务端API鉴权码,用于验证调用方合法性。在调用个推服务端API时需要提供。(请妥善保管,避免通道被盗用)
    private static String masterSecret = "";

    // 设置通知消息模板
    /*
     * 1. appId
     * 2. appKey
     * 3. 要传送到客户端的 msg
     * 3.1 标题栏:key = title,
     * 3.2 通知栏内容: key = titleText,
     * 3.3 穿透内容:key = command
     */
    private static TransmissionTemplate getNotifacationTemplate(String appId, String appKey, Map<String, String> msg) {
        // 在通知栏显示一条含图标、标题等的通知,用户点击后激活您的应用
        TransmissionTemplate template = new TransmissionTemplate();
        template.setAppId(appId);
        template.setAppkey(appKey);
        template.setTransmissionContent(msg.get("transText"));
        // 设置appid,appkey
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 穿透消息设置为,1 强制启动应用
        template.setTransmissionType(1);
        // 设置穿透内容
        return template;
    }


    /**
     * 对单个用户推送消息
     *
     * @param cid 用户的clientId
     * @param msg 透传的参数
     * @return
     */
    public static IPushResult pushMsgToSingle(String cid, Map<String, String> msg) {
        // 代表在个推注册的一个 app,调用该类实例的方法来执行对个推的请求
        IGtPush push = new IGtPush(appKey, masterSecret);
        // 创建信息模板
        TransmissionTemplate template = getNotifacationTemplate(appId, appKey, msg);
        //定义消息推送方式为,单推
        SingleMessage message = new SingleMessage();
        // 设置推送消息的内容
        message.setData(template);
        //设置过期时间
        message.setOfflineExpireTime(24 * 3600 * 1000);
        //不限制网络环境
        message.setPushNetWorkType(0);
        // 设置推送目标
        Target target = new Target();
        target.setAppId(appId);
        // 设置cid
        target.setClientId(cid);
        // 获得推送结果
        IPushResult result = push.pushMessageToSingle(message, target);
        /*
         * 1. 失败:{result=sign_error}
         * 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
         * 3. 异常
         */
        return result;
    }

    /**
     * 对多个用户推送透传消息
     *
     * @param cids
     * @param msg
     * @return
     */
    public static IPushResult pushMsgToList(List<String> cids, Map<String, String> msg) {
        // 代表在个推注册的一个 app,调用该类实例的方法来执行对个推的请求
        IGtPush push = new IGtPush(appKey, masterSecret);
        // 创建信息模板
        TransmissionTemplate template = getNotifacationTemplate(appId, appKey, msg);
        //定义消息推送方式为,多推
        ListMessage message = new ListMessage();
        // 设置推送消息的内容
        message.setData(template);
        //获取ContentId
        String contentId = push.getContentId(message);
        // 设置推送目标
        List<Target> targetList = new ArrayList<>();
        Target target;
        for (String cid : cids) {
            target = new Target();
            target.setAppId(appId);
            target.setClientId(cid);
            targetList.add(target);
        }
        // 获得推送结果
        IPushResult result = push.pushMessageToList(contentId, targetList);
        /*
         * 1. 失败:{result=sign_error}
         * 2. 成功:{result=ok, taskId=OSS-0212_1b7578259b74972b2bba556bb12a9f9a, status=successed_online}
         * 3. 异常
         */
        return result;
    }

    /**
     * 判断是否推送成功
     *
     * @param result
     * @return
     */
    public static boolean isPushSuccess(IPushResult result) {
        if (result.getResponse() != null && result.getResponse().get("result").toString().equals("ok")) {
            return true;
        }
        return false;
    }
上一篇下一篇

猜你喜欢

热点阅读