flutter_foreground_task
flutter_foreground_task copied to clipboard
MissingPluginException(No implementation found for method initialize on channel flutter_foreground_task/background
Been getting MissingPluginException(No implementation found for method initialize on channel flutter_foreground_task/background occasionally
I stop foreground tasks when the app resumes. But stopping and restarting it 2 or 3 times crsshes with the error above
Can you tell me which version of the plugin you are using? If you can also tell me the scenario in which the problem occurs, I think it will help me to solve the problem.
version 3.5.5
I get this upon starting and stopping my app
Error is dominating in Android 6 and shows sometimes on Android 11
V[/SurfaceView]()(21310): Layout: x=0 y=0 w=720 h=1280, frame=Rect(0, 0 - 720, 1280), this = io.flutter.embedding.android.FlutterSurfaceView{6b1bb96 V.E...... ........ 0,0-720,1280}
[log] 2022-02-07 11:33:46.333112 FlutterForegroundTask started
I[/TextToSpeech]()(21310): Sucessfully bound to com.google.android.tts
I[/TextToSpeech]()(21310): Connected to ComponentInfo{com.google.android.tts[/com.google.android.tts.service.GoogleTTSService]()}
I[/TextToSpeech]()(21310): Set up connection to ComponentInfo{com.google.android.tts[/com.google.android.tts.service.GoogleTTSService]()}
V[/SurfaceView]()(21310): Layout: x=0 y=0 w=720 h=1280, frame=Rect(0, 0 - 720, 1280), this = io.flutter.embedding.android.FlutterSurfaceView{6b1bb96 V.E...... ........ 0,0-720,1280}
[log] 2022-02-07 11:33:48.965741 FlutterForegroundTask stopped
E[/flutter]() (21310): [ERROR:flutter[/lib/ui/ui_dart_state.cc]()(209)] Unhandled Exception: MissingPluginException(No implementation found for method initialize on channel flutter_foreground_task[/background]())
E[/flutter]() (21310): #0 MethodChannel._invokeMethod
package:flutter/…/services/platform_channel.dart:175
E[/flutter]() (21310): <asynchronous suspension>
E[/flutter]() (21310):
Here is my code
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
Future<bool> startTask() async {
try {
if (await FlutterForegroundTask.isRunningService) return false;
await FlutterForegroundTask.init(
androidNotificationOptions: AndroidNotificationOptions(
channelId: 1,
channelName: "chanel",
channelDescription: 'desc',
channelImportance: NotificationChannelImportance.LOW,
priority: NotificationPriority.LOW,
iconData: const NotificationIconData(
resType: ResourceType.mipmap,
resPrefix: ResourcePrefix.ic,
name: 'launcher',
),
),
iosNotificationOptions: const IOSNotificationOptions(
showNotification: true,
playSound: false,
),
foregroundTaskOptions: ForegroundTaskOptions(
autoRunOnBoot: true,
),
printDevLog: true,
);
String title = "App";
String body = "App is online";
await FlutterForegroundTask.startService(
notificationTitle: title,
notificationText: body,
callback: startLocationCallback,
);
return true;
} on PlatformException catch (e) {
print("startTask PlatformException");
print(e);
return false;
} on MissingPluginException catch (e) {
print("startTask MissingPluginException");
print(e);
return false;
} catch (e) {
print(e);
return false;
}
}
Future<void> stopTask() async {
try {
if (!(await FlutterForegroundTask.isRunningService)) return;
await FlutterForegroundTask.stopService();
return;
} on PlatformException catch (e) {
print("stopTask PlatformException");
print(e.toString());
} on MissingPluginException catch (e) {
print("stopTask MissingPluginException");
print(e.toString());
} catch (e) {
print("stopTask error");
print(e.toString());
}
}
Using flutter rivepod and hooks. Am calling this in my useEffect in the build method
final appLifecycleState = useAppLifecycleState();
useEffect(() {
if (appLifecycleState == AppLifecycleState.inactive) {
_setUpBackgroundLocation();
return;
} else if (appLifecycleState == AppLifecycleState.resumed) {
ref.refresh(locPermissionProvider);
_stopForegroundTask();
return;
}
return null;
}, [appLifecycleState]);
Having this same issue, happened when I decided the startService() callback takes too long to run, so I moved the callback's code into the startService().then(()=> code here );
Yeah, my callback runs setTaskHandler() and it seems it might be because the startService() Future returns before the service is maybe actually started that running setTaskHandler() too soon caused this error.
startService() should probably only complete the future once it's actually been run.
Also, why does it take so long for the callback in startService() to be run? This means for about 2 seconds after showing the notification initially I could press the buttons and nothing will happen, because no task handler gets to be registered.
Just wondering what makes it take so long.
Solved this by delaying my TaskHandler's constructor code. Just added a Future.delayed() in the constructor and ran my code in there for now. The onStart callback proved unreliable, that's why I had to add it in the constructor.
its due to startCallback() function. Make startCallback() function as top-level function. Thats it, i will work as expected
Unfortunately not, my callback function is/was a top-level function. When it's not top-level, it isn't just delayed, it actually never runs.