改插件暂时未支持消息离线模式,至于后面版本会不会修复,那就不清楚了,首先简单安装一下:
npm库:https://www.npmjs.com/package/@luckfellow/react-native-mqtt
GitHub:https://github.com/luckfellow/react-native-mqtt
npm i @luckfellow/react-native-mqtt
或者
yarn add @luckfellow/react-native-mqtt
link
react-native link react-native-mqtt
如果link失败可以上GitHub上参考流程
首先支持MQTT离线消息则需要设置CleanSession=false,那么就在相关的地方添加该参数设置:
打开文件路径:@luckfellow/react-native-mqtt/android/src/main/java/com/tuanpm/RCTMqtt/RCTMqtt.java
在createClient函数中mqttOptions = new MqttConnectOptions();后添加 mqttOptions.setCleanSession(false);
private void createClient(@NonNull final ReadableMap params) {
...
mqttOptions = new MqttConnectOptions();
mqttOptions.setCleanSession(false);
...
}
重新运行:react-native run-android就OK了