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"}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值