Repository navigation
Conversation
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>
|
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. |
There was a problem hiding this comment.
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")); |
There was a problem hiding this comment.
| Map<String, dynamic>? data = await channel | ||
| .invokeMapMethod<String, dynamic>( | ||
| 'Messaging#isNotificationDelegationEnabled', | ||
| {'appName': app.name}, | ||
| ); | ||
|
|
||
| return data!['isNotificationDelegationEnabled'] as bool; |
There was a problem hiding this comment.
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.
| 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
- 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; |
There was a problem hiding this comment.
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.
| final expected = sdkInt != null && sdkInt! >= 29; | |
| final currentSdkInt = sdkInt; | |
| final expected = currentSdkInt != null && currentSdkInt >= 29; |
References
- NO
!operator. Use?and flow analysis (e.g.if (x != null)). (link)
Description
The Firebase Messaging Android SDK exposes runtime notification delegation APIs (
FirebaseMessaging.setNotificationDelegationEnabled(boolean)andFirebaseMessaging.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_messagingdoes 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 rebuildingAndroidManifest.xml) have to write custom Android platform channels.This PR exposes both APIs end-to-end across the
firebase_messagingpackages:FirebaseMessaging/FirebaseMessagingPlatform/MethodChannelFirebaseMessaging):Future<void> setNotificationDelegationEnabled(bool enabled)— invokesMessaging#setNotificationDelegationEnabledon Android; completes as a no-op on non-Android platforms (iOS, macOS, web).Future<bool> isNotificationDelegationEnabled()— invokesMessaging#isNotificationDelegationEnabledon Android; returnsfalseon non-Android platforms (iOS, macOS, web).FirebaseMessagingWeb):setNotificationDelegationEnabledas a no-op andisNotificationDelegationEnabledreturningfalseso cross-platform callers do not hitUnimplementedErroron web.FlutterFirebaseMessagingPlugin/FlutterFirebaseMessagingUtils):Messaging#setNotificationDelegationEnabledby awaitingFirebaseMessaging.setNotificationDelegationEnabled(enabled)off the main thread (Tasks.await(...)oncachedThreadPool).Messaging#isNotificationDelegationEnabledby returning{'isNotificationDelegationEnabled': firebaseMessaging.isNotificationDelegationEnabled()}off the main thread.setNotificationDelegationEnabledas a no-op (Tasks.forResult(null)) and returnsfalsefromisNotificationDelegationEnabled().UnimplementedErrortests inplatform_interface_messaging_test.dart.true/false, non-Android no-op/false,PlatformException->FirebaseExceptionmapping) inmethod_channel_messaging_test.dart.firebase_messaging_test.dart(and mock stubs inmock.dart).firebase_messaging_web_test.dart.FlutterFirebaseMessagingNotificationDelegationTest.java.example/integration_test/e2e_test.dart.Verification
dart format --output=none --set-exit-if-changedon all 10 modified Dart files: passed (0 changed).dart analyze . --fatal-infosinfirebase_messaging_platform_interfaceandfirebase_messaging_web, anddart analyze lib test --fatal-infosinfirebase_messaging: no issues found.flutter testinfirebase_messaging_platform_interface: 121/121 passed.flutter testinfirebase_messaging: 24/24 passed.flutter test --platform chromeinfirebase_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 inFlutterFirebaseMessagingNotificationDelegationTest).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.yamland changelogs is not required.///).melos run analyze) does not report any problems on my PR.Breaking Change
Does your PR require plugin users to manually update their apps to accommodate your change?