IoT Platform allows you to send device messages to Message Service (MNS). Cloud applications can obtain the device messages by listening to MNS queues. This topic describes how to configure an MNS server-side subscription.
Prerequisites
If you use a RAM user, the RAM user must have theAliyunIOTAccessingMNSRole
permission.Procedure
- In the IoT Platform console, configure an MNS server-side subscription for a product. IoT Platform automatically forwards messages to MNS queues.
- Configure an MNS client and listen to the MNS queue.
In this example, MNS SDK for Java is used to listen to the MNS queue.
For information about how to download the MNS SDK, see the MNS documentation.
- To install MNS SDK for Java, add the following dependencies to the pom.xml file:
<dependency> <groupId>com.aliyun.mns</groupId> <artifactId>aliyun-sdk-mns</artifactId> <version>1.1.8</version> <classifier>jar-with-dependencies</classifier> </dependency>
- Configure the following parameters when you configure the MNS SDK:
CloudAccount account = new CloudAccount( $AccessKeyId, $AccessKeySecret, $AccountEndpoint);
- Replace $AccessKeyId and $AccessKeySecret with the AccessKey ID and AccessKey secret of your Alibaba Cloud account. These parameters are required when you call API operations. To create or view an AccessKey pair, log on to the IoT Platform console, move the pointer over your profile picture, and then click AccessKey Management.
- Replace $AccountEndpoint with the MNS endpoint. In the MNS console, click Get Endpoint.
- Specify the logic that is used to receive device messages.
MNSClient client = account.getMNSClient(); CloudQueue queue = client.getQueueRef("aliyun-iot-a1wmrZPO8o9"); // Specify the automatically created queue. while (true) { // Retrieve messages. Message popMsg = queue.popMessage(10); // The timeout period of long polling requests is 10 seconds. if (popMsg != null) { System.out.println("PopMessage Body: "+ popMsg.getMessageBodyAsRawString()); // Obtain raw messages. queue.deleteMessage(popMsg.getReceiptHandle()); // Delete the messages from the queue. } else { System.out.println("Continuing"); } }
- Run the program to listen to the MNS queue.
- To install MNS SDK for Java, add the following dependencies to the pom.xml file:
- Start a device and send a message from the device to IoT Platform.
For information about how to use a Link SDK to configure a device, see the Link SDK documentation.
- Check whether your cloud application retrieves the message. The following code shows the format of a retrieved message:
{ "messageid":" ", "messagetype":"upload", "topic":"/al12345****/device123/user/update", "payload":" ", "timestamp": " " }
Parameter Description messageid The ID of the message. The message ID is generated by IoT Platform. messagetype The type of the message. Default value: false. Valid values: - upload: submitted device data
- status: device status changes
- topo_listfound: the detection of sub-devices by a gateway
- topo_lifecycle: device topology changes
- device_lifecycle: device lifecycle changes
- thing_history: historical TSL data
- ota_event: over-the-air (OTA) update status
topic The IoT Platform topic from which the message is forwarded. payload The base64-encoded message payload. For more information about data formats, see Data formats.
timestamp The timestamp. It is the number of seconds that have elapsed since the epoch time January 1, 1970, 00:00:00 UTC.