我正在构建一个聊天应用程序,其中所有实时消息都将通过 FCM 数据消息传递,然后使用
awesome_notifications
有条件地显示通知。一开始我遇到了一些问题,例如如果应用程序处于后台、屏幕关闭,甚至用户打开状态栏,FCM 后台处理程序无法更新聊天和聊天消息。我使用带有接收器和发送端口的隔离器解决了这些问题。一切都很好。问题是,如果应用程序完全终止,则不会发生任何事情。我跟踪并检查了我的所有代码和所有内容。一切似乎都是正确的,但是当我尝试运行 flutter run --release
并查看发送数据消息时它打印的内容时。我明白了:
E/flutter (23959): [ERROR:flutter/lib/ui/dart_runtime_hooks.cc(38)] Dart Error: Dart_LookupLibrary: library 'package:mobile_app_v2/services/firebase_helper.dart' not found.
E/flutter (23959): [ERROR:flutter/shell/common/shell.cc(117)] Dart Error: Dart_LookupLibrary: library 'package:mobile_app_v2/services/firebase_helper.dart' not found.
E/flutter (23959): [ERROR:flutter/shell/common/shell.cc(117)] Dart Error: Dart_LookupLibrary: library 'package:mobile_app_v2/services/firebase_helper.dart' not found.
需要知道的是,
firebase_helper.dart
是具有FCM处理程序(如onMessage
和onBackgroundMessage
)的初始化和绑定的文件,这些处理程序在runApp(const MyApp());
中的main.dart
之前被调用。
firebase_helper.dart:
static void initializeFCMHandler() async {
FirebaseMessaging.onBackgroundMessage(handleBackgroundMessage);
FirebaseMessaging.onMessage.listen(handleMessage);
}
static Future<void> handleBackgroundMessage(RemoteMessage? message) async {
NotificationService().pushNotification(
id: 200,
body: "Message From Background",
title: "Conversations",
category: NotificationCategory.Message,
payload: {
"screen": "chat",
},
);
handleDataMessage(message, isBackground: true);
}
static void handleMessage(RemoteMessage? message) async {
// check if message is notification
print(message?.data);
if (message?.notification != null) {
handleNotification(message);
} else {
handleDataMessage(message);
}
}
我还尝试将应用程序构建为 apk 并安装它,但再次没有响应。没有通知,什么都没有。我查了一下,确实可以成功接收数据信息,一切正常,那么问题出在哪里呢?我看到人们仅在 iOS 上遇到问题。
用户通过身份验证时使用 getInitialMessage:
FirebaseMessaging.instance.getInitialMessage().then(_handleFirebaseMessage);
处理消息:
static void _handleFirebaseMessage(RemoteMessage? message) {
logger.i('Remote message: $message');
if (message == null) return;
...
}