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] While .configurate "PlatformException(error, PluginRegistrantCallback is not set., null)" #248

Closed
anjrew opened this issue Oct 2, 2019 · 30 comments
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. closed-by-bot platform: android Issues / PRs which are specifically for Android. plugin: messaging Stale Issue with no recent activity type: bug Something isn't working

Comments

@anjrew
Copy link

anjrew commented Oct 2, 2019

Describe the bug
When trying to configure the Firebase Cloud Messaging an Exception is thrown.
PlatformException (PlatformException(error, PluginRegistrantCallback is not set., null))

To Reproduce
Call the configure function.
Screenshot 2019-10-02 at 12 06 10

Stack Trance

E/flutter (17015): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null)
E/flutter (17015): #0      StandardMethodCodec.decodeEnvelope 
package:flutter/…/services/message_codecs.dart:569
E/flutter (17015): #1      MethodChannel.invokeMethod 
package:flutter/…/services/platform_channel.dart:321
E/flutter (17015): <asynchronous suspension>
E/flutter (17015): #2      FirebaseMessaging.configure 
package:firebase_messaging/firebase_messaging.dart:118
E/flutter (17015): #3      _configureMessaging 
package:firebase_cloud_messaging_play/main.dart:28
E/flutter (17015): #4      main 
package:firebase_cloud_messaging_play/main.dart:9
E/flutter (17015): #5      _runMainZoned.<anonymous closure>.<anonymous closure>  (dart:ui/hooks.dart:231:25)
E/flutter (17015): #6      _rootRun  (dart:async/zone.dart:1124:13)
E/flutter (17015): #7      _CustomZone.run  (dart:async/zone.dart:1021:19)
E/flutter (17015): #8      _runZoned  (dart:async/zone.dart:1516:10)
E/flutter (17015): #9      runZoned  (dart:async/zone.dart:1500:12)
E/flutter (17015): #10     _runMainZoned.<anonymous closure>  (dart:ui/hooks.dart:223:5)
E/flutter (17015): #11     _startIsolate.<anonymous closure>  (dart:isolate-patch/isolate_patch.dart:305:19)
E/flutter (17015): #12     _RawReceivePortImpl._handleMessage  (dart:isolate-patch/isolate_patch.dart:172:12)
E/flutter (17015):

Additional context
The repo is here
https://github.com/earyzhe/firebase_cloud_messaging_play

@anjrew anjrew added the type: bug Something isn't working label Oct 2, 2019
@GProDev2019
Copy link

Hello,
I have the same issue.
Any ideas how to solve it?

@charleshan
Copy link

This issue should be rename to [firebase_messaging] PlatformException thrown on configuration

@BondarenkoStas BondarenkoStas changed the title [cloud_messaging] PlatformException thrown on configuration [firebase_messaging] While .configurate "PlatformException(error, PluginRegistrantCallback is not set., null)" Oct 23, 2019
@nmnclk
Copy link

nmnclk commented Nov 6, 2019

Please help I have same problem .

@Eddydpyl
Copy link

Eddydpyl commented Nov 20, 2019

Several people have managed to get around the issue, me included (#199 | #116). In my case, there was an incompatibility caused by the barcode_scan package.

@mhadaily
Copy link
Member

I still have this issue and it only works when I delete
// onBackgroundMessage: Platform.isIOS ? null : backgroundMessageHandler,

@insinfo
Copy link

insinfo commented Dec 22, 2019

someone got any solution

@MustafaGamalAbbas
Copy link

Same Issue. :(

@sgehrman
Copy link

same issue.

@ghost
Copy link

ghost commented Jan 2, 2020

Any news? I am getting the same issue... Did anyone try lower versions of the plugin?

@marceloiavenissi
Copy link

marceloiavenissi commented Jan 3, 2020

Hello I followed the tutorial and had this problem.
1 - Replaces <application android: name = ".Application" with <application android: name = "com.myorg.myapp.Application".
2 - I had to add a cast to line 19 of Application.java:
GeneratedPluginRegistrant.registerWith ((FlutterEngine) registry);
3 - I put all functions (onMessage, onBackgroundMessage, onLaunch, onResume) as top-level.
4 - versions:
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.google.gms: google-services: 4.3.2'
5 - I added: implementation 'com.google.firebase: firebase-messaging: 20.1.0'

And the problem has now changed to:
[ERROR: flutter /lib/ui/ui_dart_state.cc (157)] Unhandled Exception: PlatformException (error, io.flutter.app.FlutterPluginRegistry cannot be cast to io.flutter.embedding.engine.FlutterEngine, null)

@marceloiavenissi
Copy link

Hello I followed the tutorial and had this problem.
1 - Replaces <application android: name = ". Application" with <application android: name = "com.myorg.myapp.Application".
2 - I had to add a cast to line 19 of Application.java:
GeneratedPluginRegistrant.registerWith ((FlutterEngine) registry);
3 - I put all functions (onMessage, onBackgroundMessage, onLaunch, onResume) as top-level.
4 - versions:
classpath 'com.android.tools.build:gradle:3.5.3'
classpath 'com.google.gms: google-services: 4.3.2'
5 - I added: implementation 'com.google.firebase: firebase-messaging: 20.1.0'

And the problem has now changed to:
[ERROR: flutter /lib/ui/ui_dart_state.cc (157)] Unhandled Exception: PlatformException (error, io.flutter.app.FlutterPluginRegistry cannot be cast to io.flutter.embedding.engine.FlutterEngine, null)

To solve the problem, I removed the cast I had placed on line 19 and used the FirebaseCloudMessagingPluginRegistrant.java class that I found at this link:
https://stackoverflow.com/questions/59446933/pluginregistry-cannot-be-converted-to-flutterengine

And it worked perfectly in the background.

@ghost
Copy link

ghost commented Jan 4, 2020

@marceloiavenissi did you use the sendNotificationOnBackground dart function from the stackoverflow as your (onMessage, onBackgroundMessage, onLaunch, onResume) at top-lavel?

@marceloiavenissi
Copy link

marceloiavenissi commented Jan 4, 2020

@marceloiavenissi did you use the sendNotificationOnBackground dart function from the stackoverflow as your (onMessage, onBackgroundMessage, onLaunch, onResume) at top-lavel?

No, just the same FirebaseCloudMessagingPluginRegistrant class

But put as top-level was not relevant to: onMessage, onLaunch, onResume

@ghost
Copy link

ghost commented Jan 4, 2020

So I also took these classes from SO. They seem to work, but I still have one problem... Here is my dart code:

`
class NotificationServiceImpl {

// make it singleton pls
static final NotificationServiceImpl _singleton = NotificationServiceImpl._internal();

static Future<dynamic> myBackgroundMessageHandler(Map<String, dynamic> message) {
  print("triggerd");

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

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

  return null;
  // Or do other work.
}


bool _initialized = false;
String _fcmToken;

Future<void> init() async {
  if (!_initialized) {
    // For iOS request permission first.
    _firebaseMessaging.requestNotificationPermissions();

    // _firebaseMessaging.configure();

    _firebaseMessaging.configure(
      onMessage: (Map<String, dynamic> message) async {
        print("onMessage: $message");
      },
      onBackgroundMessage: myBackgroundMessageHandler,
      onLaunch: (Map<String, dynamic> message) async {
        print("onLaunch: $message");
      },
      onResume: (Map<String, dynamic> message) async {
        print("onResume: $message");
      },
    );

    _fcmToken = await _firebaseMessaging.getToken();
    print("Token : "+_fcmToken);

    _initialized = true;
  }
}

Future<String> getToken() async {
  if (!_initialized) {
    await init();
  }
  return _fcmToken;
}



}

`

So at this point the callback triggers on background. However at build Process i am still getting an error:
Unhandled Exception: Invalid argument(s): Failed to setup background message handler!onBackgroundMessageE/flutter (24529): should be a TOP-LEVEL OR STATIC FUNCTION and should NOT be tied to a E/flutter (24529): class or an anonymous function.

Am I doing something wrong here? Isn't it already top level or static?

@marceloiavenissi
Copy link

@oe19fyfa This is not wrong anyway. But how is your application.java and the manifest?

@ghost
Copy link

ghost commented Jan 4, 2020

So I have 3 files in my package: Application.java ,FirebaseCloudMessagingPluginRegistrant.java as mentioned in SO. I also have a MainActivity.java that i had prior to SO stuff.
`
package mapackag;

public class MainActivity extends FlutterActivity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GeneratedPluginRegistrant.registerWith(this);
  }
}

`

And here is my manifest, forgot to mention:

`

            <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
            <application
                android:name="my.package.Application"
                android:label="label"
                android:icon="@mipmap/ic_launcher">
                <meta-data android:name="com.google.android.geo.API_KEY"
                           android:value="apikey"/>
                <activity
                    android:name=".MainActivity"
                    android:launchMode="singleTop"
                    android:theme="@style/LaunchTheme"
                    android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
                    android:hardwareAccelerated="true"
                    android:windowSoftInputMode="adjustResize">
                    <!-- This keeps the window background of the activity showing
                         until Flutter renders its first frame. It can be removed if
                         there is no splash screen (such as the default splash screen
                         defined in @style/LaunchTheme). -->
                    <meta-data
                        android:name="io.flutter.app.android.SplashScreenUntilFirstFrame"
                        android:value="true" />
                    <!-- BEGIN: Firebase Cloud Messaging -->
                    <intent-filter>
                        <action android:name="FLUTTER_NOTIFICATION_CLICK" />
                        <category android:name="android.intent.category.DEFAULT" />
                    </intent-filter>
                    <!-- END: Firebase Cloud Messaging -->
                    <intent-filter>
                        <action android:name="android.intent.action.MAIN"/>
                        <category android:name="android.intent.category.LAUNCHER"/>
                    </intent-filter>
                </activity>
            </application>
        </manifest>

`

@marceloiavenissi
Copy link

No errors were corrected when replacing GeneratedPluginRegistrant with FirebaseCloudMessagingPluginRegistrant on line 19 of the Application.

@colbymaloy
Copy link

colbymaloy commented Jan 20, 2020

hey guys, while following the guide on the package i also got the error.

I referenced some code from over a year ago using the package and the following works inside initState:
`
_messaging.requestNotificationPermissions();

_messaging.configure(onLaunch: (data) {
  print("onLaunch $data");
}, onMessage: (data) {
  print("onMessage $data");
}, onResume: (data) {
  print("OnResume $data");
});

`

will at least get rid of the error, and should still allow your devices to receive notifications - at least on android. I havent tested on iOS.

@ACHEZ9
Copy link

ACHEZ9 commented Feb 11, 2020

Piggybacking on this issue to point out that firebase_messaging does not properly support Flutter Android Embedding V2. This same error is thrown when the pluginRegistrantCallback is not set, but this is not required for V2. It should not be required to extend FlutterApplication and call FlutterFirebaseMessagingService.setPluginRegistrant when using newer Flutter versions, unless I am missing something with how the V2 Embedding on Android works.

@KanybekMomukeyev
Copy link

KanybekMomukeyev commented Feb 20, 2020

Same here, any solutions? Pls help

Flutter (Channel stable, v1.12.13+hotfix.8, on Mac OS X 10.14.6 18G3020)
• Flutter version 1.12.13+hotfix.8 at /Users/kk/flutter
• Framework revision 0b8abb4724 (9 days ago), 2020-02-11 11:44:36 -0800
• Engine revision e1e6ced81d
• Dart version 2.7.0

@Ehesp
Copy link
Member

Ehesp commented Apr 21, 2020

Hello - please can you check whether this is an issue on the latest messaging plugin (6.0.13 at this time). Thanks,

@Ehesp Ehesp added blocked: customer-response Waiting for customer response, e.g. more information was requested. platform: android Issues / PRs which are specifically for Android. plugin: messaging labels Apr 21, 2020
@ejprok
Copy link

ejprok commented Apr 23, 2020

Several people have managed to get around the issue, me included (#199 | #116). In my case, there was an incompatibility caused by the barcode_scan package.

@Eddydpyl Any luck getting barcode_scan to work with firebase_messaging or did you have to find an alternative?

@Eddydpyl
Copy link

@ejprok In the end I switched to qrcode_reader, which worked for me.

@ghost
Copy link

ghost commented Apr 29, 2020

I have followed the code in the example app with a blank app from Flutter create (Flutter version: 1.12.13+hotfix.9 and with version 6.0.13) and I get the following error:

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.

And the application just dies when receiving a notification (this is without setting the backgroundMessages handler). If I set it, then I get the following error on app initialization:

PlatformException (PlatformException(error, PluginRegistrantCallback is not set., null))

Any pointers?

@ghost
Copy link

ghost commented Apr 29, 2020

I have followed the code in the example app with a blank app from Flutter create (Flutter version: 1.12.13+hotfix.9 and with version 6.0.13) and I get the following error:

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.

And the application just dies when receiving a notification (this is without setting the backgroundMessages handler). If I set it, then I get the following error on app initialization:

PlatformException (PlatformException(error, PluginRegistrantCallback is not set., null))

Any pointers?

So, got it working by patching info from several sources. For some reason my MainActivity was in Kotlin, so the Application class needs to be in Kotlin as well. My Application.kt file looks like this:

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?) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

Using solution from (#1613 (comment)). I also needed to add the following (per #1607 (comment)):

in android/app/build.gradle
implementation "com.google.firebase:firebase-messaging:20.1.0"

@ghost
Copy link

ghost commented May 19, 2020

This is still a very big issue clearly from researching the errors involved in registering the plugin on Android.

Most users are hacking together from 10-20 sources and there is no definite answer to the problem.

Can we get some updated documentation on this issue related to Kotlin?

@Maus3rSR
Copy link

Maus3rSR commented Jun 4, 2020

I have the same issue with latest version of the plugin
firebase_messaging: ^6.0.16

@Maus3rSR
Copy link

Maus3rSR commented Jun 4, 2020

I have followed the code in the example app with a blank app from Flutter create (Flutter version: 1.12.13+hotfix.9 and with version 6.0.13) and I get the following error:

java.lang.RuntimeException: Unable to create service io.flutter.plugins.firebasemessaging.FlutterFirebaseMessagingService: java.lang.RuntimeException: PluginRegistrantCallback is not set.

And the application just dies when receiving a notification (this is without setting the backgroundMessages handler). If I set it, then I get the following error on app initialization:

PlatformException (PlatformException(error, PluginRegistrantCallback is not set., null))

Any pointers?

So, got it working by patching info from several sources. For some reason my MainActivity was in Kotlin, so the Application class needs to be in Kotlin as well. My Application.kt file looks like this:

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?) {
        io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin.registerWith(registry?.registrarFor("io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin"));
    }
}

Using solution from (#1613 (comment)). I also needed to add the following (per #1607 (comment)):

in android/app/build.gradle
implementation "com.google.firebase:firebase-messaging:20.1.0"

Implementing this solved my problem.

Be aware if you are a noob like me:

  • replace android:name="io.flutter.app.FlutterApplication" by android:name=".Application" in AndroidManifest.xml
  • add a file in the same location as your MainActivity.kt
  • In your Application.kt do not forget to add package com.yourpackage.example

@google-oss-bot google-oss-bot added the Stale Issue with no recent activity label Jul 30, 2020
@google-oss-bot
Copy link

Hey @earyzhe. We need more information to resolve this issue but there hasn't been an update in 7 weekdays. I'm marking the issue as stale and if there are no new updates in the next 7 days I will close it automatically.

If you have more information that will help us get to the bottom of this, just add a comment!

@google-oss-bot
Copy link

Since there haven't been any recent updates here, I am going to close this issue.

@earyzhe if you're still experiencing this problem and want to continue the discussion just leave a comment here and we are happy to re-open this.

@firebase firebase locked and limited conversation to collaborators Sep 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
blocked: customer-response Waiting for customer response, e.g. more information was requested. closed-by-bot platform: android Issues / PRs which are specifically for Android. plugin: messaging Stale Issue with no recent activity type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests