我的项目使用mqtt进行通知。需要实时通知。
我有这样的mqtt类
class Mqtt {
...
Mqtt._() {
mqttClient = MqttServerClient.withPort(server, clientIdentifier, port);
///Successful connection callback
mqttClient.onConnected = _onConnected;
///Disconnected callback
mqttClient.onDisconnected = _onDisconnected;
///Subscription success callback
mqttClient.onSubscribed = _onSubscribed;
///Subscription failure callback
mqttClient.onSubscribeFail = _onSubscribeFail;
}
static Mqtt getInstance() {
if (_instance == null) {
_instance = Mqtt._();
}
return _instance!;
}
///connection
connect() {
mqttClient.connect();
_log("connecting");
}
...
}
我这样打电话
Mqtt mqtt = Mqtt.getInstance();
mqtt.subTopic = 'NOTIFICATION/';
mqtt.connect();
但当应用程序停止时,mqtt会断开连接。
如何在后台服务颤振中调用mqtt客户端?