PluginRegistrantCallback 未在 Flutter 中设置
PluginRegistrantCallback is not set in Flutter
我 运行 我的应用程序出现此错误,应用程序关闭时后台通知不起作用:
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());
});
}
从class出来:
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 class 的错误,但我尝试以任何方式未能成功地放置此文件。
大家有什么建议吗?
该错误来自您的应用程序文件。确保正确注册。
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;
}
}
如果您没有使用本地通知插件,则可以从代码中删除该部分。
我尝试使用 firebase_messaging: ^7.0.3
来解决 Application.java
问题,结果 运行 遇到了很多问题。幸运的是,我遇到了关于如何为 flutter 设置 firebase 的更新。如 the official doc 所示,当您使用 Flutter Android Embedding V2(Flutter 版本 >= 1.12) 时,android 不需要额外的步骤.因此,我们首先将以下依赖项添加到 pubsec.yaml:
firebase_core: ^0.7.0
firebase_messaging: ^8.0.0-dev.15
我在 中包含了更详细的代码示例答案。
你运行flutter pub get
。然后按照 中提到的每一步 the doc 然后你就可以开始了。
我 运行 我的应用程序出现此错误,应用程序关闭时后台通知不起作用:
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());
});
}
从class出来:
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 class 的错误,但我尝试以任何方式未能成功地放置此文件。
大家有什么建议吗?
该错误来自您的应用程序文件。确保正确注册。
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;
}
}
如果您没有使用本地通知插件,则可以从代码中删除该部分。
我尝试使用 firebase_messaging: ^7.0.3
来解决 Application.java
问题,结果 运行 遇到了很多问题。幸运的是,我遇到了关于如何为 flutter 设置 firebase 的更新。如 the official doc 所示,当您使用 Flutter Android Embedding V2(Flutter 版本 >= 1.12) 时,android 不需要额外的步骤.因此,我们首先将以下依赖项添加到 pubsec.yaml:
firebase_core: ^0.7.0
firebase_messaging: ^8.0.0-dev.15
我在
你运行flutter pub get
。然后按照 中提到的每一步 the doc 然后你就可以开始了。