本文为您介绍了JAVA调用SmsReport查看短信回执。

请求参数

名称 类型 是否必选 示例值 描述
MessageType String SmsReport 有一种消息类型:SmsReport。
QueueName String Alicom-Queue-xxxxxx-SmsReport 阿里巴巴提供的消息队列名称,在短信控制台--系统设置可以查看到。

返回数据

名称 类型 示例值 描述
MessageId String 1008***3003003 短信的发送ID。
To String 45***0121 短信的发送号码。
Status Number 1 短信的发送状态:
  • 1 (Submitted)
  • 2 (Failed)
  • 3 (Delivered)
ErrorCode String success 短信发送返回的错误码。
ErrorDescription String success 短信发送返回的错误码详细描述。
SendDate String Mon, 24 Dec 2018 16:58:22 +0800 短信转发给运营商的时间。
ReceiveDate String Mon, 24 Dec 2018 16:58:22 +0800 短信收到运营商回执的时间。
NumberDetail String 号码的详细属性。
Country String Hongkong, China 号码所属国家。
Region String HongKong Number Region。
Carrier String CMI 号码所属的运营商网络。

引入阿里云核心包

SDK下载:点击查看下载链接

调用示例

  • regionIdForPop和endpointNameForPop必须为ap-southeast-1,请勿修改。
  • domainForPop必须为dysmsapi.ap-southeast-1.aliyuncs.com,请勿修改。
  • mnsAccountEndpoint必须为http://1493622401794734.mns.ap-southeast-1.aliyuncs.com, 请勿修改。
package com.alicom.mns.sample;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


import com.alicom.mns.tools.DefaultAlicomMessagePuller;
import com.alicom.mns.tools.MessageListener;
import com.aliyun.mns.model.Message;
import com.google.gson.Gson;

/**
 * This is the DEMO for SMS Delivery Report API
 */
public class ReceiveDemo {

  private static Log logger=LogFactory.getLog(ReceiveDemo.class);

  static class MyMessageListener implements MessageListener{
    private Gson gson=new Gson();

    @Override
    public boolean dealMessage(Message message) {

      SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      System.out.println("message receiver time from mns:" + format.format(new Date()));
      System.out.println("message handle: " + message.getReceiptHandle());
            System.out.println("message body: " + message.getMessageBodyAsString());
            System.out.println("message id: " + message.getMessageId());
            System.out.println("message dequeue count:" + message.getDequeueCount());
            System.out.println("Thread:" + Thread.currentThread().getName());
            try{
              Map<String,Object> contentMap=gson.fromJson(message.getMessageBodyAsString(), HashMap.class);

                String arg = (String) contentMap.get("arg");

        //Please start your own code here

            }catch(com.google.gson.JsonSyntaxException e){
              logger.error("error_json_format:"+message.getMessageBodyAsString(),e);
        // Message will be deleted for the format error
        return true;
            } catch (Throwable e) {
        // The exception caused by your own code. Message will not be deleted and be pushed again
        return false;
      }

      // Message will be deleted for the format error
      return true;
    }

  }

  public static void main(String[] args) throws Exception, ParseException {

    DefaultAlicomMessagePuller puller=new DefaultAlicomMessagePuller();

    //Set the size of asynchronous thread
    puller.setConsumeMinThreadSize(6);
    puller.setConsumeMaxThreadSize(16);
    puller.setThreadQueueSize(200);
    puller.setPullMsgThreadSize(1);
    //Only enabled when debugging, should be disabled at working status
    puller.openDebugLog(false);

    String regionIdForPop = "ap-southeast-1";
    String endpointNameForPop = "ap-southeast-1";
    String domainForPop = "dysmsapi.ap-southeast-1.aliyuncs.com";
    String mnsAccountEndpoint = "http://1493622401794734.mns.ap-southeast-1.aliyuncs.com";

    //AccessKey, you can get if from Alicloud Console
    String accessKeyId="yourAccessKeyId";
    String accessKeySecret="yourAccessKeySecret";

    //Message Type: SmsReport
    String messageType="SmsReport";
    //Message Quenue Name, you can get it from SMS console, like:Alicom-Queue-xxxxxx-SmsReport
    String queueName="Alicom-Queue-********-SmsReport";
    puller.startReceiveMsgForVPC(accessKeyId, accessKeySecret, messageType, queueName, regionIdForPop,
      endpointNameForPop, domainForPop, mnsAccountEndpoint,new MyMessageListener());
    } 
}