Use Simple Message Queue (formerly MNS) to receive delivery receipts.
Prerequisites
An Alibaba Cloud account and an AccessKey pair are created. For more information, see Obtain an AccessKey pair.
The MNS queue has access to the following domain names: dybaseapi.ap-southeast-1.aliyuncs.com and 1493622401794734.mns.ap-southeast-1.aliyuncs.com.
The idempotence of delivery receipts cannot be guaranteed. We recommend that you take appropriate measures to ensure the correctness and consistency of your data after receiving a delivery receipt.
Request parameters
Parameter | Type | Example | Description |
To | String | 8521234**** | The mobile phone number that receives delivery receipts. |
Status | String | 1 | The status of the message. Valid values:
|
MessageId | String | 123456789**** | The ID of the delivery receipt. |
SmsSize | String | 1 | The number of messages. A long message is split into multiple messages. |
SendDate | String | Thu, 25 Nov 2021 10:27:00 +0800 | The time when the message was sent to the carrier. |
ReceiveDate | String | Thu, 25 Nov 2021 10:27:33 +0800 | The time when the delivery receipt was received from the carrier. |
ErrorCode | String | success | The error code. |
ErrorDescription | String | success | The error message. |
Example
{
"To" : "8521234****",
"SendDate" : "Thu, 25 Nov 2021 10:27:00 +0800",
"ReceiveDate" : "Thu, 25 Nov 2021 10:27:33 +0800",
"Status" : "1",
"SmsSize":"1",
"ErrorCode" : "success",
"ErrorDescription" : "success",
"MessageId" : "123456789****"
}Download the demo
Refer to Simple Message Queue consumer demo and download the demo and SDK for your preferred programming language. The following steps use Java as an example.
After downloading the demo, some JAR files are in the
libdirectory. You need to add them as a library by right-clicking them and selecting Add as Library, as shown in the image below.You can find the Maven dependencies in the
pom.xmlfile to install the Alibaba Cloud SDK for Java.

Parameter configuration
You must configure the following parameters before using the sample code.
Configure AccessKey pair
Sample code
The content of the delivery receipts you receive is processed by the dealMessage method. You can write your business logic for handling these messages within this method.
// Parse the message body according to the specific format in the documentation
String arg = (String) contentMap.get("arg");
// Add your business logic herearg represents a parameter from the callback message body. Its values include: To, Status, MessageId, SmsSize, SendDate, ReceiveDate, ErrorCode, ErrorDescription.
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 for receiving messages from Alibaba Cloud Communications services only and cannot be used for other services
*/
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");
// Key values from the message
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);
// Parse the message body according to the specific format in the documentation
String arg = (String) contentMap.get("arg");
// Add your business logic here
}catch(com.google.gson.JsonSyntaxException e){
logger.error("error_json_format:"+message.getMessageBodyAsString(),e);
// In theory, format errors should not occur. If you encounter a malformed message, delete it to prevent it from being redelivered and causing repeated errors.
return true;
} catch (Throwable e) {
// If an exception is caused by your own code, you should return false. This prevents the message from being deleted and allows it to be redelivered according to the retry policy.
return false;
}
// If the message is processed successfully, return true. The SDK will then call the MNS delete method to remove the message from the queue.
return true;
}
}
public static void main(String[] args) throws Exception, ParseException {
DefaultAlicomMessagePuller puller=new DefaultAlicomMessagePuller();
// Set the size of the async thread pool, task queue, and the sleep time for threads when there is no data.
puller.setConsumeMinThreadSize(6);
puller.setConsumeMaxThreadSize(16);
puller.setThreadQueueSize(200);
puller.setPullMsgThreadSize(1);
// Enable this for server-side debugging. It should be disabled during normal operation as it impacts performance.
puller.openDebugLog(false);
// Obtain the AccessKey ID and AccessKey Secret from local environment variables.
String accessKeyId = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_ID");
String accessKeySecret = System.getenv("ALIBABA_CLOUD_ACCESS_KEY_SECRET");
/*
* Replace messageType and queueName with the required message type and corresponding queue name.
* Supported callback message types:
* 1: SmsReport: SMS delivery receipts
* 2: SmsUp: Mobile Originated messages
* 3: GlobeSmsReport: International SMS delivery receipts
*/
String messageType="<MESSAGE_TYPE>"; // Replace this with the message type. SmsReport is supported.
String queueName="<QUEUE_NAME>"; // After enabling the feature in the console, you can find the corresponding queueName on the page. For example: Alicom-Queue-******-SmsReport.
puller.startReceiveMsg(accessKeyId,accessKeySecret,messageType,queueName,new MyMessageListener());
}
}Output after running the demo:

