I am trying to send message using methodChannel from MainActivity to dart code and everything is ok when the app is open or is in the recent apps,but when I deattach the app, dart code couldn't receive anything from method Channel, Please can anyone help me what should I do to keep dart code receving messages even if the app is not working. Here is my code.
MainActivity.java
public class MainActivity extends FlutterActivity{
void callFlutter(){
//binaryMessenger I construct is in onCreate
methodChannel=new MethodChannel(binaryMessenger,Channel);
methodChannel.invokeMethod("didRecieveTranscript","Helllo");
}
}
main.dart
main(){
WidgetsFlutterBinding.ensureInitialized();
const MethodChannel channel = MethodChannel('com.todomessages/send');
channel.setMethodCallHandler(__didRecieveTranscript);
}
Future<void> __didRecieveTranscript(MethodCall call){
ToDoDB _dbHelper=new ToDoDB();
switch(call.method){
case "didRecieveTranscript":
FlutterLogs.logInfo("Message", "Returned Call from 12 service", "");
_dbHelper.getToDoList(TO_DO_Today_For_Home_Page).then((lst) {
if (lst.length > 0) {
FlutterLogs.logInfo("Message", "Returned Call from 12 service", "There is data");
startService();
}
else
FlutterLogs.logInfo("Message", "Returned Call from 12 service", "There is no data");
});
break;
}
}
void startService() async {
if (Platform.isAndroid) {
var methodChannel = MethodChannel("com.to_do_list.messages");
await methodChannel.invokeMethod("startService");
}
}