申请之后,我们开发人员需要拿到的是
1.在pom文件中引入个推依赖
com.gexin.platform
gexin-rp-sdk-template
4.0.0.16
com.gexin.platform
gexin-rp-sdk-http
4.0.1.17
2.个推可以使用cid对个别用户推送,也可以把cid绑定别名(客户端工作),用别名进行推送,我这里使用别名进行推送
package com.dingyi.common.util;
import com.alibaba.fastjson.JSONObject;
import com.dingyi.common.Message.PushMessage;
import com.dingyi.common.base.CommonConstant;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.AppMessage;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.base.uitls.AppConditions;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.gexin.rp.sdk.template.TransmissionTemplate;
import com.gexin.rp.sdk.template.style.Style0;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import java.util.ArrayList;
import java.util.List;
@Component
public class Getui2Util {
private static Logger logger = LoggerFactory.getLogger(Getui2Util.class);
private static String appId = "GI*************VqHPT4";
private static String appKey = "7m*************iRvbMaA";
private static String masterSecret = "CZ*************HMGWeW2";
static String host = "http://sdk.open.api.igexin.com/apiex.htm";
/**
* 推送给特定ios/安卓用户 注意:不是直接通知,需要客户端处理
*
* @param pushMessage
* @param alias
*/
public void pushMessageToOne(PushMessage pushMessage, String alias) {
IGtPush push = new IGtPush(host, appKey, masterSecret);
TransmissionTemplate template = getTemplate(pushMessage);
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.toString()); //使用cid来推送
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("服务器响应异常");
}
}
/**
* 推送给特定安卓用户 注意:ios接收不到
*
* @param pushMessage
* @param alias
*/
public void pushMessageToAndriod(PushMessage pushMessage, String alias) {
IGtPush push = new IGtPush(host, appKey, masterSecret);
NotificationTemplate template = notificationTemplate(pushMessage);
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.toString());
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("服务器响应异常");
}
}
/**
* 把信息推送给安卓app内所有用户
*
* @param pushMessage
*/
public void pushMessageToApp(PushMessage pushMessage, String appTaskName) {
IGtPush push = new IGtPush(host, appKey, masterSecret);
//模版可以替换 这里不是透传模版 ios收不到
NotificationTemplate template = notificationTemplate(pushMessage);
AppMessage message = new AppMessage();
message.setData(template);
message.setOffline(true);
//离线有效时间,单位为毫秒,可选
message.setOfflineExpireTime(24 * 1000 * 3600);
//推送给App的目标用户需要满足的条件
AppConditions cdt = new AppConditions();
List appIdList = new ArrayList();
appIdList.add(appId);
message.setAppIdList(appIdList);
//手机类型
List phoneTypeList = new ArrayList();
//省份
List provinceList = new ArrayList();
//自定义tag
List tagList = new ArrayList();
cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList);
cdt.addCondition(AppConditions.REGION, provinceList);
cdt.addCondition(AppConditions.TAG, tagList);
message.setConditions(cdt);
//这个任务的名称,不会被展示
IPushResult ret = push.pushMessageToApp(message, appTaskName);
System.out.println(ret.getResponse().toString());
}
/**
* 安卓模版
*/
public NotificationTemplate notificationTemplate(PushMessage pushMessage) {
NotificationTemplate template = new NotificationTemplate();
// 设置APPID与APPKEY
template.setAppId(appId);
template.setAppkey(appKey);
// 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动
template.setTransmissionType(1);
template.setTransmissionContent("点击启动应用");
// 设置定时展示时间
// template.setDuration("2015-01-16 11:40:00", "2015-01-16 12:24:00");
Style0 style = new Style0();
// 设置通知栏标题与内容
style.setTitle(pushMessage.getTitle());
style.setText(pushMessage.getMsg());
// 配置通知栏图标
//style.setLogo("icon.png");
// 配置通知栏网络图标
//style.setLogoUrl("");
// 设置通知是否响铃,震动,或者可清除
style.setRing(true);
style.setVibrate(true);
style.setClearable(true);
template.setStyle(style);
return template;
}
/**
* ios/android透传模版
*/
public static TransmissionTemplate getTemplate(PushMessage pushMessage) {
TransmissionTemplate template = new TransmissionTemplate();
template.setAppId(appId);
template.setAppkey(appKey);
template.setTransmissionContent(pushMessage.getMsg());
template.setTransmissionType(2);
APNPayload payload = new APNPayload();
//在已有数字基础上加1显示,设置为-1时,在已有数字上减1显示,设置为数字时,显示指定数字
payload.setAutoBadge("+1");
payload.setContentAvailable(1);
payload.setSound("default");
//payload.setCategory("$由客户端定义");
//简单模式APNPayload.SimpleMsg
//payload.setAlertMsg(new APNPayload.SimpleAlertMsg("hello"));
//字典模式使用APNPayload.DictionaryAlertMsg
payload.setAlertMsg(getDictionaryAlertMsg(pushMessage));
// 添加多媒体资源
/*payload.addMultiMedia(new MultiMedia().setResType(MultiMedia.MediaType.video)
.setResUrl("http://ol5mrj259.bkt.clouddn.com/test2.mp4")
.setOnlyWifi(true));*/
//需要使用IOS语音推送,请使用VoIPPayload代替APNPayload
// VoIPPayload payload = new VoIPPayload();
// JSONObject jo = new JSONObject();
// jo.put("key1","value1");
// payload.setVoIPPayload(jo.toString());
//
template.setAPNInfo(payload);
return template;
}
/**
* ios离线APNS配置
*
* @return
*/
private static APNPayload.DictionaryAlertMsg getDictionaryAlertMsg(PushMessage pushMessage) {
APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();
alertMsg.setBody(pushMessage.getMsg());
//alertMsg.setActionLocKey("ActionLockey");
//alertMsg.setLocKey("LocKey");
//alertMsg.addLocArg("loc-args");
//alertMsg.setLaunchImage("launch-image");
// iOS8.2以上版本支持
alertMsg.setTitle(pushMessage.getTitle());
//alertMsg.setSubtitle("子标题");
//alertMsg.setTitleLocKey("TitleLocKey");
//alertMsg.addTitleLocArg("TitleLocArg");
return alertMsg;
}
}
3.PushMessage类
public class PushMessage extends BaseModel {
private String id;
private String to;
private String title;
private String image;
private String msg;
private Date timestamp;
private String readed;
private String reached;
//离线=offline ;在线=online ;系统 = system
private String type;
//0=文本; 1=图片;2=音频; 3=视频; 4=简历; 5=职位; 6=约面; 7=房源; 8=求租 ;
// 9=预约看房;10=名片; 20=招聘;22=租房;25=系统;55=已读未读; 77=登录消息; 99=action
private String msgtype;
private String destination;
}
1.andriod能接受消息,而ios不行,有可能模版选错,ios只能接收透传模板
2.关于透传模版,在客户端是没有任何提示的,透传消息个推SDK接收到后直接广播给客户端,不做任何处理,需要客户端自己去处理。确认客户端是否对透传消息进行处理