Following the instructions on pub.dev for handling background messages doesn't compile for Android since the GeneratedPluginRegistrant.registerWith method takes a FlutterEngine object as a parameter. Using the PluginRegistry provided by the method throws the error
Type mismatch: inferred type is PluginRegistry? but FlutterEngine was expected
Please see the below code for reference
package ...
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) //Error thrown here
}
}
To reproduce
Steps to reproduce the behavior:
Expected behavior
Somehow a FlutterEngine object should be passed to the method in order to successfully build for Android
Fixed by replace with:
class MainApplication : FlutterApplication(), PluginRegistrantCallback {
override fun onCreate() {
super.onCreate()
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
}
}
Hi @Jacobsjj2
does the solution proposed by @hsangtini work for you?
thank you
This does allow me to compile, but I'm not sure if the background service is being registered correctly since I get the error
E/flutter (12123): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#initialized on channel plugins.flutter.io/firebase_messaging_background)
E/flutter (12123): #0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7)
E/flutter (12123): <asynchronous suspension>
E/flutter (12123): #1 _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:51:21)
E/flutter (12123): #2 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:45:6)
E/flutter (12123): #3 _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:24:32)
E/flutter (12123): #4 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
E/flutter (12123): #5 _rootRun (dart:async/zone.dart:1126:13)
E/flutter (12123): #6 _CustomZone.run (dart:async/zone.dart:1023:19)
E/flutter (12123): #7 _runZoned (dart:async/zone.dart:1518:10)
E/flutter (12123): #8 runZoned (dart:async/zone.dart:1502:12)
E/flutter (12123): #9 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
E/flutter (12123): #10 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
E/flutter (12123): #11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)
Doesn't the replacement simply return a Registrar object without registering anything?
This does allow me to compile, but I'm not sure if the background service is being registered correctly since I get the error
E/flutter (12123): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: MissingPluginException(No implementation found for method FcmDartService#initialized on channel plugins.flutter.io/firebase_messaging_background) E/flutter (12123): #0 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:319:7) E/flutter (12123): <asynchronous suspension> E/flutter (12123): #1 _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:51:21) E/flutter (12123): #2 _AsyncAwaitCompleter.start (dart:async-patch/async_patch.dart:45:6) E/flutter (12123): #3 _fcmSetupBackgroundChannel (package:firebase_messaging/firebase_messaging.dart:24:32) E/flutter (12123): #4 _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25) E/flutter (12123): #5 _rootRun (dart:async/zone.dart:1126:13) E/flutter (12123): #6 _CustomZone.run (dart:async/zone.dart:1023:19) E/flutter (12123): #7 _runZoned (dart:async/zone.dart:1518:10) E/flutter (12123): #8 runZoned (dart:async/zone.dart:1502:12) E/flutter (12123): #9 _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5) E/flutter (12123): #10 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19) E/flutter (12123): #11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)Doesn't the replacement simply return a
Registrarobject without registering anything?
Try this replace @Jacobsjj2
override fun registerWith(registry: PluginRegistry?) {
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
Sorry for the delay! @hsangtini's latest suggestion works. I guess it would also be necessary to update the documentation to that then?
I tried your solution and i am getting
Supertypes of the following classes cannot be resolved. Please make sure you have the required dependencies in the classpath:
class io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService, unresolved supertypes: com.google.firebase.messaging.FirebaseMessagingService
Fixed by replace with:
class MainApplication : FlutterApplication(), PluginRegistrantCallback { override fun onCreate() { super.onCreate() FlutterFirebaseMessagingService.setPluginRegistrant(this); } override fun registerWith(registry: PluginRegistry?) { registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"); } }
tq work for me
I also had this problem, I changed the "Application.kt" to the suggested one. and now it's compiling, I just haven't tested the notifications yet.
package br.gov.rj.riodasostras.riodasostrasapp
import android.os.Bundle
import io.flutter.app.FlutterActivity
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
//import com.google.firebase.messaging.FirebaseMessagingService
class Application : FlutterApplication() , PluginRegistrantCallback {
override fun onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith(registry: PluginRegistry?) {
//GeneratedPluginRegistrant.registerWith(registry);
//registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
}
}
package br.gov.rj.riodasostras.riodasostrasapp
import androidx.annotation.NonNull;
import io.flutter.embedding.android.FlutterActivity
import io.flutter.embedding.engine.FlutterEngine
import io.flutter.plugins.GeneratedPluginRegistrant
class MainActivity: FlutterActivity() {
override fun configureFlutterEngine(@NonNull flutterEngine: FlutterEngine) {
GeneratedPluginRegistrant.registerWith(flutterEngine);
}
}
flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 1.20.1, on Microsoft Windows [versão 10.0.18362.657], locale pt-BR)
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.0)
[√] Android Studio (version 3.6)
[√] IntelliJ IDEA Community Edition (version 2019.3)
[√] VS Code (version 1.45.1)
[√] Connected device (1 available)
• No issues found!
PS D:\MyDartProjects\riodasostrasapp> flutter doctor -v
[√] Flutter (Channel stable, 1.20.1, on Microsoft Windows [versão 10.0.18362.657], locale pt-BR)
• Flutter version 1.20.1 at C:\src\flutter
• Framework revision 2ae34518b8 (5 days ago), 2020-08-05 19:53:19 -0700
• Engine revision c8e3b94853
• Dart version 2.9.0
[√] Android toolchain - develop for Android devices (Android SDK version 29.0.0)
• Android SDK at C:\Users\isaque\AppData\Local\Android\sdk
• Platform android-R, build-tools 29.0.0
• Java binary at: C:\Program Files\Android\Android Studio\jre\bin\java
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
• All Android licenses accepted.
[√] Android Studio (version 3.6)
• Android Studio at C:\Program Files\Android\Android Studio
• Flutter plugin version 44.0.2
• Dart plugin version 192.7761
• Java version OpenJDK Runtime Environment (build 1.8.0_212-release-1586-b04)
[√] IntelliJ IDEA Community Edition (version 2019.3)
• IntelliJ at C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2019.3.4
• Flutter plugin version 45.0.2
• Dart plugin version 193.6911.31
[√] VS Code (version 1.45.1)
• VS Code at C:\Users\isaque\AppData\Local\Programs\Microsoft VS Code
• Flutter extension version 3.12.2
[√] Connected device (1 available)
• SM J120H (mobile) • 4200b9bf90139200 • android-arm • Android 5.1.1 (API 22)
• No issues found!
Ideally, the Readme and documentation should be updated
android/app/src/main/kotlin/com/example/my_app/Aplication.kt
package com.example.my_app
import android.os.Bundle
import io.flutter.app.FlutterActivity
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
//import com.google.firebase.messaging.FirebaseMessagingService
class Application : FlutterApplication() , PluginRegistrantCallback {
override fun onCreate() {
super.onCreate();
FlutterFirebaseMessagingService.setPluginRegistrant(this);
}
override fun registerWith( registry: PluginRegistry) {
GeneratedPluginRegistrant.registerWith(registry);
}
}
@insinfo
Hello
You mean.
rename Application.kt to MainActivity.kt?
How is your android/app/src/main/AndroidManifest.xml?
Do you import? both of them?
<activity
android:name=".MainActivity"
// something
<activity
android:name=".Application"
/>
Hey all 👋
As part of our roadmap (#2582) we've just shipped a complete rework of the firebase_messaging plugin that aims to solve this and many other issues. Additionally, messaging now supports v2 embedding.
If you can, please try out the dev release (see the migration guide for upgrading and for changes) and if you have any feedback then join in the discussion here.
Given the scope of the rework I'm going to go ahead and close this issue in favor of trying out the latest plugin.
Thanks everyone.
Most helpful comment
Fixed by replace with: