简介
当您使用流量的API接口充值完流量后,可以通过使用MNS的Queue模型来接收充值的回执消息,假如服务出现异常情况时(如网络问题),导致消息回执未成功获取,还可以通过充值状态查询API接口进行一定的补偿(目前支持30天内发送记录的查询)。
消息的订阅
云通信的所有业务消息都用过MNS消息服务向外发送。用户每订阅一个类别的消息(比如流量充值回执报告消息FlowReport),系统都会为用户分配一个独立的消息队列。用户可以通过阿里云账号拿到一个临时的token用于获取队列中的消息。用户可以下载demo,编写简单的消息处理类即可完成消息处理的任务。在页面上订阅消息,订阅完消息后,能拿到消息队列名称(queueName)。比如:Alicom-Queue-xxxxxx-FlowReport 。队列名字每个用户都不同。
消息类型
流量提供一种消息类型,流量充值回执消息FlowReport
流量充值回执消息FlowReport消息体格式
技术对接步骤
下载消息SDK
下载对应语音的消息DEMO工程,工程所需要的所有依赖jar包都放在DEMO工程的lib目录下,将对于的jar包引入到您的工程当中既可按照DEMO样例编写接收消息的程序。
SDK&DEMO[
下载地址]
- [backcolor=transparent]/**
- [backcolor=transparent] * 只能用于接收云通信的消息,不能用于接收其他业务的消息
- [backcolor=transparent] */
- [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]ReceiveAlicomMsgDemo[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]Log[backcolor=transparent] logger[backcolor=transparent]=[backcolor=transparent]LogFactory[backcolor=transparent].[backcolor=transparent]getLog[backcolor=transparent]([backcolor=transparent]ReceiveAlicomMsgDemo[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]class[backcolor=transparent] [backcolor=transparent]MyMessageListener[backcolor=transparent] [backcolor=transparent]implements[backcolor=transparent] [backcolor=transparent]MessageListener[backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]private[backcolor=transparent] [backcolor=transparent]Gson[backcolor=transparent] gson[backcolor=transparent]=[backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]Gson[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]@Override
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]boolean[backcolor=transparent] dealMessage[backcolor=transparent]([backcolor=transparent]Message[backcolor=transparent] message[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"message handle: "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] message[backcolor=transparent].[backcolor=transparent]getReceiptHandle[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"message body: "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] message[backcolor=transparent].[backcolor=transparent]getMessageBodyAsString[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"message id: "[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] message[backcolor=transparent].[backcolor=transparent]getMessageId[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]System[backcolor=transparent].[backcolor=transparent]out[backcolor=transparent].[backcolor=transparent]println[backcolor=transparent]([backcolor=transparent]"message dequeue count:"[backcolor=transparent] [backcolor=transparent]+[backcolor=transparent] message[backcolor=transparent].[backcolor=transparent]getDequeueCount[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]try[backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]Map[backcolor=transparent]<[backcolor=transparent]String[backcolor=transparent],[backcolor=transparent]Object[backcolor=transparent]>[backcolor=transparent] contentMap[backcolor=transparent]=[backcolor=transparent]gson[backcolor=transparent].[backcolor=transparent]fromJson[backcolor=transparent]([backcolor=transparent]message[backcolor=transparent].[backcolor=transparent]getMessageBodyAsString[backcolor=transparent](),[backcolor=transparent] [backcolor=transparent]HashMap[backcolor=transparent].[backcolor=transparent]class[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] phone[backcolor=transparent]=([backcolor=transparent]String[backcolor=transparent])[backcolor=transparent]contentMap[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"phone"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]Boolean[backcolor=transparent] outId[backcolor=transparent]=([backcolor=transparent]Boolean[backcolor=transparent])[backcolor=transparent]contentMap[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"out_id"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] result[backcolor=transparent]=([backcolor=transparent]String[backcolor=transparent])[backcolor=transparent]contentMap[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"result"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] errCode[backcolor=transparent]=([backcolor=transparent]String[backcolor=transparent])[backcolor=transparent]contentMap[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"err_code"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] errMsg[backcolor=transparent]=([backcolor=transparent]String[backcolor=transparent])[backcolor=transparent]contentMap[backcolor=transparent].[backcolor=transparent]get[backcolor=transparent]([backcolor=transparent]"err_msg"[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]//TODO 这里开始写业务代码
- [backcolor=transparent] [backcolor=transparent]}[backcolor=transparent]catch[backcolor=transparent]([backcolor=transparent]com[backcolor=transparent].[backcolor=transparent]google[backcolor=transparent].[backcolor=transparent]gson[backcolor=transparent].[backcolor=transparent]JsonSyntaxException[backcolor=transparent] e[backcolor=transparent]){
- [backcolor=transparent] logger[backcolor=transparent].[backcolor=transparent]error[backcolor=transparent]([backcolor=transparent]"error_json_format:"[backcolor=transparent]+[backcolor=transparent]message[backcolor=transparent].[backcolor=transparent]getMessageBodyAsString[backcolor=transparent](),[backcolor=transparent]e[backcolor=transparent]);
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]Boolean[backcolor=transparent] dealResult[backcolor=transparent]=[backcolor=transparent]true[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]return[backcolor=transparent] dealResult[backcolor=transparent];[backcolor=transparent]//返回true,则工具类自动删除已拉取的消息。
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent] [backcolor=transparent]public[backcolor=transparent] [backcolor=transparent]static[backcolor=transparent] [backcolor=transparent]void[backcolor=transparent] main[backcolor=transparent]([backcolor=transparent]String[backcolor=transparent][][backcolor=transparent] args[backcolor=transparent])[backcolor=transparent] [backcolor=transparent]throws[backcolor=transparent] com[backcolor=transparent].[backcolor=transparent]aliyuncs[backcolor=transparent].[backcolor=transparent]exceptions[backcolor=transparent].[backcolor=transparent]ClientException[backcolor=transparent],[backcolor=transparent] [backcolor=transparent]ParseException[backcolor=transparent] [backcolor=transparent]{
- [backcolor=transparent] [backcolor=transparent]DefaultAlicomMessagePuller[backcolor=transparent] puller[backcolor=transparent]=[backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]DefaultAlicomMessagePuller[backcolor=transparent]();
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] accessKeyId[backcolor=transparent]=[backcolor=transparent]"yourAccessKeyId"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] accessKeySecret[backcolor=transparent]=[backcolor=transparent]"yourAccessKeySecret"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] messageType[backcolor=transparent]=[backcolor=transparent]"FlowReport"[backcolor=transparent];
- [backcolor=transparent] [backcolor=transparent]String[backcolor=transparent] queueName[backcolor=transparent]=[backcolor=transparent]"yourQueueName"[backcolor=transparent];[backcolor=transparent]//在云通信页面开通相应业务消息后,就能在页面上获得对应的queueName
- [backcolor=transparent] puller[backcolor=transparent].[backcolor=transparent]startReceiveMsg[backcolor=transparent]([backcolor=transparent]accessKeyId[backcolor=transparent],[backcolor=transparent]accessKeySecret [backcolor=transparent],[backcolor=transparent]messageType[backcolor=transparent],[backcolor=transparent]queueName [backcolor=transparent],[backcolor=transparent] [backcolor=transparent]new[backcolor=transparent] [backcolor=transparent]MyMessageListener[backcolor=transparent]());
- [backcolor=transparent] [backcolor=transparent]}
- [backcolor=transparent]}