This topic provides examples of how to receive messages in popular programming languages. You can download the corresponding SDK package to pull messages from a queue.
SDK download links
The SMS SDK supports multiple programming languages. You can install the SDK by using the method provided in OpenAPI Explorer. You can also view the source code and related installation guidelines on GitHub. We recommend that you install the SDK by using mainstream dependency management tools of programming languages.
Language | SDK installation method | GitHub address | Quick start |
Java | |||
Python | |||
PHP | |||
Go | |||
TypeScript | |||
C++ | N/A | ||
C# | |||
Swift | N/A |
Message receipt examples
Before you call the API, configure environment variables for your access credentials. The environment variables for the AccessKey ID and AccessKey secret are ALIBABA_CLOUD_ACCESS_KEY_ID and ALIBABA_CLOUD_ACCESS_KEY_SECRET. For configuration details, see Configure environment variables.
Java demo
package com.alicom.mns.sample;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.alicom.mns.tools.inter.DefaultAlicomMessagePuller;
import com.alicom.mns.tools.inter.MessageListener;
import com.aliyun.mns.model.Message;
/**
* This is a demo for consuming messages from an SMS MNS queue.
* MessageType: SmsReport
*/
public class ReceiveDemo {
static class MyMessageListener implements MessageListener {
@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 {
// Start your own code here.
} catch (Throwable e) {
// This exception is caused by your code. The message is not deleted and can be retrieved again.
return false;
}
// The message is deleted due to a format error.
return true;
}
}
public static void main(String[] args) {
DefaultAlicomMessagePuller puller = new DefaultAlicomMessagePuller();
// Set the size of the asynchronous thread.
puller.setConsumeMinThreadSize(6);
puller.setConsumeMaxThreadSize(16);
puller.setThreadQueueSize(200);
puller.setPullMsgThreadSize(1);
// Enable this only for debugging. Disable it in a production environment.
puller.openDebugLog(false);
String regionIdForPop = "ap-southeast-1";
String endpointNameForPop = "ap-southeast-1";
// If you send SMS requests to the Singapore region OpenAPI (domain: dysmsapi.ap-southeast-1.aliyuncs.com)
// Use the following settings.
String domainForPop = "dybaseapi.ap-southeast-1.aliyuncs.com";
String mnsAccountEndpoint = "http://1493622401794734.mns.ap-southeast-1.aliyuncs.com";
// If you send SMS requests to the Indonesia (Jakarta) region OpenAPI (domain: dysmsapi.ap-southeast-5.aliyuncs.com)
// Use the following settings.
//String domainForPop = "dybaseapi.ap-southeast-5.aliyuncs.com";
//String mnsAccountEndpoint = "http://1493622401794734.mns.ap-southeast-5.aliyuncs.com";
// If you send SMS requests to the Germany (Frankfurt) region OpenAPI (domain: dysmsapi.eu-central-1.aliyuncs.com)
//String domainForPop = "dybaseapi.eu-central-1.aliyuncs.com";
//String mnsAccountEndpoint = "http://1493622401794734.mns.eu-central-1.aliyuncs.com";
// If you send SMS requests to the US East (Virginia) region OpenAPI (domain: dysmsapi.us-east-1.aliyuncs.com)
//String domainForPop = "dybaseapi.us-east-1.aliyuncs.com";
//String mnsAccountEndpoint = "http://1493622401794734.mns.us-east-1.aliyuncs.com";
// You can get the AccessKey ID and AccessKey secret from the Alibaba Cloud console.
String accessKeyId = "";
String accessKeySecret = "";
//SmsReport
String messageType = "SmsReport";
// You can get the queue name from the Alibaba Cloud SMS console.
// Pattern: Alicom-Queue-{UID}-{messageType}
String queueName = "";
puller.startReceiveMsgForVPC(accessKeyId, accessKeySecret, messageType, queueName, regionIdForPop,
endpointNameForPop, domainForPop, mnsAccountEndpoint, new MyMessageListener());
// Or, initialize your IAcsClient (AccessKey secret not required).
//puller.startReceiveMsg(acsClient, messageType, queueName, new MyMessageListener(), mnsAccountEndpoint);
}
}
FAQ
SSL certificate verification failed ("SSL: CERTIFICATE_VERIFY_FAILED") when integrating the Python SDK on macOS
What causes the "SSL: CERTIFICATE_VERIFY_FAILED" exception when I integrate the Python SDK in a macOS environment? (WebSocket closed due to [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to retrieve local issuer certificate (_ssl.c:1000)).
When you connect to a WebSocket, OpenSSL may fail to verify the certificate and report that the certificate cannot be found. This issue is usually caused by incorrect certificate configurations in the Python environment. You can follow these steps to manually locate and fix the certificate issue:
Export system certificates and set environment variables: Run the following commands to export all certificates from the macOS system to a file. Then, set this file as the default certificate path for Python and related libraries.
security find-certificate -a -p > ~/all_mac_certs.pem export SSL_CERT_FILE=~/all_mac_certs.pem export REQUESTS_CA_BUNDLE=~/all_mac_certs.pemCreate a symbolic link to fix the Python OpenSSL configuration: If the Python OpenSSL configuration is missing a certificate, you can run the following command to manually create a symbolic link. Make sure to replace the path in the command with the actual installation directory of your local Python version.
# 3.9 is an example version number. Adjust the path based on the Python version installed on your local machine. ln -s /etc/ssl/* /Library/Frameworks/Python.framework/Versions/3.9/etc/opensslRestart the terminal and clear the cache: After you complete the preceding operations, close and reopen the terminal to make sure that the environment variables take effect. Clear any possible cache and try to connect to the WebSocket again.
These steps can resolve connection issues caused by incorrect certificate configurations. If the issue persists, check whether the certificate configuration of the destination server is correct.