This article describes how to create a Message Service (MNS) server-side subscription to push status changes of the devices under a product to an MNS queue. Your server can receive the messages by listening to the MNS queue.


  • The following Alibaba Cloud services are activated:
  • The Java development environment Eclipse is installed.

Background information

The following figure shows data flows.

Server-side subscription

Configure a server-side subscription

In the IoT Platform console, create an MNS server-side subscription and select the types of messages to which you want to subscribe.

  1. Log on to the IoT Platform console.
  2. In the left-side navigation pane, choose Devices > Products. On the Products page, click Create Product to create a product.
  3. In the left-side navigation pane, choose Devices > Devices. On the Devices page, click Add Device to create a device under the product.

    When you use Link SDK to develop a device, you must obtain the device certificate.

  4. In the left-side navigation pane, choose Rules Engine > Server-side Subscription. On the Server-side Subscription page, click Create Subscription to create an MNS server-side subscription. For more information, see Configure MNS server-side subscriptions.
    Note The first time you select MNS in the Subscription Type field, a message prompts you to authorize the access of IoT Platform to MNS. Click Authorize Now to go to the Resource Access Management (RAM) console. Click Confirm Authorization Policy.

    Set the Message Type parameter to Device Status Change Notification. All messages about status changes of the devices under the product are pushed to an MNS queue.

    After you create the 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 listen to 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.

    Server-side subscription

Use MNS SDK to receive messages

In this example, MNS SDK for Java is used.

  1. To download the SDK, see Release notes of the SDK for Java. Download the aliyun-sdk-mns-samples1.1.8 package, and then decompress the package.
  2. Open Eclipse, click Import Project, and then select the aliyun-sdk-mns-samples directory.
  3. 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.
    Parameter Description
    accountendpoint The endpoint of MNS. 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, log on to the Alibaba Cloud Management 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.
  4. Add the following code to the ComsumerDemo file 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(
            MNSClient client = account.getMNSClient(); // Initialize the client.
            // Retrieve messages.
                CloudQueue queue = client.getQueueRef("aliyun-iot-a1eN7La****");// Replace this queue with the queue that is automatically created by IoT Platform.
                for (int i = 0; i < 10; i++)
                    Message popMsg = queue.popMessage(); // The long polling period.
                    if (popMsg != null){
                        System.out.println("message handle:
                    " + popMsg.getReceiptHandle());
                        System.out.println("message body:
                    " + popMsg.getMessageBodyAsString()); // Retrieve the message payloads.
                        System.out.println("message id:
                    " + popMsg.getMessageId());
                        System.out.println("message dequeue count:"
            + popMsg.getDequeueCount());                    // <<to add your special logic.>>
                        // Delete messages from the queue.
                        System.out.println("delete message successfully.\n");
  5. Run the ComsumerDemo file.

Configure a device SDK

  1. To download a device SDK, see Download device SDKs. In this example, use the SDK for Java.
  2. At the bottom of the Configure a project article, click Java SDK Demo to download the package, and then decompress the package.
  3. In the Java development tool, import the JavaLinkKitDemo directory as a project.
  4. In the device_id file, specify the device certificate information.
    Server-side subscription
  5. In the MqttSample file of the src\devicesdk\demo directory, add the information about the device, and specify the name of the topic to which device data is submitted.
    Server-side subscription
  6. Run the MqttSample file to connect the device with IoT Platform.

Verify the result

After you run the code, a message indicating that the device is online is sent to the MNS queue. You can use MNS SDK for Java to receive the message and then delete the message from the queue.

The following figure shows how to receive the message and then delete the message.

Server-side subscription