0

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");
  }
}
4

1 回答 1

0

后台进程(如您所提到的,在分离应用程序时仍应工作的代码)不会开箱即用,您需要一些特定的配置/设置来做到这一点。这是一篇解决这个问题的好文章:https ://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124

于 2021-05-15T11:49:29.500 回答