代码是由我们公司俊哥呕心沥血编写的,良好的独立模块,耦合度低,完全是做到了,拿来即用;不用在配多余的配置;
希望各位在运用代码时,可以保留下原著作者;
回到正题,要使用个推;
- 必须先申请账号,获取其AppID,AppSecret等配置信息;
- 需和app端相协同合作,获取app客户端的cid (这样才能准确推送到客户端上)
具体请看个推官网api文档内容: http://docs.getui.com/server/java/start/
一样老规矩,直接贴代码;
1.调用代码处
2.基础使用类代码块
import java.io.IOException;import java.util.ArrayList;import java.util.List;import java.util.Map;import org.apache.log4j.Logger;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.APNTemplate;import com.gexin.rp.sdk.template.NotificationTemplate;import com.gexin.rp.sdk.template.TransmissionTemplate;/** * ** 功能:个推app客户端消息推送实现类 *
* @ClassName: PushMsgInfoImpl * @version V1.0 * @date 2014-12-12 * @author tangy */public class PushMsgInfoHelper implements PushMsgInfo{ private static Logger log = Logger.getLogger(PushMsgInfoHelper.class); /** * 离线消息有效时间(ms) */ private static long OFFLINE_EXPLIRE_TIME = 24 * 3600 * 1000; /** * ** 功能:向单个安卓客户端发送消息,需app采集clientid 通知 *
* @author junly * @date 2 * @throws IOException */ public String andrSinglePush(String clientId,GeTuiModel model) throws IOException{ IGtPush push = new IGtPush(model.getHost(), model.getAppKey(),model.getMaster()); push.connect(); NotificationTemplate template = PushTemplate.notificationTemplate(model.getAppId(), model.getAppKey(), model.getTitle(), model.getContent(), model.getTransContent()); SingleMessage message = new SingleMessage(); message.setOffline(false); //离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(OFFLINE_EXPLIRE_TIME); message.setData(template); Target target1 = new Target(); target1.setAppId(model.getAppId()); target1.setClientId(clientId); IPushResult ret = push.pushMessageToSingle(message, target1); log.info("向单个安卓客户端发送消息:"+ret.getResponse().toString()); return ret.getResponse().toString(); } /** * ** 功能:个推向所有安卓客户端发送消息 *
* @author junly * @date * @param title * @param content * @return 发送结果code * @throws IOException */ public MappushAllAndroid(GeTuiModel model) throws IOException{ IGtPush push = new IGtPush(model.getHost(), model.getAppKey(),model.getMaster()); //建立连接,开始鉴权 push.connect(); //通知模板 NotificationTemplate template = PushTemplate.notificationTemplate(model.getAppId(), model.getAppKey(), model.getTitle(), model.getContent(), model.getTransContent()); AppMessage message = new AppMessage(); message.setData(template); //设置消息离线,并设置离线时间 message.setOffline(true); //离线有效时间,单位为毫秒,可选 message.setOfflineExpireTime(OFFLINE_EXPLIRE_TIME); //推送给App的目标用户需要满足的条件 AppConditions cdt = new AppConditions(); //设置推送目标条件过滤 List appIdList = new ArrayList (); appIdList.add(model.getAppId()); message.setAppIdList(appIdList); //手机类型 List phoneTypeList = new ArrayList (); phoneTypeList.add("ANDROID"); cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList); message.setConditions(cdt); IPushResult ret = push.pushMessageToApp(message); log.info("个推向所有安卓客户端发送消息推送:"+ret.getResponse().toString()); return ret.getResponse(); } /** * 功能:IOS单个推送(不建议使用了字数有限制) *
* @author junly * @date 2016年12月26日 * @param cid * @param model * @return * @throws IOException * @see com.tsou.funny.common.helper.PushMsgInfo#iosSinglePush(java.lang.String, com.tsou.funny.common.model.GeTuiModel) */ public String iosSinglePush(String cid,GeTuiModel model) throws IOException { IGtPush push = new IGtPush(model.getHost(), model.getAppKey(),model.getMaster()); // 建立连接,开始鉴权 push.connect(); APNTemplate template = new APNTemplate(); APNPayload apnpayload = new APNPayload(); apnpayload.setSound(""); APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg(); alertMsg.setTitle(model.getTitle()); alertMsg.setBody(model.getContent()); apnpayload.setAlertMsg(alertMsg); template.setAPNInfo(apnpayload); SingleMessage singleMessage = new SingleMessage(); singleMessage.setOffline(true); singleMessage.setOfflineExpireTime(OFFLINE_EXPLIRE_TIME); singleMessage.setData(template); IPushResult ret = push.pushAPNMessageToSingle(model.getAppId(), cid, singleMessage); log.info("消息推送:"+ret.getResponse().toString()); return ret.getResponse().toString(); } /* 推所有苹果 * (non-Javadoc) * @see com.tsou.funny.user.app.helper.PushMsgInfo#pushAllIOS(com.tsou.funny.common.model.GeTuiModel) */ @Override public MappushAllIOS(GeTuiModel model) throws IOException { IGtPush push = new IGtPush(model.getHost(), model.getAppKey(),model.getMaster()); push.connect(); TransmissionTemplate template = PushTemplate.transmissionTemplate(model.getAppId(), model.getAppKey(), model.getTitle(),model.getContent(), model.getContent()); AppMessage message = new AppMessage(); message.setOffline(true); message.setOfflineExpireTime(OFFLINE_EXPLIRE_TIME); message.setPushNetWorkType(0); message.setData(template); List appIdList = new ArrayList (); appIdList.add(model.getAppId()); message.setAppIdList(appIdList); //推送给App的目标用户需要满足的条件 AppConditions cdt = new AppConditions(); //手机类型 List phoneTypeList = new ArrayList (); phoneTypeList.add("IOS"); cdt.addCondition(AppConditions.PHONE_TYPE, phoneTypeList); message.setConditions(cdt); IPushResult ret = push.pushMessageToApp(message); return ret.getResponse(); } /** * 功能:单个推送使用透传推送!(苹果安卓都可以用) 替代安卓单个推送和苹果单个推送 *
* @author junly * @date 2016年10月9日 * @param cid * @param model * @param devieType * @return * @throws IOException * @see com.tsou.funny.user.app.helper.PushMsgInfo#singlePushByTransmission(java.lang.String, com.tsou.funny.common.model.GeTuiModel, java.lang.Integer) */ @Override public String singlePushByTransmission(String cid, GeTuiModel model) throws IOException { IGtPush push = new IGtPush(model.getHost(), model.getAppKey(),model.getMaster()); push.connect(); TransmissionTemplate template = PushTemplate.transmissionTemplate(model.getAppId(), model.getAppKey(), model.getTitle(), model.getContent(),model.getTransContent()); SingleMessage message = new SingleMessage(); message.setOffline(true); message.setOfflineExpireTime(OFFLINE_EXPLIRE_TIME); message.setPushNetWorkType(0); message.setData(template); Target target = new Target(); target.setAppId(model.getAppId()); 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) { return ret.getResponse().toString(); } else { return "0"; } }}
public class GeTuiModel { /** * 个推接口地址 */ private String host = "http://sdk.open.api.igexin.com/apiex.htm"; /** * 个推申请的应用ID */ private String appId = "XXXXXXXXXXXXXXXXXXXXXXXX"; /** * 个推申请的应用KEY */ private String appKey = "XXXXXXXXXXXXXXXXXXXXXXXX"; /** * 个推申请的安全密钥mastersecret */ private String master = "XXXXXXXXXXXXXXXXXXXXXXXX"; /** * 通知的标题 */ private String title = "标题"; /** * 推送的内容 */ private String content; /** * 透传内容 */ private String transContent; public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getAppId() { return appId; } public void setAppId(String appId) { this.appId = appId; } public String getAppKey() { return appKey; } public void setAppKey(String appKey) { this.appKey = appKey; } public String getMaster() { return master; } public void setMaster(String master) { this.master = master; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } public String getTransContent() { return transContent; } public void setTransContent(String transContent) { this.transContent = transContent; } public GeTuiModel(String host, String appId, String appKey, String master, String title, String content, String transContent) { super(); this.host = host; this.appId = appId; this.appKey = appKey; this.master = master; this.title = title; this.content = content; this.transContent = transContent; } public GeTuiModel() {} }
import com.gexin.rp.sdk.base.payload.APNPayload;import com.gexin.rp.sdk.template.NotificationTemplate;import com.gexin.rp.sdk.template.TransmissionTemplate;public class PushTemplate { /** * 功能:点击通知打开应用模板 * @param title * @param text * @param transText * @return */ public static final NotificationTemplate notificationTemplate(String appId,String appKey,String title,String text,String transText) { NotificationTemplate template = new NotificationTemplate(); // 设置APPID与APPKEY template.setAppId(appId); template.setAppkey(appKey); // 设置通知栏标题与内容 template.setTitle(title); template.setText(text); // 配置通知栏图标 template.setLogo("icon.png"); // 配置通知栏网络图标 //if( Tool.notEmpty(logoUrl) ){ // template.setLogoUrl( logoUrl ); //} // 设置通知是否响铃,震动,或者可清除 template.setIsRing(true); //收到通知是否响铃:true响铃,false不响铃。默认响铃。 template.setIsVibrate(true); //收到通知是否振动:true振动,false不振动。默认振动。 template.setIsClearable(true); //通知是否可清除:true可清除,false不可清除。默认可清除。 // 透传消息设置,1为强制启动应用,客户端接收到消息后就会立即启动应用;2为等待应用启动 template.setTransmissionType(2); template.setTransmissionContent(transText); //透传内容,不支持转义字符 // 设定定时展示时间 //template.setDuration("2015-01-16 11:40:00", ""2015-01-16 12:24:00""); return template; } /** * 苹果透传面板 * junly * 2016年5月24日 下午4:01:59 * @param title * @param msgStr * @return */ public static TransmissionTemplate transmissionTemplate(String appId,String appKey,String title,String msgStr,String tranMsg) { TransmissionTemplate template = new TransmissionTemplate(); template.setAppId(appId ); template.setAppkey(appKey); template.setTransmissionType(2); //收到消息是否立即启动应用:1为立即启动,2则广播等待客户端自启动 template.setTransmissionContent(tranMsg); //透传内容,不支持转义字符 try { APNPayload payload = new APNPayload(); payload.setContentAvailable(1); payload.setSound("default"); APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg(); alertMsg.setTitle(title); alertMsg.setBody(msgStr); payload.setAlertMsg(alertMsg); template.setAPNInfo(payload); } catch (Exception e) { e.printStackTrace(); } return template; }}
发送成功提示
{result=ok, taskId=OSS-1226_06eea6f54d0aa0a8cad01a028813bd45, status=successed_offline}
offline的,说明cid是离线的 , online 则在线
顺带根据个推技术人员说明:
oss 是单推的,不统计记录的,个推官网后台是查询不到的;
所需jar包:
注意了,版本很重要,题主因没对好版本,报了其他错误,还是折腾一段时间才弄好的;