改插件暫時未支持消息離線模式,至於後面版本會不會修復,那就不清楚了,首先簡單安裝一下:
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了