3

I have below packages in my pubspec.yaml

firebase_core: ^2.2.0 
firebase_messaging: ^14.1.0

And in my main.dart

@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {

}

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  FirebaseMessaging.onBackgroundMessage(_firebaseMessagingBackgroundHandler);

  runApp(const MyApp());
}

This causes my main method to run twice, in 2 isolates. If I remove the FirebaseMessaging.onBackgroundMessage(...) line, it runs a single isolate.

I see this in vscode callstacks window when I put a break point at runApp(...) as this gets hit twice.

Without onBackgroundMessage line

single isolate running main method

With onBackgroundMessage line

two isolates running main method

As a result of this, the firebase onMessage and other handlers fire twice for each notification and does duplicate work, and since they're in different isolates, its hard to check and avoid it. (example above does not have this part)

I see the build method for MyApp also runs twice, which I don't want. This likely has a memory and compute cost which I would like to avoid.

I would like to know if this is normal behavior? If so, is there a way to avoid this? i.e is there a way to detect the duplicate isolate and main method can return?

If its not normal behavior, what am I doing wrong?


for completeness, output of flutter --version

Flutter 3.3.6 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 6928314d50 (3 weeks ago) • 2022-10-25 16:34:41 -0400
Engine • revision 3ad69d7be3
Tools • Dart 2.18.2 • DevTools 2.15.0
Madushan
  • 6,977
  • 31
  • 79

1 Answers1

1

UPDATE:

Below latest versions now work as expected.

  firebase_core: 2.3.0
  firebase_remote_config: 3.0.6
  firebase_analytics: 10.0.6
  firebase_crashlytics: 3.0.6
  firebase_messaging: 14.1.1

Firebase team has reverted some changes and will apply it in a future version.


If you're only using messaging, use the following versions

  firebase_core: ^1.24.0
  firebase_messaging: ^13.1.0

If you're using other firebase services, these are the versions I managed to get to work together, without using dependency overrides.

  # when you update firebase versions again because its
  # "good practice" and it ended up in subtle bugs, loss 
  # of self-confidence, sleep deprevation and an overall 
  # degredation in quality of life, increase the counter 
  # here as a warning for the time after that you feel 
  # like doing it again.
  #
  # time spent resolving issues caused by updating
  # firebase version upgrades that wasn't needed: 43h
  # versions last updated on: 15 nov 2022
  firebase_core: 1.24.0
  firebase_remote_config: 2.0.20
  firebase_analytics: 9.3.8
  firebase_crashlytics: 2.9.0
  firebase_messaging: 13.1.0

Currently you can either take the latest versions of everything and accept the bug, or lock your firebase packages to above versions and wait for a time you have a good weekend free to try and update to the latest versions and see what breaks.

It will likely be fixed eventually, and will likely be delivered in a bugfix version to all the packages.

Got pointers from this question and answers

This issue on github is likely the same as this one.

Madushan
  • 6,977
  • 31
  • 79