Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
manuuuel opened this issue Dec 20, 2019 · 61 comments
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging type: bug Something isn't working

Comments

@manuuuel
Copy link

manuuuel commented Dec 20, 2019

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)
    }
}
@manuuuel manuuuel added the type: bug Something isn't working label Dec 20, 2019
@manuuuel manuuuel changed the title [firebase_messaging] customerApplication/android/app/src/main/kotlin/com/application/customerApplication/Application.kt: (17, 48): Type mismatch: inferred type is PluginRegistry but FlutterEngine was expected [firebase_messaging] Application.kt: Type mismatch: inferred type is PluginRegistry but FlutterEngine was expected Dec 20, 2019
@marcoparioli
Copy link

+1 same problem here!

@Jeanlo
Copy link

Jeanlo commented Dec 20, 2019

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

@daadu
Copy link
Contributor

daadu commented Dec 20, 2019

+1 same problem here!

@NateNjuguna
Copy link

NateNjuguna commented Dec 20, 2019

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" />

@manuuuel
Copy link
Author

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.

@daadu
Copy link
Contributor

daadu commented Dec 21, 2019

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?

@idevol
Copy link

idevol commented Dec 23, 2019

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)

@iapicca iapicca added blocked: customer-response Waiting for customer response, e.g. more information was requested. impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) and removed blocked: customer-response Waiting for customer response, e.g. more information was requested. labels Dec 23, 2019
@iapicca
Copy link

iapicca commented Dec 23, 2019

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

@iapicca iapicca added the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Dec 23, 2019
@manuuuel
Copy link
Author

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.

@manuuuel
Copy link
Author

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

@4RSIM3R
Copy link

4RSIM3R commented Feb 22, 2020

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

@NeXT405
Copy link

NeXT405 commented Feb 26, 2020

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

@iapicca iapicca removed the blocked: customer-response Waiting for customer response, e.g. more information was requested. label Feb 26, 2020
@pneves001
Copy link

pneves001 commented Feb 27, 2020

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.

@odysahe
Copy link

odysahe commented Feb 29, 2020

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.

@peternagy1332
Copy link

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

@schankam
Copy link

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...

@pneves001
Copy link

pneves001 commented Mar 28, 2020

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
Copy link

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

@LasseRosenow
Copy link

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

@pneves001
Copy link

pneves001 commented Mar 28, 2020

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

}

@LasseRosenow
Copy link

@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.

@mnorhamizan
Copy link

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

@bartekpacia
Copy link

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

@krunduev

This comment has been minimized.

@Aurora12
Copy link

Aurora12 commented May 5, 2020

@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

@firatcetiner
Copy link

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.

@pneves001
Copy link

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

@aidandavis
Copy link

@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.

@yanivshaked

This comment has been minimized.

@awhitford
Copy link
Contributor

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.

@akoua
Copy link

akoua commented Jul 17, 2020

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

@dmiedev

This comment has been minimized.

@Xbobo5530
Copy link

Xbobo5530 commented Aug 15, 2020

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?

@khacnha
Copy link

khacnha commented Aug 19, 2020

+1 same problem here!

@awhitford
Copy link
Contributor

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

@Aurora12
Copy link

Aurora12 commented Aug 26, 2020

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

@awhitford
Copy link
Contributor

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?

@juzejunior
Copy link

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

@MungaraJay
Copy link

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'

@MungaraJay
Copy link

Following solution works for me as well,

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

@tvc12
Copy link

tvc12 commented Sep 19, 2020

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

@gulmensedat
Copy link

gulmensedat commented Sep 28, 2020

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>

@sanjaymajoka
Copy link

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?

@shahnawazahmed88
Copy link

Do change in your app-level Gradle :

compileSdkVersion 30
targetSdkVersion 30

@Salakar
Copy link
Member

Salakar commented Nov 5, 2020

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.

@Salakar Salakar closed this as completed Nov 5, 2020
@supposedlysam-bb
Copy link

supposedlysam-bb commented Nov 17, 2020

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

@firebase firebase locked and limited conversation to collaborators Dec 6, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
impact: crowd Affects many people, though not necessarily a specific customer with an assigned label. (P2) plugin: messaging type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests