Flutter 中未设置 PluginRegistrantCallback
Posted
技术标签:
【中文标题】Flutter 中未设置 PluginRegistrantCallback【英文标题】:PluginRegistrantCallback is not set in Flutter 【发布时间】:2021-06-06 10:59:09 【问题描述】:我在运行我的应用程序时收到此错误,并且应用程序关闭时的后台通知不起作用:
firebase_messaging: ^7.0.3
final FirebaseMessaging firebaseMessaging = FirebaseMessaging();
@override
void initState()
super.initState();
registerNotification();
void registerNotification()
firebaseMessaging.requestNotificationPermissions();
firebaseMessaging.configure(onMessage: (Map<String, dynamic> message)
print('onMessage: $message');
return ;
, onResume: (Map<String, dynamic> message)
print('onResume: $message');
return Navigator.push(
context,
MaterialPageRoute(
builder: (context) => NotificationsScreen()));
, onBackgroundMessage: _firebaseMessagingBackgroundHandler,
onLaunch: (Map<String, dynamic> message)
print('onLaunch: $message');
return;
);
firebaseMessaging.getToken().then((token)
print('token: $token');
FirebaseFirestore.instance
.collection('Consultant')
.doc(firebaseUser.uid)
.update('deviceToken': token);
).catchError((err)
//Fluttertoast.showToast(msg: err.message.toString());
);
下课:
Future<dynamic> _firebaseMessagingBackgroundHandler(
Map<String, dynamic> message,
) async
// Initialize the Firebase app
await Firebase.initializeApp();
print('onBackgroundMessage received: $message');
我得到了:ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(error, PluginRegistrantCallback is not set., null, java.lang.RuntimeException: PluginRegistrantCallback is not set.
我发现的问题是当应用程序关闭时我无法收到通知。
在互联网上搜索我看到:PluginRegistrantCallback is not set
是涉及 Application.kt 类的错误,但我尝试以任何方式放置此文件均未成功。
有人有什么建议吗?
【问题讨论】:
同样的问题 【参考方案1】:该错误来自您的应用程序文件。请务必正确注册。
Application.java
package <your package name>;
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class CustomPluginRegistrant
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 = CustomPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key))
return true;
registry.registrarFor(key);
return false;
在同一路径中添加一个名为 CustomPluginRegistant.java 的文件
package <your package name>;
import com.dexterous.flutterlocalnotifications.FlutterLocalNotificationsPlugin;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugins.firebasemessaging.FirebaseMessagingPlugin;
public final class CustomPluginRegistrant
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 = CustomPluginRegistrant.class.getCanonicalName();
if (registry.hasPlugin(key))
return true;
registry.registrarFor(key);
return false;
如果您没有使用本地通知插件,则可以从代码中删除该部分。
【讨论】:
我们需要把这些文件放在哪里? 我假设我们需要将 Application.java 与 CustomPluginRegistant.java 一起放入 java 文件夹 路径是-> android/app/src/main/java/我尝试使用firebase_messaging: ^7.0.3
解决Application.java
的解决方法,但最终遇到了很多问题。幸运的是,我遇到了关于如何设置 firebase 的更新。正如the official doc 所指出的,当您使用Flutter Android Embedding V2 (Flutter Version >= 1.12) 时,android 不需要额外的步骤。因此,我们首先将以下依赖项添加到 pubsec.yaml:
firebase_core: ^0.7.0
firebase_messaging: ^8.0.0-dev.15
我在my answer here 中包含了更详细的代码示例答案。
你运行flutter pub get
。然后按照the doc 中提到的每一步,您就可以开始了。
【讨论】:
以上是关于Flutter 中未设置 PluginRegistrantCallback的主要内容,如果未能解决你的问题,请参考以下文章
IntelliJ 2020.1 版中未安装 Flutter 插件
未捕获的 ReferenceError:Flutter 中未定义 firebase
Flutter 中未定义的类名“MaterialApp”。如何解决这个问题?