Solvedflutterfire [firebase_messaging] <Background push notification handling - no implementation found for method FcmDartService#initialized>

Occurrence of MissingPluginException(No implementation found for method FcmDartService#initialized)

Recently I have updated FCM plugin for flutter to 5.1.5 version that supports
handling of background push notifications. I have followed the instructions
to implement and incorporate it into my application, but handling still fails.
Every time when I try to interact with a push notification on device, from onResume or onLaunch state, plugin fails and I receive this error:

> E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): Failed to handle method call
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): java.lang.IllegalStateException: registrar.activity() must not be null
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at com.apptreesoftware.barcodescan.BarcodeScanPlugin$Companion.registerWith(BarcodeScanPlugin.kt:19)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at com.apptreesoftware.barcodescan.BarcodeScanPlugin.registerWith(Unknown Source:2)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at io.flutter.plugins.GeneratedPluginRegistrant.registerWith(GeneratedPluginRegistrant.java:28)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at example.app.myapp.Application.registerWith(Application.kt:18)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService.startBackgroundIsolate(FlutterFirebaseMessagingService.java:164)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.onMethodCall(FirebaseMessagingPlugin.java:133)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at io.flutter.plugin.common.MethodChannel$IncomingMethodCallHandler.onMessage(MethodChannel.java:222)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at io.flutter.embedding.engine.dart.DartMessenger.handleMessageFromDart(DartMessenger.java:96)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at io.flutter.embedding.engine.FlutterJNI.handlePlatformMessage(FlutterJNI.java:643)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at android.os.MessageQueue.nativePollOnce(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at android.os.MessageQueue.next(MessageQueue.java:326)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at android.os.Looper.loop(Looper.java:160)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at android.app.ActivityThread.main(ActivityThread.java:6863)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at java.lang.reflect.Method.invoke(Native Method)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
E/MethodChannel#plugins.flutter.io/firebase_messaging(19582): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
E/flutter (19582): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, registrar.activity() must not be null, null)
E/flutter (19582): #0      StandardMethodCodec.decodeEnvelope (package:flutter/src/services/message_codecs.dart:564:7)
E/flutter (19582): #1      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:316:33)
E/flutter (19582): <asynchronous suspension>
E/flutter (19582): #2      FirebaseMessaging.configure (package:firebase_messaging/firebase_messaging.dart:118:16)
E/flutter (19582): #3      PushNotificationReceiver.handleReceivedPushNotification (package:panda/utils/push_notification_receiver.dart:57:26)
E/flutter (19582): #4      _MainNavigationState.build (package:panda/navigation/main_navigation.dart:116:30)
E/flutter (19582): #5      StatefulElement.build (package:flutter/src/widgets/framework.dart:4012:27)
E/flutter (19582): #6      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3924:15)
E/flutter (19582): #7      Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (19582): #8      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (19582): #9      StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11)
E/flutter (19582): #10     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (19582): #11     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #12     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #13     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (19582): #14     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (19582): #15     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (19582): #16     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (19582): #17     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #18     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #19     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (19582): #20     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #21     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #22     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (19582): #23     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #24     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #25     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (19582): #26     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #27     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #28     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (19582): #29     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (19582): #30     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (19582): #31     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11)
E/flutter (19582): #32     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (19582): #33     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #34     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #35     ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:3935:16)
E/flutter (19582): #36     Element.rebuild (package:flutter/src/widgets/framework.dart:3721:5)
E/flutter (19582): #37     ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:3907:5)
E/flutter (19582): #38     StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4053:11)
E/flutter (19582): #39     ComponentElement.mount (package:flutter/src/widgets/framework.dart:3902:5)
E/flutter (19582): #40     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #41     Element.updateChild (package:flutter/src/widgets/framework.dart:2887:12)
E/flutter (19582): #42     SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5092:14)
E/flutter (19582): #43     Element.inflateWidget (package:flutter/src/widgets/framework.dart:3084:14)
E/flutter (19582): #44     Element.updateChild (package:flutter/src/widget
E/flutter (19582): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] **Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#initialized on channel plugins.flutter.io/firebase_messaging_background)**
E/flutter (19582): #0      MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:314:7)
E/flutter (19582): <asynchronous suspension>
E/flutter (19582): #1      _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:50:21)
E/flutter (19582): #2      _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:49:6)
E/flutter (19582): #3      _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:23:32)
E/flutter (19582): #4      _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:216:25)
E/flutter (19582): #5      _rootRun (dart:async/zone.dart:1124:13)
E/flutter (19582): #6      _CustomZone.run (dart:async/zone.dart:1021:19)
E/flutter (19582): #7      _runZoned (dart:async/zone.dart:1516:10)
E/flutter (19582): #8      runZoned (dart:async/zone.dart:1500:12)
E/flutter (19582): #9      _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:208:5)
E/flutter (19582): #10     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:301:19)
E/flutter (19582): #11     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)
E/flutter (19582): 

Here are my Application.kt and MainActivity kotlin classes:

Application.kt

package example.app.myapp

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.GeneratedPluginRegistrant
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry) {
    GeneratedPluginRegistrant.registerWith(registry)
  }
}

MainActivity.kt:

package example.app.myapp

import android.os.Bundle

import io.flutter.plugins.GeneratedPluginRegistrant
import android.view.WindowManager.LayoutParams
import io.flutter.app.FlutterFragmentActivity

class MainActivity: FlutterFragmentActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    GeneratedPluginRegistrant.registerWith(this)
    getWindow().addFlags(LayoutParams.FLAG_SECURE)
  }
}
30 Answers

βœ”οΈAccepted Answer

So in the new firebase_messaging version, support for background messages was added. Actually, if you don't add background message handler, it failed to work on Android for me, so I added handler, according to docs:

_fcm.configure(
	...
	onBackgroundMessage: myBackgroundMessageHandler,
	...
)

Now, if you run code with background handler on iOS, it explodes with the error in this issue. So the solution is to set the handler to null for iOS:

_fcm.configure(
	...
	onBackgroundMessage: Platform.isIOS ? null : myBackgroundMessageHandler,
	...
)

This solved the problem for me.

Other Answers:

Same error for me...

Ok, it appears that that the issue was with notification data payload message, not with the
code implementation itself.

I just added

"data" : {
     "body" : "example body data",
     "title" : "example title data",
     "click_action": "FLUTTER_NOTIFICATION_CLICK",
 }

to notification message payload and now it works for both onResume and onLaunch states.

I've Managed to fix the no implantation error's by following these steps as mentioned here.

https://stackoverflow.com/a/55834355/12241836

  1. Move Application.kt / .java to the same folder as MainActivity.kt / .java
  2. Change android:name in AndroidMainfest.xml as done
  3. flutter clean and rebuild. (This is important to do)

Also upon further reading i discovered that OnbackgroundMessage is fired only when the fcm notification is of a DATA TYPE

{
   "to": "USER_TOKEN_HERE",
   "data": {
      "uid": 123
   },
   "priority": "high"
}

Another important is thing to keep in mind is to have the OnMessageHandler be a top-level function, i.e, not be part of any class.

This PR has a lot of the talked upon.

hi @woutergoossens , could you share your onbackground handler function to show notification in background? and what widget that you used to make notification appears? and what version of your firebase messaging? i tried to find to find the way for a mont but i got nothing

More Issues: