When the app is in the background and receives notifications through the Firebase Cloud Messaging (FCM) system, the onBackgroundMessage handler in the app is invoked to manage these notifications. In this scenario, I need to trigger a native method from Flutter using a method channel to execute a specific action on the native side. However, invoking the method directly from the onBackgroundMessage handler is not feasible, as it is a top-level function as per Flutter’s FCM requirements.
The error I’m receiving is:
FlutterFire Messaging: An error occurred in your background messaging handler:
MissingPluginException(No implementation found for method methodName on channel com.example.channel_name)
And this is how I can it for reference:
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
var MethodChannel platformChannel =
MethodChannel('com.example.channel_name');
WidgetsFlutterBinding.ensureInitialized();
final int result = await platformChannel.invokeMethod('methodName'); // Error is here
}
I’ve tried the same on non-top-level function and it worked perfectly so the functionality itself has no issues.