日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > php >内容正文

php

php极光推送获取cid返回404错误,极光推送心得

發(fā)布時(shí)間:2023/12/18 php 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php极光推送获取cid返回404错误,极光推送心得 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

最近做了個(gè)極光推送的功能,一下是我對(duì)這個(gè)功能的一點(diǎn)理解;

首先要先介入極光的包,通過(guò)maven加載包:jiguang-common,gson,log4j,slf4j,導(dǎo)入的方法官網(wǎng)上都有,直接去極光API文檔上復(fù)制就行;導(dǎo)入完成之后在Service層添加推送方法

public Integer sendPush(String operatorId, String taskId) { ... }

首先要先添加兩個(gè)重要的對(duì)象ClientConfig,JPushClient;ClientConfig是客戶(hù)端配置的一些參數(shù)和極光推送需要訪問(wèn)的一些url;JPushClient就是極光推送的核心類(lèi)。

ClientConfig clientConfig = ClientConfig.getInstance();

JPushClient jpushClient = new JPushClient(MASTER_SECRET, APP_KEY, null, clientConfig);

ClientConfig.getInstance(); ? 獲取對(duì)象的實(shí)例;

MASTER_SECRET,APP_KEY,這兩個(gè)字段是注冊(cè)極光推送賬號(hào)的時(shí)候分配給你的,復(fù)制上去即可;

接下來(lái)是獲取regId和cid;regId是當(dāng)客戶(hù)端接入了極光的代碼之后,會(huì)自動(dòng)生成一個(gè)編碼,需要服務(wù)端這邊記錄下來(lái),我自己建了一張表,通過(guò)客戶(hù)的手機(jī)號(hào)碼和regId建立管理關(guān)系,依次來(lái)推送。注:極光推送的時(shí)候,極光推送的服務(wù)端是不認(rèn)手機(jī)號(hào)碼的,他們是根據(jù)regId來(lái)進(jìn)行推送,所以regId至關(guān)重要;由于我有多個(gè)推送方法,而regId和cid是都需要獲取的,所以我把他提出來(lái)寫(xiě);

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獲取錯(cuò)誤");

e1.printStackTrace();

map.put("result", "1");

return map;

}

map.put("regId", regId);

map.put("cid", cid);

map.put("result", result);

return map;

}

operatorId是手機(jī)號(hào)碼,通過(guò)手機(jī)號(hào)碼查詢(xún)到表里面的regId;cid是通過(guò)jpushClient.getCidList方法獲得。其中result的編碼:0-成功,1-cid獲取失敗,2-該用戶(hù)未登錄沒(méi)有regId;

在都獲取完成之后,需要構(gòu)建一個(gè)push的對(duì)象:PushPayload payload = buildPushObject_to_do_task(regId,cid);

//創(chuàng)建一個(gè)推送-待辦任務(wù)的推送

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()

//提交生產(chǎn)的時(shí)候需要切換

//.setApnsProduction(true)

.setApnsProduction(false)

.setTimeToLive(864000)

.build())

.build();

}

推送對(duì)象需要的元素是:Platform.all()推送的平臺(tái),可以設(shè)置僅推ios,僅推and,和全平臺(tái)都推;Audience.registrationId(regId)這個(gè)就是客戶(hù)端登錄的時(shí)候,極光后臺(tái)分配給你的編碼,需要你自己保存的;.setNotification(Notification.alert(ALERT)),推送通知的內(nèi)容,ALERT可以在類(lèi)的頂部自己定義一段話;.setCid(cid) 通過(guò) jpushClient.getCidList(...);方法獲取;Options選項(xiàng)Production:環(huán)境,true生產(chǎn)環(huán)境;false開(kāi)發(fā)、測(cè)試環(huán)境;TimeToLive保留多少天,默認(rèn)1天,最多10天,86400*10 = 10天;

推送對(duì)象完成后,進(jìn)入最后一步,推送:

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-推送失敗;

以下為整個(gè)類(lèi)的完整代碼:

//極光推送 收到待辦任務(wù) 1-推送失敗,0-推送成功,2-該用戶(hù)未登錄,沒(méi)有生成regid沒(méi)有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")+"";

//構(gòu)建一個(gè)push對(duì)象

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;

}

}

//創(chuàng)建一個(gè)推送-待辦任務(wù)的推送

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()

//提交生產(chǎn)的時(shí)候需要切換

//.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獲取錯(cuò)誤");

e1.printStackTrace();

map.put("result", "1");

return map;

}

map.put("regId", regId);

map.put("cid", cid);

map.put("result", result);

return map;

}

第一次用極光推送,還有很多瑕疵,希望大家多多指點(diǎn) ~

推送成功后的json:

Got result - {"msg_id":1141315895,"sendno":1718225847,"statusCode":0}

payload 推送對(duì)象的json:

payload -

{"platform":"all",

"audience":{"registration_id":["13065ffa4e0c9ea0662"]},

"notification":{"alert":"您有一條交辦任務(wù)于3小時(shí)后截止,記得去完成哦!",

"android":{"alert":"您有一條交辦任務(wù)于3小時(shí)后截止,記得去完成哦!",

"extras":{"taskId":"JB00013420171114011"},

"title":"任務(wù)到期推送"},"ios":{"alert":"您有一條交辦任務(wù)于3小時(shí)后截止,記得去完成哦!",

"extras":{"taskId":"JB00013420171114011"},"badge":"+1","sound":""}},

"options":{"sendno":1718225847,"time_to_live":864000,"apns_production":false},

"cid":"8c393f1fcc57e465e84019d5-d9f7fbc8-7cab-4447-aab4-350ab55c67ac"}

總結(jié)

以上是生活随笔為你收集整理的php极光推送获取cid返回404错误,极光推送心得的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。