個推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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM