使用个推实现消息推送到客户端

时间:2025-03-19 11:10:26

由于公司业务需要,也得学习一下个推的使用,在此记录一下。

1、使用之前,首先的到个推官网注册账号:

2、注册完之后登录进去,之后你会获得

appId 、appKey 、masterSecret

快速入门:

private static String appId = "";
private static String appKey = "";
private static String masterSecret = "";
private static String url = "/";

IGtPush push = new IGtPush(url, appKey, masterSecret);
// 定义"点击链接打开通知模板",并设置标题、内容、链接
LinkTemplate template = new LinkTemplate();
(appId);
(appKey);
("欢迎使用个推!");
("这是一条推送消息~");
("");
List<String> appIds = new ArrayList<String>();
(appId);
// 定义"AppMessage"类型消息对象,设置消息内容模板、发送的目标App列表
// 、是否支持离线发送、以及离线消息有效期(单位毫秒)
AppMessage message = new AppMessage();
(template);
(appIds);
(true);
(1000 * 600);
IPushResult ret = (message);
(().toString());

这样你就实现了一个简单的推送。

maven 项目实现个推推送消息

配置maven 项目的文件

<!-- getui -->
        <dependency>
            <groupId></groupId>
            <artifactId>gexin-rp-fastjson</artifactId>
            <version>1.0.0.1</version>
          </dependency>
        <dependency>
            <groupId></groupId>
            <artifactId>gexin-rp-sdk-http</artifactId>
            <version>4.0.1.17</version>
          </dependency>
          <dependency>
            <groupId></groupId>
            <artifactId>gexin-rp-sdk-template</artifactId>
            <version>4.0.0.16</version>
          </dependency>
          <dependency>
            <groupId></groupId>
            <artifactId>gexin-rp-sdk-base</artifactId>
            <version>4.0.0.22</version>
          </dependency>
          <dependency>
            <groupId></groupId>
            <artifactId>protobuf-java</artifactId>
            <version>2.5.0</version>
        </dependency>

本地仓库若是没有上述jar包,可以使用命令让jar包配到仓库里面,cmd命令。   mvn install:install-file -DgroupId= -DartifactId=alipay-sdk -Dversion=20171026141113 -Dpackaging=jar -Dfile=,把相应的参数替换一下就可以了。

这里我提供一个个推工具类:

 

import ;
import ;

import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import .Style0;

public class GetuiUtil {

    static String appId = "";
    static String appKey = "";
    static String masterSecret = "";
    static String host = "/";
    static IGtPush push;
    static {
        push = new IGtPush(host, appKey, masterSecret);
    }

    /**
     * 绑定用户cid 别名
     * 
     * @param Alias
     * @param CID
     * @return
     */
    public static boolean bindAlias(String alias, String CID) {
        IAliasResult bindSCid = (appId, alias, CID);
        if (()) {
            return true;
        }
        return false;
    }

    /**
     * 对单个用户推送消息
     * @param alias
     * @param msg
     * @return
     */
    public static boolean pushMessageToSingle(String cid, String text, String transMsg) {
        IGtPush push = new IGtPush(host, appKey, masterSecret);
        NotificationTemplate template = notificationTemplate("title", text, transMsg);
        SingleMessage message = new SingleMessage();
        (true);
        // 离线有效时间,单位为毫秒,可选
        (24 * 3600 * 1000);
        (template);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        (0);
        Target target = new Target();
        (appId);
        (cid);
        //(Alias);
        IPushResult ret = null;
        try {
            ret = (message, target);
        } catch (RequestException e) {
            ();
            ret = (message, target, ());
        }
        if (ret != null && () != null && ().containsKey("result")) {
            (().toString());
            if(().get("result").toString().equals("ok") && ().containsKey("status")){
                return true;
            }
        } 
        return false;
    }
    
    /**
     * 指定应用的所有用户群发推送消息
     * @param msg
     */
    public static boolean pushtoAPP (String text, String transMsg){
        IGtPush push = new IGtPush(host, appKey, masterSecret);

        NotificationTemplate template = notificationTemplate("title", text, transMsg);
        AppMessage message = new AppMessage();
        (template);

        (true);
        //离线有效时间,单位为毫秒,可选
        (24 * 1000 * 3600);
        //推送给App的目标用户需要满足的条件
        AppConditions cdt = new AppConditions();
        List<String> appIdList = new ArrayList<String>();
        (appId);
        (appIdList);
        //手机类型
        List<String> phoneTypeList = new ArrayList<String>();
        //省份
        List<String> provinceList = new ArrayList<String>();
        //自定义tag
        List<String> tagList = new ArrayList<String>();

        (AppConditions.PHONE_TYPE, phoneTypeList);
        (, provinceList);
        (,tagList);
        (cdt);

        IPushResult ret = (message,"msg_toApp");
        
        if (ret != null && () != null && ().containsKey("result")) {
            (().toString());
            if(().get("result").toString().equals("ok")){
                return true;
            }
        } 
        return false;
    }
    
    /**
     * 对单个用户推送透传消息
     * 
     * @param alias
     * @param title
     * @param content
     * @return
     */
    public static boolean pushTransMessageToSingle(String cid, String msg) {
        TransmissionTemplate template = transTemplate(cid, msg);
        SingleMessage message = new SingleMessage();
        (true);
        // 离线有效时间,单位为毫秒,可选
        (24 * 3600 * 1000);
        (template);
        // 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
        (0);
        Target target = new Target();
        (appId);
        (cid);
        IPushResult ret = null;
        try {
            ret = (message, target);
        } catch (RequestException e) {
            ();
            ret = (message, target, ());
        }
        if (ret != null && () != null && ().containsKey("result")) {
            (().toString());
            if(().get("result").toString().equals("ok") && ().containsKey("status")){
                return true;
            }
        } 
        return false;
    }

    public static void pushMessageToIBatch(List<String> alias, String msg) {
        IBatch batch = ();
        IPushResult ret = null;
        try {
            for (int i = 0; i < (); i++) {
                // 构建客户a的透传消息a
                constructClientTransMsg((i), msg, batch);
            }
            // 构建客户B的点击通知打开网页消息b
            // constructClientLinkMsg(CID_B,"msgB",batch);
            ret = ();
        } catch (Exception e) {
            ();
        }
        (().toString());
    }

    private static NotificationTemplate notificationTemplate(String title, String text, String obj) {
        NotificationTemplate template = new NotificationTemplate();
        // 设置APPID与APPKEY
        (appId);
        (appKey);
        // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
        (1);
        (obj);
        // 设置定时展示时间
        // ("2015-01-16 11:40:00", "2015-01-16 12:24:00");

        Style0 style = new Style0();
        // 设置通知栏标题与内容
        (title);
        (text);
        // 配置通知栏图标
        ("XXX");
        // 配置通知栏网络图标
        //("");
        // 设置通知是否响铃,震动,或者可清除
        (true);
        (true);
        (true);
        (style);

        return template;
    }
    
    private static TransmissionTemplate transTemplate(String cid, String msg) {
        TransmissionTemplate template = new TransmissionTemplate();
        // 设置APPID与APPKEY

        (appId);
        (appKey);
        (msg);
        (0); // 这个Type为int型,填写1则自动启动app

        return template;
    }

    private static void constructClientTransMsg(String alias, String msg, IBatch batch) throws Exception {

        SingleMessage message = new SingleMessage();
        TransmissionTemplate template = new TransmissionTemplate();
        (appId);
        (appKey);
        (msg);
        (0); // 这个Type为int型,填写1则自动启动app

        (0);
        (template);
        (true);
        (24 * 3600 * 1000);

        // 设置推送目标,填入appid和alias
        Target target = new Target();
        (appId);
        (alias);
        (message, target);
    }

}
然后在你的服务端调用工具类里面相应的方法传上相应参数的值就可以了。这里只记录服务端的。如有bug,欢迎指正~