IoT Platform allows cloud applications to receive device messages by listening to queues in Message Service (MNS). This topic describes how to subscribe to device messages by using Message Service.

Procedure

  1. In the IoT Platform console, configure service subscription for a product. IoT Platform can automatically forward messages to queues in Message Service.
    1. From the left-side navigation pane, choose Devices > Product. On the Products page, select a product, and click View in the Actions column.
    2. Click the Service Subscription tab.
    3. On the Service Subscription tab page, click Set corresponding to User Service Client(Push MNS). Then in the dialog box that appears, select the types of messages that you want to push to MNS.

    After the subscription is complete, IoT Platform automatically creates a message queue in MNS . Details about the message queue are displayed on the Service Subscription tab page.

  2. Receive device messages by listening to the message queue.

    In this example, enter the following information to use the Java SDK of Message Service.

    For more information, see Message Service documentation.

    • In the pom.xml file, add the following dependencies:
      <dependency>
          <groupId>com.aliyun.mns</groupId>
          <artifactId>aliyun-sdk-mns</artifactId>
          <version>1.1.8</version>
          <classifier>jar-with-dependencies</classifier>
      </dependency>
    • When you configure message receiving, enter the following information:
      CloudAccount account = new CloudAccount ($AccessKeyId, $AccessKeySecret, $AccountEndpoint);
      • Replace $AccessKeyId and $AccessKeySecret with your AccessKey ID and AccessKey Secret values. These values are required for you to access APIs. You can find these values in your profile by clicking the Alibaba Cloud account avatar.
      • Replace $AccountEndpoint with the actual endpoint value. You can obtain this value from the Message Service console.
    • Enter the logic for receiving device messages:
      MNSClient client = account.getMNSClient(); 
      CloudQueue queue = client.getQueueRef("aliyun-iot-a1xxxxxx8o9"); //Enter the name of the queue that has been automatically created
       
          while (true) { 
          //Get messages 
          Message popMsg = queue.popMessage(10);  //The timeout for long polling is 10 seconds      
          if (popMsg ! = null) { 
              System.out.println("PopMessage Body: "+ popMsg.getMessageBodyAsRawString()); //Get raw messages 
              queue.deleteMessage(popMsg.getReceiptHandle()); //Delete messages from the queue 
          } else { 
              System.out.println("Continuing");
      
    • Run the program to listen to the MNS queue.
  3. Start a device and send a message from the device to IoT Platform.

    To view the content of the reported message, see SDK reference.

  4. Check if the cloud applications can listen to the subscribed messages. If they successfully listen to the messages, they will receive messages like the following:
    {
    "messageid":" ", //The message ID
    "messagetype":"upload",
    "topic":"//The topic from which the message comes
    "payload": //Base64-encoded data
    "timestamp": //The timestamp
    }
    Parameter Description
    messageid Message ID generated by IoT Platform .
    messagetype The message type.
    • status: Indicates device status change notifications.
    • upload: Indicates device upstream notifications.
    • device_lifecycle: Indicates device lifecycle change messages.
    • topo_lifecycle: Indicates topological relationship change notifications.
    • topo_listfound: Indicates messages of sub-devices reported by gateway devices.
    topic The topic from which the message comes.
    payload Base64-encoded data of message payload.
    timestamp The timestamp in the format of Epoch.