Flutterfire: [firebase_messaging] Application.kt: Type mismatch: inferred type is PluginRegistry but FlutterEngine was expected

Created on 20 Dec 2019  Â·  61Comments  Â·  Source: FirebaseExtended/flutterfire

Problem

customerApplication/android/app/src/main/kotlin/com/application/customerApplication/Application.kt: (17, 48): Type mismatch: inferred type is PluginRegistry but FlutterEngine was expected

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

* Get more help at https://help.gradle.org

BUILD FAILED in 12s
Finished with error: Gradle task assembleDebug failed with exit code 1

I'm using the master repo version of firebase_messaging and I also done the fix suggested in last README.md.

My environment

[✓] Flutter (Channel stable, v1.12.13+hotfix.5, on Mac OS X 10.15.1 19B88, locale it-IT)
    • Flutter version 1.12.13+hotfix.5 at /Users/user/flutter
    • Framework revision 27321ebbad (9 days ago), 2019-12-10 18:15:01 -0800
    • Engine revision 2994f7e1e6
    • Dart version 2.7.0

[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /Users/user/Library/Android/sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • ANDROID_HOME = /Users/user/Library/Android/sdk
    • ANDROID_SDK_ROOT = /Users/user/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 11.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Xcode 11.3, Build version 11C29
    • CocoaPods version 1.8.4

[✓] Android Studio (version 3.5)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin version 42.1.1
    • Dart plugin version 191.8593
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[✓] VS Code (version 1.41.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.7.1

[✓] Connected device (1 available)
    • AOSP on IA Emulator • emulator-5554 • android-x86 • Android 9 (API 28) (emulator)

• No issues found!

Code:

MainActivity.kt

package com.application.customerApplication

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)
    }
}

Application.kt

package com.application.customerApplication

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)
    }
}
crowd messaging bug

Most helpful comment

There is a workaround mentioned in: #1613

Replace:

 override fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry) //Error thrown here
    }

To:

    override fun registerWith(registry: PluginRegistry?) {
  io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

Also temporary downgrade firebase_messaging to 5.1.6

  firebase_messaging: ^5.1.6

The build crash is solved. Don't know if "background messaging" works. If someone can confirm that?

All 61 comments

+1 same problem here!

I'm having the same issue with my Application.kt file after upgrading to Flutter v1.12.13 and firebase_message v6.0.9.

+1 same problem here!

Hi all,
I had the same issue with Application.java and was able to solve this by changing _AndroidManifest.xml_ from:

        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

to:

        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="1" />

Hi all,
I had the same issue with Application.java and was able to solve this by changing _AndroidManifest.xml_ from:

        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />

to:

        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="1" />

I tried but it doesn't solve. I had to back to activity V1.

There is a workaround mentioned in: #1613

Replace:

 override fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry) //Error thrown here
    }

To:

    override fun registerWith(registry: PluginRegistry?) {
  io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

Also temporary downgrade firebase_messaging to 5.1.6

  firebase_messaging: ^5.1.6

The build crash is solved. Don't know if "background messaging" works. If someone can confirm that?

I had the same problem in Application.kt:

package com.application.customerApplication

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)
    }
}

To solve the problem I now use in Application.tk:

package com.application.customerApplication

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

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
    }
}

I found this solution in here.

But there are still problems in background notifications, like:

E/flutter (22439): [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)

Hi @ManuelDiPietro
in case you updated an app originally created in v1.9
you may want to run
flutter clean
flutter pub cache repair
let me know if this is the case and if the issue persists
thank you

I had the same problem in Application.kt:

package com.application.customerApplication

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)
    }
}

To solve the problem I now use in Application.tk:

package com.application.customerApplication

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

class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    override fun registerWith(registry: PluginRegistry?) {
        registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin");
    }
}

I found this solution in here.

But there are still problems in background notifications, like:

E/flutter (22439): [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)

Yep, but in this way you cannot manage background message.

Hi @ManuelDiPietro
in case you updated an app originally created in v1.9
you may want to run
flutter clean
flutter pub cache repair
let me know if this is the case and if the issue persists
thank you

Yes, It works but using previous version of plugin ( <=6.0.6 ).

There is a workaround mentioned in: #1613

Replace:

 override fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry) //Error thrown here
    }

To:

    override fun registerWith(registry: PluginRegistry?) {
  io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

Also temporary downgrade firebase_messaging to 5.1.6

  firebase_messaging: ^5.1.6

The build crash is solved. Don't know if "background messaging" works. If someone can confirm that?

Hey, Thank You This Is Work For Me

any update on this? Same issue with a new project created with:

Flutter 1.12.13+hotfix.8 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 0b8abb4724 (2 weeks ago) • 2020-02-11 11:44:36 -0800
Engine • revision e1e6ced81d
Tools • Dart 2.7.0

I have been struggling with this issue for the last 4 days. To say that this is a massive issue is an understatement. Its making this plugin in android completely unusable for what I'm doing. The version I've been trying to get working is version 6.0.9. It's the latest version. If you need more information on this issue I'll be happy to provide it to you. Please let me know. It's kind of urgent I get this working. And the trouble is I have other plugins I am using and If I break flutters plugin system to get this plugin working it kind of defeats the benefit.

it's likely that you deleted the default metadata in AndroidManifest.xml
I experienced the same thing, but after I changed to the initial settings everything was fine.

@collinjackson @kroikie Any updates, please? This issue blocks new projects receive push notifications from Cloud Messaging.

It is currently impossible to handle background push notifications (what you need the most basically) on Android because of this problem. When can we expect a fix ?

It is not working with latest 6.0.13, also getting the same error.

Changing the registerWithmethod is not a fix, as it will crash if we define the onBackgroundMessage.

Push Notification is a very basic feature of any application, I was expecting it to be simple when I switched to Flutter + FCM but it looks like I was wrong...

I managed to fix the issue. The following are changes I had to make. It took an exhaustive search on the internet to figure this out. I wish the people associated with this project would just fix the docs with the correct information. The other piece of the for this is that when you set your intent you use the URL on the firebase console. NOT the url you encoded. This was one little line on an obscure page in the firebase dock. It took me weeks to find it. Cheers hope this helps.

Add the following to the AndroidManifest.xml


<intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>


<intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="yourfirebaseconsole.page.link" android:scheme="https"/>
  </intent-filter>

FirebaseCloudMessagingPluginRegistrant.kt



package com.your.packagename

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin


object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
        val key: String = FirebaseCloudMessagingPluginRegistrant::class.java.getCanonicalName()
        if (registry.hasPlugin(key)) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}

MainActivity.kt


package com.your.packagename

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);
 //
 //   }
}

Application.kt


package com.your.packagename


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


class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

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

And this is a bug with the documentation in MYHO making it a bug with the plugin.

@pneves001 but you still don't get background notifications ..

No I got it working. But background notifications happen when the app is closed. I also added this to the main.dart file of my app Some of it is in the main function

import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:platform/platform.dart';

final FirebaseMessaging _firebaseMessaging = FirebaseMessaging();

Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
   if (message.containsKey('data')) {
     // Handle data message
     final dynamic data = message['data'];
   }

   if (message.containsKey('notification')) {
     // Handle notification message
     final dynamic notification = message['notification'];

     print(notification.toString()); 
   }

void iosPermissions()
  {
    _firebaseMessaging.onIosSettingsRegistered
      .listen((IosNotificationSettings settings)
      {
      print("Settings registered: $settings");
      });

    _firebaseMessaging.requestNotificationPermissions(
      IosNotificationSettings(sound: true, badge: true, alert: true)
  );

  }

void main() async {
 Platform platform = LocalPlatform(); 

  if (platform.isIOS == true) iosPermissions(); 

_firebaseMessaging.configure(
          onMessage: (Map<String, dynamic> message) async 
            {
            print("####  onMessage: $message");

            },
          onBackgroundMessage: (platform.isIOS == true) ? null : myBackgroundMessageHandler,
          onLaunch: (Map<String, dynamic> message) async 
            {
            print("onLaunch: $message");


            },
          onResume: (Map<String, dynamic> message) async 
            {
            print("onResume: $message");


            },
        );

  _firebaseMessaging.getToken().then((token)
          {
          print(token);

          }, 
      onError: (error)
          {
          print(error); 

          });

}

@pneves001 that is weird because in my case background messages work if i set the android embedding to version 1 and switch back to the old kotlin implementation...

but with embedding v2 and your new implementation it's only working if the app is in the foreground.

Oh I forgot to mention. You need to change the application name to point to your new Application at the top of your AndroidManifest.xml.

    <application
        android:name=".Application"
        android:label="yourappname"
        android:icon="@mipmap/ic_launcher">

I know I did that. Otherwise I wouldn't have gotten it to work with v1 embedding

I'm just checking my app to see if there is anything else I neglected to mention.

Are you sending data messages or notification messages? Because I'm using data messages ..

This plugin is so finicky it took me a long time to get this to work right. One thing I had to do is store the message in local storage so that I can pass it to other widgets in separate files. Dart likes to keep everything in different files separate. It creates a contained space in every file. So I resorted to storing the messages in local storage.

Here is the version of the plugin I'm using:

firebase_messaging: ^6.0.12

Just text. No additional information.

On Sat, Mar 28, 2020 at 2:55 PM Lazy Llama notifications@github.com wrote:

Are you sending data messages or notification messages? Because I'm using
data messages ..

—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/FirebaseExtended/flutterfire/issues/1684#issuecomment-605524716,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAJGTQUJQQIWFY6DW7LNQWLRJZW6NANCNFSM4J52JVEQ
.

--

Philip Neves - Director/CEO Neves Software Inc. *
*Website:
https://www.nevessoftware.com
Email: philip.[email protected]
twitter: nevessoftware
Cell: 778 232 9847
Office: 604 259 2645

And btw... I found this information in a stack overflow article. So it would be nice to have complete documentation that we needed to make everything work.

BTW... I believe I'm using V2.

Just text. No additional information.
…
On Sat, Mar 28, 2020 at 2:55 PM Lazy Llama @.*> wrote: Are you sending data messages or notification messages? Because I'm using data messages .. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub <#1684 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAJGTQUJQQIWFY6DW7LNQWLRJZW6NANCNFSM4J52JVEQ .
-- Philip Neves - Director/CEO Neves Software Inc. * *Website: https://www.nevessoftware.com Email: philip.[email protected] twitter: nevessoftware Cell: 778 232 9847 Office: 604 259 2645

What do you mean with "just text"?

Are you doing

{
      "to": "/topics/mytopic",
      "notification":{
          "title":"Portugal vs. Denmark",
          "body":"great match!"
      }
}

or

{
      "to": "/topics/mytopic",
      "data":{
          "property":"Portugal vs. Denmark",
          "property3":"great match!"
      }
}

Well let me put it this way. When it comes to receiving messages out right and displaying them in app. I'm using websockets. I use firebase notifications just for text and to notify my users that there is a message that came in. The plugin is too finicky for that. IMO

Okay I found the issue... I am using the local notifications plugin and it wasn't able to execute in the background... So I had to do write a FlutterLocalNotificationPluginRegistrant ... now everything is working fine. Thank you.

I got the solution from here:
https://stackoverflow.com/questions/53572110/flutter-push-notifications-even-if-the-app-is-closed

Managed to get a ticket with the Google team to provide a Kotlin based example for https://pub.dev/packages/firebase_messaging at https://github.com/FirebaseExtended/flutterfire/issues/2311

Reading through this issue, also I have no application.kt, just MainActivity.kt so this whole thing is a mess.

I expect the documentation to clarify what needs to be done for Kotlin when that is the new default for Flutter projects now.

I expect the documentation to clarify what needs to be done for Kotlin when that is the new default for Flutter projects now.

No you need to create the Application.kt yourself. Your actually replacing the MainActivity.kt file with that.

@pneves001, After doing these workarounds. Do we not use GeneratedPluginRegistrant.java anymore?

I have this in the file and I'm not sure what's causing the crash after installation during debugging. There's no error whatsoever on my side.

public final class GeneratedPluginRegistrant {
  public static void registerWith(@NonNull FlutterEngine flutterEngine) {
    ShimPluginRegistry shimPluginRegistry = new ShimPluginRegistry(flutterEngine);
      de.mintware.barcode_scan.BarcodeScanPlugin.registerWith(shimPluginRegistry.registrarFor("de.mintware.barcode_scan.BarcodeScanPlugin"));
      io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin.registerWith(shimPluginRegistry.registrarFor("io.flutter.plugins.firebase.cloudfirestore.CloudFirestorePlugin"));
    flutterEngine.getPlugins().add(new com.mr.flutter.plugin.filepicker.FilePickerPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.firebaseauth.FirebaseAuthPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.firebase.core.FirebaseCorePlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.firebase.storage.FirebaseStoragePlugin());
      com.roughike.facebooklogin.facebooklogin.FacebookLoginPlugin.registerWith(shimPluginRegistry.registrarFor("com.roughike.facebooklogin.facebooklogin.FacebookLoginPlugin"));
      io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin.registerWith(shimPluginRegistry.registrarFor("io.flutter.plugins.flutter_plugin_android_lifecycle.FlutterAndroidLifecyclePlugin"));
      com.noeatsleepdev.geoflutterfire.GeoflutterfirePlugin.registerWith(shimPluginRegistry.registrarFor("com.noeatsleepdev.geoflutterfire.GeoflutterfirePlugin"));
    flutterEngine.getPlugins().add(new io.flutter.plugins.googlemaps.GoogleMapsPlugin());
      io.flutter.plugins.googlesignin.GoogleSignInPlugin.registerWith(shimPluginRegistry.registrarFor("io.flutter.plugins.googlesignin.GoogleSignInPlugin"));
    flutterEngine.getPlugins().add(new io.flutter.plugins.imagepicker.ImagePickerPlugin());
    flutterEngine.getPlugins().add(new com.lyokone.location.LocationPlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.pathprovider.PathProviderPlugin());
    flutterEngine.getPlugins().add(new com.tekartik.sqflite.SqflitePlugin());
    flutterEngine.getPlugins().add(new io.flutter.plugins.urllauncher.UrlLauncherPlugin());
  }

Any update on this? I'm getting the same error as the OP had 4 months ago.

Me too.

@mnorhamizan This is what I get too. :(
I followed @pneves001 instructions and got the app crashing upon launch.

Flutter Channel master, 1.18.0-9.0.pre.122
firebase_messaging: 6.0.13

I managed to fix the issue. The following are changes I had to make. It took an exhaustive search on the internet to figure this out. I wish the people associated with this project would just fix the docs with the correct information. The other piece of the for this is that when you set your intent you use the URL on the firebase console. NOT the url you encoded. This was one little line on an obscure page in the firebase dock. It took me weeks to find it. Cheers hope this helps.

Add the following to the AndroidManifest.xml


<intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>


<intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="yourfirebaseconsole.page.link" android:scheme="https"/>
  </intent-filter>

FirebaseCloudMessagingPluginRegistrant.kt



package com.your.packagename

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin


object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
        val key: String = FirebaseCloudMessagingPluginRegistrant::class.java.getCanonicalName()
        if (registry.hasPlugin(key)) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}

MainActivity.kt


package com.your.packagename

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);
 //
 //   }
}

Application.kt


package com.your.packagename


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


class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

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

@pneves001 Why did you commented out the configureFlutterEngine() from MainActivity.kt? It is important to keep that function in order to register other types of plugins.

The FlutterApplication() base class already implements the configureFlutterEngine(). So you don't need to do it again.

@mnorhamizan This is what I get too. :(
I followed @pneves001 instructions and got the app crashing upon launch.

Flutter Channel master, 1.18.0-9.0.pre.122
firebase_messaging: 6.0.13

Hi @Aurora12 , did you gix the crash on launch? I am having the same issue, can't get around it.

Same issue here.

I'd like to second this comment:

I wish the people associated with this project would just fix the docs with the correct information.

The Readme should have instructions for Kotlin since it is the default for new Flutter projects.

I'd like to second this comment:

I wish the people associated with this project would just fix the docs with the correct information.

The Readme should have instructions for Kotlin since it is the default for new Flutter projects.

Yes I'm agree

Can we get any attention to this problem from the team? The documentation is all messed up.

This is what I am still getting:

e: ...Application.kt: (10, 7): 'onCreate' hides member of supertype 'FlutterApplication' and needs 'override' modifier
e: .../Application.kt: (14, 7): 'registerWith' hides member of supertype 'PluginRegistrantCallback' and needs 'override' modifier
e: .../Application.kt: (15, 44): Type mismatch: inferred type is PluginRegistry but FlutterEngine was expected
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> Compilation error. See log for more details
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
* Get more help at https://help.gradle.org
BUILD FAILED in 42s
Exception: Gradle task assembleDebug failed with exit code 1
Exited (sigterm)

I still have this same issue, seems like this new set up complicates things... I guess I might have to put off notifications on my app for the time being... are there any known alternatives?

+1 same problem here!

Still no Kotlin instructions on the latest firebase_messaging 7.0.0 release: https://pub.dev/packages/firebase_messaging 😢

This is what I have in Application.kt. Works for me!

package com.your.packagename

import io.flutter.app.FlutterApplication
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin
import io.flutter.view.FlutterMain
import io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService

class Application : FlutterApplication(), PluginRegistrantCallback {

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

    override fun registerWith(registry: PluginRegistry?) {
        if (!registry!!.hasPlugin("io.flutter.plugins.firebasemessaging")) {
            FirebaseMessagingPlugin.registerWith(registry!!.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        }
    }
}

MainActivity.kt is default.

package com.your.packagename

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);
    }
}

Based on advice found in #1775, this is what I had for Application.kt:

package com.your.packagename

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

public class Application : FlutterApplication(), PluginRegistrantCallback {
  override fun onCreate() {
    super.onCreate()
    FlutterFirebaseMessagingService.setPluginRegistrant(this)
  }

  override fun registerWith(registry: PluginRegistry) {
    registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin")
  }
}

What is the significance of adding FlutterMain.startInitialization(this) to onCreate?

Can you explain why your registerWith is better? Why is the guard necessary?

Finally, why do we need to tweak Application & MainActivity at all? Why can't the initialization be contained and completely managed by the dependency?

Hello guys, how to configure it to embedding v2? Here I'm having this trouble:

Fatal Exception: java.lang.RuntimeException Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set. android.app.ActivityThread.handleCreateService

I am also getting the same error, Type mismatch: inferred type is PluginRegistry but FlutterEngine was expected

tried above all solutions. Nothing worked.

Can anyone show me workaround to fix it?

I am using following versions,
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.google.gms:google-services:4.3.2'

implementation 'com.google.firebase:firebase-messaging:20.2.4'

Following solution works for me as well,

https://stackoverflow.com/a/63742287/5715935

There is a workaround mentioned in: #1613

Replace:

 override fun registerWith(registry: PluginRegistry?) {
        GeneratedPluginRegistrant.registerWith(registry) //Error thrown here
    }

To:

    override fun registerWith(registry: PluginRegistry?) {
  io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }

Also temporary downgrade firebase_messaging to 5.1.6

  firebase_messaging: ^5.1.6

The build crash is solved. Don't know if "background messaging" works. If someone can confirm that?

It's work for me,

flutter version Channel beta, 1.22.0-12.1.pre, on Linux, locale en_US.UTF-8
Dart version 2.10.0 (build 2.10.0-110.3.beta)

dependencies:
  firebase_analytics: 6.0.0
  firebase_remote_config: 0.4.0
  firebase_crashlytics: 0.1.4+1
  firebase_dynamic_links: 0.6.0
  firebase_messaging: 7.0.0

solved the problem. I used java, you can also use jeans.

MainActivity.kt :

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);

      }
}


FirebaseCloudMessagingPluginRegistrant.java :

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;

public final class FirebaseCloudMessagingPluginRegistrant{
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }

        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

Application.java :

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

public class Application extends FlutterApplication implements PluginRegistrantCallback {

    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
    }
}

AndroidManifest.xml :


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="//package name">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:name=".Application"
        android:label="myapp"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>


        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

solved the problem. I used java, you can also use jeans.

MainActivity.kt :

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);

      }
}

FirebaseCloudMessagingPluginRegistrant.java :

import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;

public final class FirebaseCloudMessagingPluginRegistrant{
    public static void registerWith(PluginRegistry registry) {
        if (alreadyRegisteredWith(registry)) {
            return;
        }

        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
        FlutterLocalNotificationsPlugin.registerWith(registry.registrarFor("com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin"));
    }

    private static boolean alreadyRegisteredWith(PluginRegistry registry) {
        final String key = FirebaseCloudMessagingPluginRegistrant.class.getCanonicalName();
        if (registry.hasPlugin(key)) {
            return true;
        }
        registry.registrarFor(key);
        return false;
    }
}

Application.java :

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

public class Application extends FlutterApplication implements PluginRegistrantCallback {

    @Override
    public void onCreate() {
        super.onCreate();
        FlutterFirebaseMessagingService.setPluginRegistrant(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        FirebaseCloudMessagingPluginRegistrant.registerWith(registry);
    }
}

AndroidManifest.xml :


<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="//package name">
    <!-- io.flutter.app.FlutterApplication is an android.app.Application that
         calls FlutterMain.startInitialization(this); in its onCreate method.
         In most cases you can leave this as-is, but you if you want to provide
         additional functionality it is fine to subclass or reimplement
         FlutterApplication and put your custom class here. -->
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED"/>
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />
    <application
        android:name=".Application"
        android:label="myapp"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <!-- Displays an Android View that continues showing the launch screen
                 Drawable until Flutter paints its first frame, then this splash
                 screen fades out. A splash screen is useful to avoid any visual
                 gap between the end of Android's launch screen and the painting of
                 Flutter's first frame. -->
            <meta-data
              android:name="io.flutter.embedding.android.SplashScreenDrawable"
              android:resource="@drawable/launch_background"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
            <intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>

        </activity>


        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
        <receiver android:name="com.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            </intent-filter>
        </receiver>
    </application>
</manifest>

What is om.dexterous.flutterlocalnotifications.ScheduledNotificationBootReceiver?

Do change in your app-level Gradle :

compileSdkVersion 30
targetSdkVersion 30

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.

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.

I managed to fix the issue. The following are changes I had to make. It took an exhaustive search on the internet to figure this out. I wish the people associated with this project would just fix the docs with the correct information. The other piece of the for this is that when you set your intent you use the URL on the firebase console. NOT the url you encoded. This was one little line on an obscure page in the firebase dock. It took me weeks to find it. Cheers hope this helps.

Add the following to the AndroidManifest.xml


<intent-filter>
                <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                <category android:name="android.intent.category.DEFAULT" />
  </intent-filter>


<intent-filter android:autoVerify="true">
                <action android:name="android.intent.action.VIEW"/>
                <category android:name="android.intent.category.DEFAULT"/>
                <category android:name="android.intent.category.BROWSABLE"/>
                <data android:host="yourfirebaseconsole.page.link" android:scheme="https"/>
  </intent-filter>

FirebaseCloudMessagingPluginRegistrant.kt



package com.your.packagename

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin


object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry) {
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
        val key: String = FirebaseCloudMessagingPluginRegistrant::class.java.getCanonicalName()
        if (registry.hasPlugin(key)) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}

MainActivity.kt


package com.your.packagename

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);
 //
 //   }
}

Application.kt


package com.your.packagename


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


class Application : FlutterApplication(), PluginRegistrantCallback {
    override fun onCreate() {
        super.onCreate()
        FlutterFirebaseMessagingService.setPluginRegistrant(this)
    }

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

For those who have used this solution in the past and upgrade the targetSdkVersion of your build.gradle file to 29 and are receiving an error of FirebaseCloudMessagingPluginRegistrant.kt: (18, 27): Type mismatch: inferred type is String? but String was expected.

All you need to do is change your FirebaseCloudMessagingPluginRegistrant.alreadyRegisteredWith file from accepting a String to accepting a String? instead.

FirebaseCloudMessagingPluginRegistrant.kt

package com.your.packagename

import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin


object FirebaseCloudMessagingPluginRegistrant {
    fun registerWith(registry: PluginRegistry) { 
        if (alreadyRegisteredWith(registry)) {
            return
        }
        FirebaseMessagingPlugin.registerWith(registry.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"))
    }

    private fun alreadyRegisteredWith(registry: PluginRegistry): Boolean {
        val key: String? = FirebaseCloudMessagingPluginRegistrant::class.java.getCanonicalName()
        if (registry.hasPlugin(key)) {
            return true
        }
        registry.registrarFor(key)
        return false
    }
}

Was this page helpful?
0 / 5 - 0 ratings