I am working on a Flutter project where I have set up notifications using Firebase Messaging. Everything was working perfectly until recently. After coming back to the project after a long time and running the code again, the app crashes with an ANR (Application Not Responding) when a notification is received.
The notification is received, and the foreground handler gets the data, but then the app crashes immediately.
After debugging, I realized that once the notification is received, something takes a long time to process, which is likely causing the crash.
To investigate further, I removed all heavy tasks and left only a simple print log inside the handler—but the crash still occurs.
Here is the error message shown when the app crashes:
ANR in com.company
PID: 24618
Reason: Broadcast of Intent { act=com.google.android.c2dm.intent.RECEIVE flg=0x11000010 pkg=com.company cmp=com.company/uk.orth.push.FirebaseMessagingReceiver (has extras) }
ErrorId: f29624e4-05d6-4b28-960f-81a2550b55eb
Frozen: false
Load: 3.89 / 3.9 / 3.99
Other memory and CPU stats follow, which seem to indicate system pressure, but I’m not doing anything intensive in the handler anymore. Yet the crash continues.
Now I’m stuck and not sure what to do next.
If anyone has faced a similar issue or knows what might be causing this, please guide me on how to fix it.
here is the code that procue error (Actual code is diffrenet but for this code it's not working)
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/material.dart';
// This is the background message handler. This should be run on a background isolate.
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
print("Handling a background message: ${message.messageId}");
// Perform background processing here, but ensure no UI updates happen here.
// You can log data, fetch data, or update your database.
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
await Firebase.initializeApp();
// Register the background message handler
FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);
runApp(MyApp());
}
class MyApp extends StatefulWidget {
@override
_MyAppState createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final FirebaseMessaging _firebaseMessaging = FirebaseMessaging.instance;
@override
void initState() {
super.initState();
// Request notification permissions for iOS
_firebaseMessaging.requestPermission();
// Handle foreground messages
FirebaseMessaging.onMessage.listen((RemoteMessage message) {
print("Received message while the app is in the foreground: ${message.messageId}");
// Handle foreground notification here (e.g., show a dialog, update UI)
if (message.notification != null) {
print("Notification title: ${message.notification!.title}");
print("Notification body: ${message.notification!.body}");
}
});
// Handle message when the app is resumed from background (user taps on notification)
FirebaseMessaging.onMessageOpenedApp.listen((RemoteMessage message) {
print("Message clicked! Opening app...");
// Perform navigation or specific actions on notification tap
});
// Get the FCM token (to register the device for notifications)
_firebaseMessaging.getToken().then((String? token) {
print("FCM Token: $token");
});
}
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter FCM Demo',
home: Scaffold(
appBar: AppBar(
title: Text("Flutter FCM Demo"),
),
body: Center(child: Text("Push Notification Example")),
),
);
}
}
After some debugging(for my acctuale code), I was able to identify the root cause of the ANR (Application Not Responding) issue in my Flutter project when using Firebase notifications.
I had implemented Firebase Cloud Messaging (FCM) using the HTTP v1 API and was sending custom data along with the notification payload. This data was being stored locally using the sqflite plugin.
In the database, I had defined a table with some fields marked as NOT NULL. However, when sending notifications without including the expected data in the payload, the app still attempted to insert the incomplete data into the SQLite table. Since the NOT NULL constraints were violated, this led to exceptions during the insert operation — and for some reason, this caused the app to hang and eventually throw an ANR.
Once I ensured that the necessary data was always included in the notification payload — matching the table structure and respecting the NOT NULL fields — the issue was resolved.
Hope this helps others facing a similar issue!
But here I don't know why it's not working for the minimal code where i didn't used that function to store the data after fixing this for this minimal code then it's dosen't give the ANR.