问题描述:

I need to use MQTT to deliver messages from our server to an android device,and i decided to go with the open source server mosquitto. I have setup all installation requirements for mosquitto and tokudu services.

The question now arises is how to achieve the main goal that is to send message to device. How can i achieve this.

I refered the link here for reference.

To send simple text messages and receive response i've implemented below service code.

$srv = new soap_server();

$srv->configureWSDL('send mqtt msg', 'urn:mqttwsdl');

$srv->register("send_mqtt_msg",

array('target' => 'xsd:string', 'message' => 'xsd:string'), // input parameters

array('return' => 'xsd:string'), // output parameters

'urn:mqttwsdl', // namespace

'urn:mqttwsdl#mqtt', // soapaction

'rpc', // style

'encoded', // use

'Sends message to server' // documentation );

function send_mqtt_msg has following stuff

function send_mqtt_msg($target, $message)

{

include("SAM/php_sam.php");

$conn = new SAMConnection();

$result = $conn->connect(SAM_MQTT, array("SAM_HOST" => '127.0.0.1', "SAM_PORT" => 1883));

$msgCpu = new SAMMessage($message);

$sent = $conn->send('topic://EBackup/'.$target, $msgCpu);

$rc = $conn->disconnect();

if($result)

return "Message target {$target} | Message is {$message}";

}

and this is working fine for me.

But when it comes to sending message to device, I am not getting any idea how to achieve this.Please help.

I am new to this please guide for the same.

Thank you in advance.

网友答案:
  1. You will want to add an MQTT client, such as Eclipse Paho, to your Android app.
  2. Connect to your MQTT broker from the Android app. If MQTT is not connected, you won't be able to receive those notifications. Thus, you should probably look into something like an Android service to ensure that it stays alive and then have robust code to ensure it handles disconnections.
  3. Subscribe to a client-specific channel, such as one identified by the device ID.
  4. Send messages to that device by publishing on that channel.

Something to be aware of: depending on your MQTT broker, messages that weren't received by a subscriber at the time they were sent may not be retained for very long, if at all.

相关阅读:
Top