我试图在BackgroundMessage上初始化Firebase消息传递,但出现了一个非常不寻常的错误。我尝试了所有我知道的方法,但发现错误仍然存在。
Future<void> handleBackgroundMessage(RemoteMessage message) async {
final notification = message.notification;
if (notification != null) {
print("title: ${notification.title}");
print("body: ${notification.body}");
} else {
print("No notification data found in the message.");
}
final data = message.data;
print("payload: $data");
}
这就是我调用handleBackgroundMessage的地方
Future<void> initNotification() async {
try {
await _firebaseMessaging.requestPermission();
final fcmToken = await _firebaseMessaging.getToken();
await _firebaseMessaging.subscribeToTopic("Church");
print(fcmToken);
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
} catch (e) {
print("Error initializing notifications: $e");
}
}
这是我在主函数中初始化firebase的地方
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseApi().initNotification();
runApp(const MainApp());
}
这是我得到的错误
Restarted application in 7,630ms.
W/kiibati.kiibat( 2363): Accessing hidden method Ldalvik/system/CloseGuard;->close()V (greylist,core-platform-api, linking, allowed)
W/kiibati.kiibat( 2363): Accessing hidden method Ldalvik/system/CloseGuard;->warnIfOpen()V (greylist,core-platform-api, linking, allowed)
I/flutter ( 2363): c14bRHbiQtmVpfJ5RlOpng:APA91bFm5XYVPU4CvICaNb8oFkYWw80T7ZK1RehffDPgna59WxX1nWRzEFSltY7mH4cBRrpmb07ZMZNKY0g3TrlbnclGG8oqn8XGL8oL8SzhOCUR8r1dCpW1H6qBzxVztj4mJi3p7Sne
E/flutter ( 2363): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Null check operator used on a null value
E/flutter ( 2363): #0 MethodChannelFirebaseMessaging.registerBackgroundMessageHandler
method_channel_messaging.dart:181
E/flutter ( 2363): #1 FirebaseMessagingPlatform.onBackgroundMessage=
platform_interface_messaging.dart:102
E/flutter ( 2363): #2 FirebaseMessaging.onBackgroundMessage
messaging.dart:73
E/flutter ( 2363): #3 FirebaseApi.initNotification
firebaseapi.dart:17
E/flutter ( 2363): <asynchronous suspension>
E/flutter ( 2363): #4 main
main.dart:23
E/flutter ( 2363): <asynchronous suspension>
E/flutter ( 2363):
D/SharedPreferencesImpl( 2363): Time required to fsync /data/user/0/com.kiibati.kiibati/shared_prefs/com.google.android.gms.appid.xml: [<1: 0, <2: 0, <4: 0, <8: 1, <16: 2, <32: 0, <64: 0, <128: 0, <256: 0, <512: 1, <1024: 0, <2048: 0, <4096: 0, <8192: 0, <16384: 0, >=16384: 0]
W/DynamiteModule( 2363): Local module descriptor class for com.google.android.gms.providerinstaller.dynamite not found.
I/DynamiteModule( 2363): Considering local module com.google.android.gms.providerinstaller.dynamite:0 and remote module com.google.android.gms.providerinstaller.dynamite:0
W/ProviderInstaller( 2363): Failed to load providerinstaller module: No acceptable module com.google.android.gms.providerinstaller.dynamite found. Local version is 0 and remote version is 0.
D/FLTFireMsgReceiver( 2363): broadcast received for message
W/FLTFireMsgService( 2363): A background message could not be handled in Dart as no onBackgroundMessage handler has been registered.
W/FirebaseMessaging( 2363): Unable to log event: analytics library is missing
W/FirebaseMessaging( 2363): Missing Default Notification Channel metadata in AndroidManifest. Default value will be used.
2条答案
按热度按时间lymgl2op1#
好的,请将你的代码还原成这样:
这是从我的代码请更换所有的变量,以您的喜好
dfty9e192#
错误“Unhandled Exception:Null check operator used on a null value”表示您正尝试对为null的变量使用null check operator。在本例中,似乎是在尝试使用FirebaseMessaging.onBackgroundMessage(默认BackgroundMessage)注册后台消息处理程序时出现问题。
若要解决此问题,应确保确实定义了handleBackgroundMessage,并且不为null。另外,请确保正确配置了FirebaseMessaging的导入和初始化。以下是一些步骤:
确保在Dart文件中正确导入了FirebaseMessaging:
确保您已在main或应用的入口点初始化FirebaseMessaging:
验证handleBackgroundMessage函数是否定义正确且没有任何错误:
确保适当地调用了initNotification函数。检查函数调用序列是否存在任何问题,并确保Firebase配置正确。希望这有助于解决问题。
如果执行这些步骤后问题仍然存在,您可能需要检查您正在使用的Firebase Messaging版本,以确保它是最新的,并且与您正在使用的Flutter版本兼容。