This topic describes how to create a server-side subscription of the MNS type. The
subscription allows messages about status changes on the devices of a product to be
pushed to an MNS queue. Servers receive messages about status changes on devices by
monitoring MNS queues.
Background information
The following figure shows data flows.
Create a server-side subscription
In the IoT Platform console, follow these steps to create a server-side subscription
of the MNS type and select the types of messages to which you want to subscribe.
- Log on to the IoT Platform console.
- In the left-side navigation pane, choose . On the Products page, click Create Product to create a product.
- Choose to add a device in the product.
When you use a server MNS SDK to provision a device, you must obtain the certificate
information of the device.
- In the left-side navigation pane, choose . On the Server-side Subscription page, click Create Subscription to create a server-side subscription of the MNS type.
Note The first time you select MNS in the Subscription Type field, a message prompts you
to authorize access. Click Authorize Now to go to the Resource Access Management (RAM) console. Click Confirm Authorization
Policy.
Select Device Status Change Notification. In the product, messages about status changes that occur on all devices will be
pushed to an MNS queue.
After you create a server-side subscription, IoT Platform automatically creates a
queue in MNS. The queue is used to receive messages from IoT Platform. The format
of the queue name is aliyun-iot-${yourProductKey}
. When you use an MNS SDK to monitor the messages of a queue, you must specify the
name of the queue.
On the Server-side Subscription page, move the pointer over the icon next to MNS to
view the name of a queue.

Use MNS SDK for Java to receive messages
This section uses the MNS Java SDK Demo project as an example.
- Visit MNS SDK for Java. On the page that appears, click Download sample to download the aliyun-sdk-mns-samples1.1.8 package, and then decompress the package.
- Open Eclipse, click Import Project, and select the aliyun-sdk-mns-samples directory to import.
- In the C:\Users\${YourComputerUserName} local directory, create the .aliyun-mns .properties file. Add identity information in the following format to the file. The
identity information is used by MNS for authentication.
mns.accountendpoint=http://$your_accountId.mns.$your_regionId.aliyuncs.com
mns.accesskeyid=$your_accesskeyid
mns.accesskeysecret=$your_accesskeysecret
Parameter |
Description |
accountendpoint |
The endpoint of Message Service. In the MNS console, select the region where the queue resides and click Get Endpoint to view the information.
|
accesskeyid |
The AccessKey ID of your Alibaba Cloud account.
To create or view your AccessKey pair, follows these steps: Log on to the Alibaba
Cloud console. Move the pointer over your profile picture, and click AccessKey to go to the User Management page. On the page, you can create or view your AccessKey pairs.
|
accesskeysecret |
The AccessKey secret of your Alibaba Cloud account. Obtain the AccessKey secret in
the same way as the AccessKey ID.
|
- Add the following code to the ComsumerDemo script in the src\main\java\com.aliyun.mns.sample.Queue directory. The code is used to specify the name of the queue that is automatically
created by IoT Platform.
public static void main(String[] args) {
CloudAccount account = new CloudAccount(
ServiceSettings.getMNSAccessKeyId(),
ServiceSettings.getMNSAccessKeySecret());
ServiceSettings.getMNSAccountEndpoint());
MNSClient client = account.getMNSClient(); // Initialize a client
// Extract messages
try{
CloudQueue queue = client.getQueueRef("aliyun-iot-a1eN7La****");//Specify the name of the queue that is automatically created by IoT Platform
for (int i = 0; i < 10; i++)
{
Message popMsg = queue.popMessage(10); //The timeout for long polling
if (popMsg ! = null){
System.out.println("message handle: " + popMsg.getReceiptHandle());
System.out.println("message body: " + popMsg.getMessageBodyAsString()); //Obtain raw messages
System.out.println("message id: " + popMsg.getMessageId());
System.out.println("message dequeue count:" + popMsg.getDequeueCount());
//<<to add your special logic. >>
//Delete the messages from the queue
queue.deleteMessage(popMsg.getReceiptHandle());
System.out.println("delete message successfully.\n");
}
}
}
- Run the ComsumerDemo script.
Configure a device SDK
- Visit Download device SDKs and select Java SDK.
- At the bottom of the Configure a project topic, click Java SDK Demo to download the
package, and then decompress the package.
- In a Java IDE, select the JavaLinkKitDemo directory to import as a project.
- In the device_id file, add certificate information for the device.
- In the MqttSample script of the src\devicesdk\demo directory, add information of the device, specify the name of your device topic in
the publish method.
- Run the MqttSample project to connect the device to IoT Platform.
Verify the results
After you run the device SDK Demo project, a message indicating that the device is
online is sent to the MNS queue. The MNS Java SDK Demo project receives the message
from the queue and then deletes the message from the queue.
The following figure shows how the MNS Java SDK Demo project receives the message
and then deletes the message.