个推-推送hello world

最近项目中的一个百度推送真是把我搞的有点头大,真的是很垃圾,到达率又低,还特么遇上停止维护了。。。

所以项目决定转用别的推送平台,现在改用个推,官方文档写的很好,除了刚下载下来,折腾了一阵子,不过很快也就运行起来了。

这这里也就简单的记录一下。

具体可以参考:http://docs.getui.com/getui/server/java/start/

第一步:注册

第二步:获取凭证

第三步:安装demo到手机,这一步安装到手机后,就能获取手机对应那个app的cID了,以后就对着这个CID进行推送

第四部:导入jar包,这里说一下,服务端的给的sdk下载下来,如果包导入不进来的话,就需要手动导入了。

然后一切没问题可以运行代码。

注意,这时候用的是第三步下载的测试APP,所以代码里面的参数要设置为测试的。

然后上代码:

package com.getui.java.https;

import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
public class HttpsDemo {
	
	//测试APP
	
	public static final String appId = "看后台配置";
	public static final String appKey = "看后台配置";
	public static final String masterSecret = "看后台配置";   
	public static final String CID = "从第三步下载的app看clientid";
	
	private static String host = "http://sdk.open.api.igexin.com/apiex.htm";
    //static String host = "https://api.getui.com/apiex.htm";
    public static void main(String[] args) throws Exception {
        // https连接
        IGtPush push = new IGtPush(appKey, masterSecret, true);
        // 此处true为https域名,false为http,默认为false。Java语言推荐使用此方式
        // IGtPush push = new IGtPush(host, appkey, master);
        // host为域名,根据域名区分是http协议/https协议
        LinkTemplate template = linkTemplateDemo();
        SingleMessage message = new SingleMessage();
        message.setOffline(true);
        // 离线有效时间,单位为毫秒,可选
        message.setOfflineExpireTime(24 * 3600 * 1000);
        message.setData(template);
        message.setPushNetWorkType(0); // 可选,判断是否客户端是否wifi环境下推送,1为在WIFI环境下,0为不限制网络环境。
        Target target = new Target();
        target.setAppId(appId);
        target.setClientId(CID);
        // 用户别名推送,cid和用户别名只能2者选其一
        // String alias = "个";
        // target.setAlias(alias);
        IPushResult ret = null;
        try {
            ret = push.pushMessageToSingle(message, target);
        } catch (RequestException e) {
            e.printStackTrace();
            ret = push.pushMessageToSingle(message, target, e.getRequestId());
        }
        if (ret != null) {
            System.out.println(ret.getResponse().toString());
        } else {
            System.out.println("服务器响应异常");
        }
    }
    public static LinkTemplate linkTemplateDemo() {
        LinkTemplate template = new LinkTemplate();
        // 设置APPID与APPKEY
        template.setAppId(appId);
        template.setAppkey(appKey);
        // 设置通知栏标题与内容
        template.setTitle("hello world");
        template.setText("今天的天气是真的好啊");
        // 配置通知栏图标
        template.setLogo("icon.png");
        // 配置通知栏网络图标,填写图标URL地址
        template.setLogoUrl("");
        // 设置通知是否响铃,震动,或者可清除
        template.setIsRing(true);
        template.setIsVibrate(true);
        template.setIsClearable(true);
        // 设置打开的网址地址
        template.setUrl("http://www.baidu.com");
        return template;
    }
}

 到这里,应该是可以完美输出的。

猜你喜欢

转载自www.cnblogs.com/sunxun/p/9089771.html
今日推荐