先去下载jar包,然后集成到开发工具中,
链接:https://pan.baidu.com/s/14Cq_gNGkeoU_qstgHGIL-A
提取码:48h9
然后创建单个个推测试消息类
package com.crm.pushMessage;
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;
import com.gexin.rp.sdk.template.style.Style0;
/*****
* 该类用于个推单发消息
* @author daju
*/
public class pushMsgMain {
//注册时候的参数,需要更改的去app上看,进行更改
private static String appId ="***********";
private static String appKey ="************";
private static String masterSecret ="**************";
//别名推送方式
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
//主函数 入口
public static void main(String[] args) {
String title="中秋快乐";
String content="国庆快乐";
String CID="3de98bb05cbe6fdfc61482a4565cb69c";
String pushResult=commonPushMsg(CID,title,content);
if("pushSuccess".equals(pushResult)){
System.out.print("推送消息成功!!");
}
}
/*****
* 该方法是调用群发的公共类,那里需要就那里调用
* @param CID 需要发送给谁,,,也就是那个用户的应用id
* @param title 标题
* @param content 文本,内容
* @return String 返回的结果
* @throws Exception
* @author daju
*/
public static String commonPushMsg(String CID,String title,String content) {
String pushResult="pushFail";
IGtPush push = new IGtPush(host, appKey, masterSecret);
LinkTemplate template = linkTemplateDemo(title,content);
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) {
if("ok".equals(ret.getResponse().get("result"))){
pushResult="pushSuccess";
}
}else {
System.out.println("服务器响应异常");
}
return pushResult;
}
/*****
* 该方法是设置群发消息的数据设置
* @param title 群发的标题
* @param content 群发的文本,内容
* @return LinkTemplate 返回设置后的需要发送的对象
* @throws Exception
* @author daju
*/
public static LinkTemplate linkTemplateDemo(String title,String content) {
LinkTemplate template = new LinkTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle(title);
style.setText(content);
// 配置通知栏图标
style.setLogo("icon.png");
// 配置通知栏网络图标
style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
//设置打开的网址地址
template.setUrl("http://www.baidu.com");
return template;
}
}
群发消息
package com.crm.pushMessage;
import java.util.ArrayList;
import java.util.List;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.LinkTemplate;
import com.gexin.rp.sdk.template.style.Style0;
/*****
* 该类用于个推群发消息
* @author daju
*/
public class pushAllMsgMain {
//注册时候的参数,需要更改的去app上看,进行更改
private static String appId ="**********";
private static String appKey ="**********";
private static String masterSecret ="**********";
//别名推送方式
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
//主函数 入口
public static void main(String[] args) {
String title="现在是群推消息:::::中秋快乐";
String content="国庆快乐";
String pushResult=commonAllPushMsg(title,content);
if("pushSuccess".equals(pushResult)){
System.out.print("群推送消息成功!!");
}
}
/*****
* 该方法是调用群发的公共类,那里需要就那里调用
* @param title 群发的标题
* @param content 群发的文本,内容
* @return String 返回群发的结果
* @throws Exception
* @author daju
*/
public static String commonAllPushMsg(String title,String content){
String pushResult="pushFail";
IGtPush push = new IGtPush(host, appKey, masterSecret);
LinkTemplate template = null;
try {
template = linkTemplateDemo(title,content);
} catch (Exception e) {
e.printStackTrace();
}
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,"群发消息");
if("ok".equals(ret.getResponse().get("result"))){
pushResult="pushSuccess";
}
return pushResult;
}
/*****
* 该方法是设置群发消息的数据设置
* @param title 群发的标题
* @param content 群发的文本,内容
* @return LinkTemplate 返回设置后的需要发送的对象
* @throws Exception
* @author daju
*/
public static LinkTemplate linkTemplateDemo(String title,String content) throws Exception {
LinkTemplate template = new LinkTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle(title);
style.setText(content);
// 配置通知栏图标
style.setLogo("icon.png");
// 配置通知栏网络图标
style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
template.setUrl("http://www.baidu.com");
return template;
}
}