php极光推送获取cid返回404错误,极光推送心得
最近做了個極光推送的功能,一下是我對這個功能的一點理解;
首先要先介入極光的包,通過maven加載包:jiguang-common,gson,log4j,slf4j,導入的方法官網上都有,直接去極光API文檔上復制就行;導入完成之后在Service層添加推送方法
public Integer sendPush(String operatorId, String taskId) { ... }
首先要先添加兩個重要的對象ClientConfig,JPushClient;ClientConfig是客戶端配置的一些參數和極光推送需要訪問的一些url;JPushClient就是極光推送的核心類。
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);
ClientConfig.getInstance(); ? 獲取對象的實例;
MASTER_SECRET,APP_KEY,這兩個字段是注冊極光推送賬號的時候分配給你的,復制上去即可;
接下來是獲取regId和cid;regId是當客戶端接入了極光的代碼之后,會自動生成一個編碼,需要服務端這邊記錄下來,我自己建了一張表,通過客戶的手機號碼和regId建立管理關系,依次來推送。注:極光推送的時候,極光推送的服務端是不認手機號碼的,他們是根據regId來進行推送,所以regId至關重要;由于我有多個推送方法,而regId和cid是都需要獲取的,所以我把他提出來寫;
MapresultMap = regIdAndCid(operatorId);
public MapregIdAndCid(String operatorId) {
String result = "0";
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ????????clientConfig);Mapmap = new HashMap();
PbRegist pbRegist = new PbRegist();
pbRegist.setUserId(operatorId);
PbRegist regist = pbRegistDao.load(pbRegist);
String regId = "";
if (null != regist) {
regId = regist.getRegId();
} else {
_logger.info("regId未生成");
map.put("result", "2");
return map;
}
//獲取cid
CIDResult result_cidList = null;
String cid = "";
try {
result_cidList = jpushClient.getCidList(1, "push");
//切割cid
String a = result_cidList.toString();
cid = a.substring(13,a.length()-3);
} catch (APIConnectionException | APIRequestException e1) {
_logger.info("cid獲取錯誤");
e1.printStackTrace();
map.put("result", "1");
return map;
}
map.put("regId", regId);
map.put("cid", cid);
map.put("result", result);
return map;
}
operatorId是手機號碼,通過手機號碼查詢到表里面的regId;cid是通過jpushClient.getCidList方法獲得。其中result的編碼:0-成功,1-cid獲取失敗,2-該用戶未登錄沒有regId;
在都獲取完成之后,需要構建一個push的對象:PushPayload payload = buildPushObject_to_do_task(regId,cid);
//創建一個推送-待辦任務的推送
public static PushPayload buildPushObject_to_do_task(String regId, String cid) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.registrationId(regId))
.setNotification(Notification.alert(ALERT))
.setCid(cid)
.setOptions(Options.newBuilder()
//提交生產的時候需要切換
//.setApnsProduction(true)
.setApnsProduction(false)
.setTimeToLive(864000)
.build())
.build();
}
推送對象需要的元素是:Platform.all()推送的平臺,可以設置僅推ios,僅推and,和全平臺都推;Audience.registrationId(regId)這個就是客戶端登錄的時候,極光后臺分配給你的編碼,需要你自己保存的;.setNotification(Notification.alert(ALERT)),推送通知的內容,ALERT可以在類的頂部自己定義一段話;.setCid(cid) 通過 jpushClient.getCidList(...);方法獲取;Options選項Production:環境,true生產環境;false開發、測試環境;TimeToLive保留多少天,默認1天,最多10天,86400*10 = 10天;
推送對象完成后,進入最后一步,推送:
PushResult result = jpushClient.sendPush(payload); ? ?要用try -?catch
//推送
try {
PushResult result = jpushClient.sendPush(payload);
_logger.info("Got result - " + result);
return 0;
} catch (APIConnectionException e) {
_logger.error("Connection error. Should retry later. ", e);
return 1;
} catch (APIRequestException e) {
_logger.error("Error response from JPush server. Should review and fix it. ", e);
_logger.info("HTTP Status: " + e.getStatus());
_logger.info("Error Code: " + e.getErrorCode());
_logger.info("Error Message: " + e.getErrorMessage());
return 1;
}
備注:0-推送成功;1-推送失敗;
以下為整個類的完整代碼:
//極光推送 收到待辦任務 1-推送失敗,0-推送成功,2-該用戶未登錄,沒有生成regid沒有regId@Override
public Integer sendPush(String operatorId, String taskId) {
ClientConfig clientConfig = ClientConfig.getInstance();JPushClient jpushClient = new ????????JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);MapresultMap = ????????????regIdAndCid(operatorId);
String resultFlag = resultMap.get("result")+"";
if("1".equals(resultFlag)){
return 1;
}
if("2".equals(resultFlag)){
return 2;
}
String regId = resultMap.get("regId")+"";
String cid = resultMap.get("cid")+"";
//構建一個push對象
PushPayload payload = buildPushObject_to_do_task(regId,cid);
_logger.info("payload - " + payload);
//推送
try {
PushResult result = jpushClient.sendPush(payload);
_logger.info("Got result - " + result);
return 0;
} catch (APIConnectionException e) {
_logger.error("Connection error. Should retry later. ", e);
return 1;
} catch (APIRequestException e) {
_logger.error("Error response from JPush server. Should review and fix it. ", e);
_logger.info("HTTP Status: " + e.getStatus());
_logger.info("Error Code: " + e.getErrorCode());
_logger.info("Error Message: " + e.getErrorMessage());
return 1;
}
}
//創建一個推送-待辦任務的推送
public static PushPayload buildPushObject_to_do_task(String regId, String cid) {
return PushPayload.newBuilder()
.setPlatform(Platform.all())
.setAudience(Audience.registrationId(regId))
.setNotification(Notification.alert(ALERT))
.setCid(cid)
.setOptions(Options.newBuilder()
//提交生產的時候需要切換
//.setApnsProduction(true)
.setApnsProduction(false)
.????????setTimeToLive(864000)
.build())
.build();
}
//獲取regId和cid方法
public MapregIdAndCid(String operatorId) {
String result = "0";
ClientConfig clientConfig = ClientConfig.getInstance();
JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, ????????????clientConfig);
Mapmap = new HashMap();
PbRegist pbRegist = new PbRegist();
pbRegist.setUserId(operatorId);
PbRegist regist = pbRegistDao.load(pbRegist);
String regId = "";
if (null != regist) {
regId = regist.getRegId();
} else {
_logger.info("regId未生成");
map.put("result", "2");
return map;
}
//獲取cid
CIDResult result_cidList = null;
String cid = "";
try {
result_cidList = jpushClient.getCidList(1, "push");
//切割cid
String a = result_cidList.toString();
cid = a.substring(13,a.length()-3);
} catch (APIConnectionException | APIRequestException e1) {
_logger.info("cid獲取錯誤");
e1.printStackTrace();
map.put("result", "1");
return map;
}
map.put("regId", regId);
map.put("cid", cid);
map.put("result", result);
return map;
}
第一次用極光推送,還有很多瑕疵,希望大家多多指點 ~
推送成功后的json:
Got result - {"msg_id":1141315895,"sendno":1718225847,"statusCode":0}
payload 推送對象的json:
payload -
{"platform":"all",
"audience":{"registration_id":["13065ffa4e0c9ea0662"]},
"notification":{"alert":"您有一條交辦任務于3小時后截止,記得去完成哦!",
"android":{"alert":"您有一條交辦任務于3小時后截止,記得去完成哦!",
"extras":{"taskId":"JB00013420171114011"},
"title":"任務到期推送"},"ios":{"alert":"您有一條交辦任務于3小時后截止,記得去完成哦!",
"extras":{"taskId":"JB00013420171114011"},"badge":"+1","sound":""}},
"options":{"sendno":1718225847,"time_to_live":864000,"apns_production":false},
"cid":"8c393f1fcc57e465e84019d5-d9f7fbc8-7cab-4447-aab4-350ab55c67ac"}
總結
以上是生活随笔為你收集整理的php极光推送获取cid返回404错误,极光推送心得的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 认识了大学
- 下一篇: http://coffeejp.com/