I am building a chat app where all the realtime messages will be delivered via FCM data messages and then conditionally show a notification using awesome_notifications
. I had some problems at first like FCM background handler can't update chats and chats' messages if the app is in background, screen off, or even the user opened the status bar. I resolved those issues using an isolate with a receiver and send ports. everything is fine. The problem is if the app is completely terminated, nothing happens. I traced and checked all my code and everything. everything seems correct but when I try to run flutter run --release
and see what it prints when the data message is sent. I get this:
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.
all there is to know is that firebase_helper.dart
is the file that has the initialization and binding of the FCM handlers like onMessage
and onBackgroundMessage
that is being called before runApp(const MyApp());
in 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);
}
}
I also tried building the app as apk and installing it but again no response. no notification, nothing. I searched and people actually can receive data messages successfully and everything works so where is the problem? I see people having a problem in iOS only.
Tree shaking is probably removing this method. Likely this is why it's only happening on release builds. Update to the following:
@pragma('vm:entry-point') // required or this will be stripped by tree-shaking
static void handleMessage(RemoteMessage? message) async {
(existing code...)
You may also need it on static void initializeFCMHandler() async {