Skip to content

fix(messaging,android): deliver every foreground message to onMessage exactly once - #18831

Open
Lyokone wants to merge 2 commits into
mainfrom
fix/messaging-android-foreground-delivery
Open

Lyokone wants to merge 2 commits into
mainfrom
fix/messaging-android-foreground-delivery

Conversation

@Lyokone

@Lyokone Lyokone commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Builds on #18830, now merged: its Dart fix makes onMessage work without another messaging call first, on Android too.

Description

On Android, foreground messages go from FlutterFirebaseMessagingReceiver to the plugin through a LiveData. That caused two bugs, both reproduced on an emulator (Pixel 9a):

1. Messages lost in bursts. The receiver posts with postValue from a background thread, and postValue keeps only the last of the values posted before the main thread runs. 5 messages handed to the receiver back to back reached onMessage as 1 (the last).

2. Old messages delivered again (#12411, closed by the stale bot, never fixed). LiveData hands its last value to every new observer, so each newly attached engine gets the previous foreground message as a new onMessage. For example: a message arrives, the user leaves with Back (the engine is destroyed, the process is kept), then reopens the app, and the new engine gets the old message again.

FlutterFirebaseRemoteMessageDispatcher replaces the LiveData:

  • Every message is delivered once, in order, on the main thread, to the engines whose Dart listens to FirebaseMessaging.onMessage.
  • Listening and cancelling tell the plugin (Messaging#startForegroundMessages / Messaging#stopForegroundMessages). The signal is sent from the firebase_messaging package, which ships together with the native code, so a newer native side can't be paired with Dart that never sends it.
  • Messages received while no engine listens are kept, for up to a minute and at most 50, and handed to the first engine that listens, never again. Engines that never listen, like the background engine that runs onBackgroundMessage, get none.
  • FlutterFirebaseRemoteMessageLiveData is kept for compatibility. It is deprecated, postRemoteMessage forwards to the dispatcher, and its observers are still told about every message.

The main channel buffer is also enlarged (Flutter keeps one message by default), with every message sent to Dart. Engines created with new FlutterEngine(...) register plugins before they run Dart, and Flutter drops messages until then.

Emulator before after
5 messages back to back onMessage gets 1 (the last) all 5, in order
A second engine starts listening after a message it also gets the old message only new messages
3 messages sent before Dart handles the channel only the last is kept all 3

Notes:

  • App unit tests that mock the plugins.flutter.io/firebase_messaging channel on Android (flutter_test's default platform) now also see Messaging#startForegroundMessages / Messaging#stopForegroundMessages calls.
  • Token and FID LiveDatas still replay their last value to new engines (onTokenRefresh with the same token); left as is.
  • After a hot restart, messages that arrive before the new Dart listens are not kept (debug only).

Tests

  • JUnit (android/src/test, now run by android-unit-tests): bursts, every listener, no replay to later listeners, kept messages delivered once, lifetime and size limits, removal, and the legacy LiveData being notified.
  • Dart (firebase_messaging/test/on_message_listening_test.dart): start on the first listen, stop on the last cancel, nothing on iOS.
  • Android e2e (example/integration_test/remote_notification_delivery.dart, run by e2e-messaging): a test-only channel in the example's MainActivity.kt hands FCM-shaped intents to the real FlutterFirebaseMessagingReceiver. Each test below fails on the previous code and passes with this change:
    • a burst reaches onMessage once and in order;
    • a second engine started via a callback handle gets only the messages received after it listens;
    • messages sent before Dart handles the channel are all kept.

Related Issues

Fixes #12411. Builds on #18830 (merged).

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
  • My PR includes unit or integration tests for all changed/updated/fixed behaviors (See Contributor Guide).
  • All existing and new tests are passing.
  • I updated/added relevant documentation (doc comments with ///).
  • The analyzer (melos run analyze) does not report any problems on my PR.
  • I read and followed the Flutter Style Guide.
  • I signed the CLA.
  • I am willing to follow-up on review comments in a timely manner.

Breaking Change

  • No, this is not a breaking change.

@gemini-code-assist

Copy link
Copy Markdown
Contributor
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

… exactly once

Foreground messages went from the broadcast receiver to the plugin through a
`LiveData`, which caused two bugs:

- Messages were lost: `postValue` keeps only the last of the values posted
  before the main thread runs, so a burst of pushes reached `onMessage` as a
  single message (5 back to back: only the last one arrived).
- Old messages were delivered again: `LiveData` hands its last value to every
  new observer, so each newly attached engine (app reopened after Back while
  the process lived, or another engine) got the previous foreground message
  as a new `onMessage` (#12411).

`FlutterFirebaseRemoteMessageDispatcher` replaces it. It delivers each message
once, in order, to the engines whose Dart listens to
`FirebaseMessaging.onMessage`, which now tells the plugin when it starts and
stops listening (`Messaging#startForegroundMessages` /
`Messaging#stopForegroundMessages`). The signal is sent from this package,
which ships with the native code, so the two cannot get out of step. Messages
received while no engine listens are kept for up to a minute and handed to the
first one that does. Engines that never listen, like the background engine
that runs `onBackgroundMessage`, get none.

`FlutterFirebaseRemoteMessageLiveData` is deprecated: `postRemoteMessage`
forwards to the dispatcher, and its observers are still told about every
message.

The main channel buffer is also enlarged, with every message sent to Dart:
engines created with `new FlutterEngine(...)` register plugins before running
Dart, and Flutter drops messages until then.

Tests:
- JUnit tests for the dispatcher, added to `android-unit-tests`.
- Dart tests for the start/stop signal.
- e2e tests that hand messages to the real receiver through a test-only
  channel in the example app: a burst reaches `onMessage` once and in order,
  a second engine started later gets only the new messages, and messages sent
  before Dart handles the channel are all kept. Each fails on the previous
  code.
@Lyokone
Lyokone force-pushed the fix/messaging-android-foreground-delivery branch from 7c4eecb to 40c1414 Compare October 21, 2026 14:08
@Lyokone
Lyokone marked this pull request as ready for review October 21, 2026 14:39

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

🐛 [firebase_messaging] Latest foreground messages delivered again after app is closed and re-opened

2 participants