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");
}
}
Background processes (code that should still work when the app is detached, as you mentioned) won't work out of the box, you need some specific configuration/setup to do that. Here is a great article resolving this problem: https://medium.com/flutter/executing-dart-in-the-background-with-flutter-plugins-and-geofencing-2b3e40a1a124
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.