app.LoadPlugin( "MQTT" ); //Note: When running background services on newer Huawei phones, you //may need to go into the Android battery saving settings and enable //DroidScript to run in the background if you want background //notifications to work when the phone is locked. //Called when service is started. function OnStart() { //Force service to foreground on newer devices (required). if( app.GetBuildNum() > 25 ) { app.SetInForeground( "Waiting for notification..."); } // my private mosquitto configuration var mosquitto_options = { servers:[{ host: "bukys.eu", port: 9004 }], keepalive: 1800, username: "myUser", password: "myPass", } // Unique identifier for each device if(app.IsAPK()) mosquitto_options.clientId = "Android-" + app.GetDeviceId() else mosquitto_options.clientId = "IDE-" + app.GetDeviceId() // Public mosquitto connection // client = mqtt.connect( 'ws://broker.hivemq.com:8000/mqtt' ); client = mqtt.connect( mosquitto_options ); client.on( 'connect', ()=>{ client.subscribe( 'notification', {qos:2} ); // send client id as message to topic "clients" client.publish("clients", mosquitto_options.clientId) }); // create Android notification when MQTT message arrives to subscribed topic client.on( 'message', (topic, message)=>{ not = app.CreateNotification(); not.SetMessage( "ticker", "MQTT "+topic, "Msg: " + message, "Message: "+ message ); not.Notify( message ); }); setInterval(()=>{ client.publish("heartbeat", new Date().toLocaleString()) }, 60000) }