Skip to content

feat(messaging, android): expose Android notification delegation APIs - #19019

Open
ReoAndo wants to merge 1 commit into
firebase:mainfrom
ReoAndo:feat/messaging-android-notification-delegation
Open

ReoAndo wants to merge 1 commit into
firebase:mainfrom
ReoAndo:feat/messaging-android-notification-delegation

Conversation

@ReoAndo

@ReoAndo ReoAndo commented Nov 27, 2026

Copy link
Copy Markdown

Description

The Firebase Messaging Android SDK exposes runtime notification delegation APIs (FirebaseMessaging.setNotificationDelegationEnabled(boolean) and FirebaseMessaging.isNotificationDelegationEnabled()) to control whether Google Play services can display FCM notification messages on the app's behalf on Android 10 (API level 29) and higher.

Currently, firebase_messaging does not expose these APIs to Dart, so Flutter apps that need to query or toggle notification delegation at runtime (for example, to ensure incoming notifications are delivered directly to the app's messaging service or to adjust delegation state dynamically without rebuilding AndroidManifest.xml) have to write custom Android platform channels.

This PR exposes both APIs end-to-end across the firebase_messaging packages:

  • Dart API (FirebaseMessaging / FirebaseMessagingPlatform / MethodChannelFirebaseMessaging):
    • Future<void> setNotificationDelegationEnabled(bool enabled) — invokes Messaging#setNotificationDelegationEnabled on Android; completes as a no-op on non-Android platforms (iOS, macOS, web).
    • Future<bool> isNotificationDelegationEnabled() — invokes Messaging#isNotificationDelegationEnabled on Android; returns false on non-Android platforms (iOS, macOS, web).
  • Web (FirebaseMessagingWeb):
    • Overrides setNotificationDelegationEnabled as a no-op and isNotificationDelegationEnabled returning false so cross-platform callers do not hit UnimplementedError on web.
  • Android (FlutterFirebaseMessagingPlugin / FlutterFirebaseMessagingUtils):
    • Handles Messaging#setNotificationDelegationEnabled by awaiting FirebaseMessaging.setNotificationDelegationEnabled(enabled) off the main thread (Tasks.await(...) on cachedThreadPool).
    • Handles Messaging#isNotificationDelegationEnabled by returning {'isNotificationDelegationEnabled': firebaseMessaging.isNotificationDelegationEnabled()} off the main thread.
    • On Android devices below API level 29, the underlying Firebase Messaging Android SDK handles setNotificationDelegationEnabled as a no-op (Tasks.forResult(null)) and returns false from isNotificationDelegationEnabled().
  • Tests:
    • Platform interface UnimplementedError tests in platform_interface_messaging_test.dart.
    • Method channel tests (Android true/false, non-Android no-op/false, PlatformException -> FirebaseException mapping) in method_channel_messaging_test.dart.
    • Wrapper delegation tests in firebase_messaging_test.dart (and mock stubs in mock.dart).
    • Web implementation tests in firebase_messaging_web_test.dart.
    • Android Robolectric unit tests in FlutterFirebaseMessagingNotificationDelegationTest.java.
    • Integration test coverage in example/integration_test/e2e_test.dart.

Verification

  • dart format --output=none --set-exit-if-changed on all 10 modified Dart files: passed (0 changed).
  • dart analyze . --fatal-infos in firebase_messaging_platform_interface and firebase_messaging_web, and dart analyze lib test --fatal-infos in firebase_messaging: no issues found.
  • flutter test in firebase_messaging_platform_interface: 121/121 passed.
  • flutter test in firebase_messaging: 24/24 passed.
  • flutter test --platform chrome in firebase_messaging_web: 4/4 passed.
  • dart scripts/validate_wasm_imports.dart: passed (43 packages checked).
  • cd tests/android && ./gradlew :firebase_messaging:testDebugUnitTest: passed (42/42 Android unit tests across 6 suites, including all 5 new tests in FlutterFirebaseMessagingNotificationDelegationTest).

Related Issues

Related context: #13569, #17207 (both closed; users reported needing to disable Android notification delegation so notification taps and callbacks behave consistently).

Checklist

Before you create this PR confirm that it meets all requirements listed below by checking the relevant checkboxes ([x]).
This will ensure a smooth and quick review process. Updating the pubspec.yaml and changelogs is not required.

  • 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

Does your PR require plugin users to manually update their apps to accommodate your change?

  • Yes, this is a breaking change.
  • No, this is not a breaking change.

Add setNotificationDelegationEnabled(bool enabled) and
isNotificationDelegationEnabled() across firebase_messaging,
firebase_messaging_platform_interface, firebase_messaging_web, and the
Android plugin to wrap the Firebase Messaging Android SDK notification
delegation APIs.

On Android 10 (API 29) and higher, apps can query and toggle Google Play
services notification delegation at runtime. On Android devices below
API 29 and on non-Android platforms (iOS, macOS, web),
setNotificationDelegationEnabled completes as a no-op and
isNotificationDelegationEnabled returns false.

Co-Authored-By: Antigravity (Gemini 4 Neon) <noreply@google.com>
@google-cla

google-cla Bot commented Nov 27, 2026

Copy link
Copy Markdown

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces support for enabling and disabling notification delegation on Android within the firebase_messaging plugin, exposing setNotificationDelegationEnabled and isNotificationDelegationEnabled APIs. The changes are well-tested across unit, integration, and platform interface levels. Feedback on the implementation highlights a potential NullPointerException in the Java plugin when handling arguments, and points out a couple of violations of the repository's style guide regarding the use of the ! operator in Dart.

cachedThreadPool.execute(
() -> {
try {
Boolean enabled = (Boolean) Objects.requireNonNull(arguments.get("enabled"));

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To prevent a potential NullPointerException, perform a null check on arguments before attempting to retrieve the 'enabled' key.

            if (arguments == null) {
              throw new IllegalArgumentException("Arguments cannot be null");
            }
            Boolean enabled = (Boolean) Objects.requireNonNull(arguments.get("enabled"));

Comment on lines +571 to +577
Map<String, dynamic>? data = await channel
.invokeMapMethod<String, dynamic>(
'Messaging#isNotificationDelegationEnabled',
{'appName': app.name},
);

return data!['isNotificationDelegationEnabled'] as bool;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

To adhere to the repository style guide regarding null safety, avoid using the ! operator. We can safely retrieve the value using optional chaining and a null-coalescing operator.

Suggested change
Map<String, dynamic>? data = await channel
.invokeMapMethod<String, dynamic>(
'Messaging#isNotificationDelegationEnabled',
{'appName': app.name},
);
return data!['isNotificationDelegationEnabled'] as bool;
final data = await channel.invokeMapMethod<String, dynamic>(
'Messaging#isNotificationDelegationEnabled',
{'appName': app.name},
);
return data?['isNotificationDelegationEnabled'] as bool? ?? false;
References
  1. NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

expect(await messaging.isNotificationDelegationEnabled(), isFalse);

await messaging.setNotificationDelegationEnabled(true);
final expected = sdkInt != null && sdkInt! >= 29;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

low

Avoid using the ! operator to adhere to the repository style guide. Since sdkInt is a mutable instance/closure variable, copying it to a local variable allows Dart's flow analysis to promote it to non-nullable safely.

Suggested change
final expected = sdkInt != null && sdkInt! >= 29;
final currentSdkInt = sdkInt;
final expected = currentSdkInt != null && currentSdkInt >= 29;
References
  1. NO ! operator. Use ? and flow analysis (e.g. if (x != null)). (link)

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.

1 participant