个推java服务端集成详解!

示例


 

全局参数,实际中替换为自己的

public static final String AppID = "";
public static final String AppSecret = "";
public static final String AppKey = "";
public static final String MasterSecret = "";
public static final String HOST = "http://sdk.open.api.igexin.com/apiex.htm";
第一个推送方式,也是最常用性能最高的推送,单推:

/**
*
* @param cidOrAlias
* 别名或者cid
* @param msg
* 透传消息内容
* @param type
* 1-cid推,2-别名推
*/
public static void pushToSingle(String cidOrAlias, String msg, int type) {
IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
ITemplate template = buildTransmissionTemplate(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);
if (type == 1) {
target.setClientId(cidOrAlias);
} else if (type == 2) {
// 按别名推送
target.setAlias(cidOrAlias);
}
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("服务器响应异常");
}
}
 上面有详细的注释,有几个需要注意几个地方。

推送的消息类型,支持各种模板,有通知栏透传模板,通知栏点击跳转网页模板,透传模板等,下面列举示例

1、通知栏点击跳转网页模板:

public static LinkTemplate buildLinkTemplate() {
LinkTemplate template = new LinkTemplate();
// 设置APPID与APPKEY
template.setAppId(AppID);
template.setAppkey(AppKey);

Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle("请输入通知栏标题");
style.setText("请输入通知栏内容");
// 配置通知栏图标
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;
}
通过这种方式推送,手机上会收到一条通知栏消息,点击后会打开指定网页。

2、通知栏透传模板

public static NotificationTemplate buildNotificationTemplate() {
NotificationTemplate template = new NotificationTemplate();
// 设置APPID与APPKEY
template.setAppId(AppID);
template.setAppkey(AppKey);

Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle("群推通知栏标题");
style.setText("群推通知栏内容");
// 配置通知栏图标
style.setLogo("icon.png");
// 配置通知栏网络图标
style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);

// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
template.setTransmissionType(2);
template.setTransmissionContent("请输入您要透传的内容");
return template;
}
通过这种方式,手机上会收到一条通知栏消息,并且带有透传消息。

3、纯透传模板:

public static ITemplate buildTransmissionTemplate(String msg) {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(AppID);
template.setAppkey(AppKey);
template.setTransmissionContent(msg);
template.setTransmissionType(1); // 这个Type为int型,填写1则自动启动app
return template;
}
客户端集成SDK设置监听后,会收到透传消息,客户端可以自己灵活的选择处理方式。

上面还提到按别名推送,那么别名是怎么来的呢

public static void bindAlias(String cid, String alias) {
IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
IAliasResult bindSCid = push.bindAlias(AppID, alias, cid);
System.out.println("绑定结果:" + bindSCid.getResult() + "错误码:" + bindSCid.getErrorMsg());
}
这是绑定别名的方法,这样通过绑定别名可以和自己的业务数据绑定进行推送。单推就介绍到这里。

第二种推送方式:批量推送:

public static void pushToList(List<String> cids, String msg) {
// 配置返回每个用户返回用户状态,可选
System.setProperty("gexin_pushList_needDetails", "true");
// 配置返回每个别名及其对应cid的用户状态,可选
// System.setProperty("gexin_pushList_needAliasDetails", "true");
IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
// 透传模板
ITemplate template = buildTransmissionTemplate(msg);
ListMessage message = new ListMessage();
message.setData(template);
// 设置消息离线,并设置离线时间
message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 1000 * 3600);
// 配置推送目标
List<Target> targets = new ArrayList<Target>();
Target target = null;
for (String cid : cids) {
target = new Target();
target.setAppId(AppID);
target.setClientId(cid);
targets.add(target);
// target.setAlias(Alias1);
}
// taskId用于在推送时去查找对应的message
String taskId = push.getContentId(message, "任务别名_toApp");
// String taskId = push.getContentId(message);
IPushResult ret = push.pushMessageToList(taskId, targets);
System.out.println(ret.getResponse().toString());
}
这里实际上就是,对一批指定cliendid的用户进行推送,也支持上面的几种模板,参数也可以传别名集合进来。

第三种推送方式,按各种筛选条件进行群推:

public static void pushToApp(String msg, List<String> tagList) throws Exception {
IGtPush push = new IGtPush(HOST, AppKey, AppSecret);

// 使用通知栏链接模板
ITemplate template = buildLinkTemplate();
AppMessage message = new AppMessage();
message.setData(template);

message.setOffline(true);
// 离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 1000 * 3600);
// 可选,1为wifi,0为不限制网络环境。根据手机处于的网络情况,决定是否下发
message.setPushNetWorkType(0);
// 全量推送个推控制下发速度在100条/秒,只有toApp支持定速推送。
// message.setSpeed(100);
// 设置指定时间推送
// message.setPushTime("201903271756");

List<String> appIdList = new ArrayList<String>();
appIdList.add(AppID);
message.setAppIdList(appIdList);

// 推送给App的目标用户需要满足的条件
AppConditions cdt = new AppConditions();

// 手机类型
List<String> phoneTypeList = new ArrayList<String>();
phoneTypeList.add("ANDROID");
phoneTypeList.add("IOS");

// 省份
List<String> provinceList = new ArrayList<String>();
// 50000000代表重庆市
provinceList.add("50000000");

// 设置手机类型筛选
cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
// 设置省份筛选
cdt.addCondition(AppConditions.REGION, provinceList);
// 设置tag筛选
cdt.addCondition(AppConditions.TAG, tagList);

// 交并补
// cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList,
// OptType.or);
// cdt.addCondition(AppConditions.REGION, provinceList, OptType.or);
// cdt.addCondition(AppConditions.TAG, tagList, OptType.or);
message.setConditions(cdt);

IPushResult ret = push.pushMessageToApp(message, "任务别名_toApp");
System.out.println(ret.getResponse().toString());
}
几个注意事项:

省份和城市编码,请参考官方文档

定速推送:旨在解决个推群推系统在全量推送时速度过快,导致部分客户服务器连接压力过大的问题。提供接口设置让用户按自身情况控制推送速度,如果未设置则按默认推送速度发送。

定时推送:对单个指定应用的所有用户群发推送消息。该消息可以在用户设定的时间点进行推送。此接口需要开通。

交并补设置:应用群推对于复杂的查询条件新增加的交并补功能,以对应查询语义中的与或非的关系

场景:需要发送给城市在A,B,C里面,没有设置tagtest标签,手机型号为android的用户,用条件交并补功能可以实现,city(A|B|C) && !tag(tagtest) && phonetype(andriod)
条件类型(OptType.or 或, OptType.and 与, OptType.not 非)
tag列表:上面有提到tag,tag就是给用户打的标签,可以实现按标签推送,tag的设置方式如下:

public static void setTag(String cid, List<String> tagList) {
IGtPush push = new IGtPush(HOST, AppKey, MasterSecret);
IQueryResult ret = push.setClientTag(AppID, cid, tagList);
System.out.println(ret.getResponse().toString());
}
通过设置标签,所有终端都可以设置一个或者多个标签,进行标签推送时就会实现筛选作用。

群推就介绍到这里。

单推还有一种扩展形式,批量单推,用于一次创建提交多个单推任务。当单推任务较多时,推荐使用该接口,可以减少与服务端的交互次public static void pushBatch(String cid, String content) throws Exception IIGtPush push = new IGtPush(HOST, AppKey, MasterSecret);

IBatch batch = push.getBatch();
try {
// 构建透传消息
constructClientTransMsg(cid, content, batch);
// 构建点击通知打开网页消息
constructClientLinkMsg(cid, content, batch);
} catch (Exception e) {
e.printStackTrace();
}
batch.submit();
}
private static void constructClientTransMsg(String cid, String msg, IBatch batch) throws Exception {
SingleMessage message = new SingleMessage();
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(AppID);
template.setAppkey(AppKey);
template.setTransmissionContent(msg);
// 这个Type为int型,填写1则自动启动app
template.setTransmissionType(1);

message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(1 * 1000);

// 设置推送目标,填入appid和clientId
Target target = new Target();
target.setAppId(AppID);
target.setClientId(cid);
batch.add(message, target);
}
private static void constructClientLinkMsg(String cid, String msg, IBatch batch) throws Exception {
SingleMessage message = new SingleMessage();
LinkTemplate template = new LinkTemplate();
template.setAppId(AppID);
template.setAppkey(AppKey);
template.setTitle("title");
template.setText("msg");
template.setLogo("push.png");
template.setLogoUrl("logoUrl");
template.setUrl("url");

message.setData(template);
message.setOffline(true);
message.setOfflineExpireTime(1 * 1000);

// 设置推送目标,填入appid和clientId
Target target = new Target();
target.setAppId(AppID);
target.setClientId(cid);
batch.add(message, target);
}
这样就可以实现一次服务端交互,完成多个单推任务,cid也可以是不同的,给不同的终端进行推送,推送内容也可以不同,这里为了简便就取的一样的。
---------------------
原文:https://blog.csdn.net/u010142437/article/details/88908161

posted @ 2019-08-05 15:45  真理不真  阅读(3505)  评论(0编辑  收藏  举报