Repository navigation
feat(messaging, android): expose Android notification delegation APIs #19019
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ReoAndo
wants to merge
1
commit into
firebase:main
Choose a base branch
from
ReoAndo:feat/messaging-android-notification-delegation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
220 changes: 220 additions & 0 deletions
220
...lutter/plugins/firebase/messaging/FlutterFirebaseMessagingNotificationDelegationTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,220 @@ | ||
| // Copyright 2026, the Chromium project authors. Please see the AUTHORS file | ||
| // for details. All rights reserved. Use of this source code is governed by a | ||
| // BSD-style license that can be found in the LICENSE file. | ||
|
|
||
| package io.flutter.plugins.firebase.messaging; | ||
|
|
||
| import static org.junit.Assert.assertEquals; | ||
| import static org.junit.Assert.assertFalse; | ||
| import static org.junit.Assert.assertNotNull; | ||
| import static org.junit.Assert.assertNull; | ||
| import static org.junit.Assert.assertTrue; | ||
|
|
||
| import android.os.Looper; | ||
| import androidx.annotation.Nullable; | ||
| import com.google.android.gms.tasks.Task; | ||
| import com.google.android.gms.tasks.Tasks; | ||
| import io.flutter.plugin.common.MethodCall; | ||
| import io.flutter.plugin.common.MethodChannel; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import org.junit.Test; | ||
| import org.junit.runner.RunWith; | ||
| import org.robolectric.RobolectricTestRunner; | ||
| import org.robolectric.Shadows; | ||
| import org.robolectric.annotation.Config; | ||
|
|
||
| @RunWith(RobolectricTestRunner.class) | ||
| @Config(sdk = 28) | ||
| public class FlutterFirebaseMessagingNotificationDelegationTest { | ||
|
|
||
| private static final class RecordingResult implements MethodChannel.Result { | ||
| final AtomicBoolean completed = new AtomicBoolean(false); | ||
| final AtomicBoolean succeeded = new AtomicBoolean(false); | ||
| final AtomicReference<Object> successValue = new AtomicReference<>(); | ||
| final AtomicReference<String> errorCode = new AtomicReference<>(); | ||
| final AtomicReference<String> errorMessage = new AtomicReference<>(); | ||
| final AtomicReference<Object> errorDetails = new AtomicReference<>(); | ||
|
|
||
| @Override | ||
| public void success(@Nullable Object result) { | ||
| successValue.set(result); | ||
| succeeded.set(true); | ||
| completed.set(true); | ||
| } | ||
|
|
||
| @Override | ||
| public void error(String code, @Nullable String message, @Nullable Object details) { | ||
| errorCode.set(code); | ||
| errorMessage.set(message); | ||
| errorDetails.set(details); | ||
| completed.set(true); | ||
| } | ||
|
|
||
| @Override | ||
| public void notImplemented() { | ||
| completed.set(true); | ||
| } | ||
|
|
||
| void await() throws InterruptedException { | ||
| long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); | ||
| while (!completed.get() && System.nanoTime() < deadline) { | ||
| Shadows.shadowOf(Looper.getMainLooper()).idle(); | ||
| Thread.sleep(10); | ||
| } | ||
| Shadows.shadowOf(Looper.getMainLooper()).idle(); | ||
| assertTrue("Timed out waiting for MethodChannel.Result", completed.get()); | ||
| } | ||
| } | ||
|
|
||
| private static final class FakeDelegationHandler | ||
| implements FlutterFirebaseMessagingPlugin.NotificationDelegationHandler { | ||
| boolean enabled; | ||
| Boolean lastSetEnabled; | ||
| RuntimeException setError; | ||
| RuntimeException getError; | ||
|
|
||
| @Override | ||
| public Task<Void> setEnabled(Map<String, Object> arguments, boolean enabled) { | ||
| lastSetEnabled = enabled; | ||
| if (setError != null) { | ||
| return Tasks.forException(setError); | ||
| } | ||
| this.enabled = enabled; | ||
| return Tasks.forResult(null); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEnabled(Map<String, Object> arguments) { | ||
| if (getError != null) { | ||
| throw getError; | ||
| } | ||
| return enabled; | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void setNotificationDelegationEnabledUpdatesState() throws Exception { | ||
| FakeDelegationHandler handler = new FakeDelegationHandler(); | ||
| FlutterFirebaseMessagingPlugin plugin = new FlutterFirebaseMessagingPlugin(handler); | ||
|
|
||
| Map<String, Object> enableArgs = new HashMap<>(); | ||
| enableArgs.put("appName", "[DEFAULT]"); | ||
| enableArgs.put("enabled", true); | ||
| MethodCall enableCall = | ||
| new MethodCall("Messaging#setNotificationDelegationEnabled", enableArgs); | ||
| RecordingResult enableResult = new RecordingResult(); | ||
|
|
||
| plugin.onMethodCall(enableCall, enableResult); | ||
| enableResult.await(); | ||
|
|
||
| assertTrue(enableResult.succeeded.get()); | ||
| assertNull(enableResult.successValue.get()); | ||
| assertEquals(Boolean.TRUE, handler.lastSetEnabled); | ||
|
|
||
| Map<String, Object> disableArgs = new HashMap<>(); | ||
| disableArgs.put("appName", "[DEFAULT]"); | ||
| disableArgs.put("enabled", false); | ||
| MethodCall disableCall = | ||
| new MethodCall("Messaging#setNotificationDelegationEnabled", disableArgs); | ||
| RecordingResult disableResult = new RecordingResult(); | ||
|
|
||
| plugin.onMethodCall(disableCall, disableResult); | ||
| disableResult.await(); | ||
|
|
||
| assertTrue(disableResult.succeeded.get()); | ||
| assertNull(disableResult.successValue.get()); | ||
| assertEquals(Boolean.FALSE, handler.lastSetEnabled); | ||
| } | ||
|
|
||
| @Test | ||
| public void setNotificationDelegationEnabledErrorsWhenArgumentMissing() throws Exception { | ||
| FakeDelegationHandler handler = new FakeDelegationHandler(); | ||
| FlutterFirebaseMessagingPlugin plugin = new FlutterFirebaseMessagingPlugin(handler); | ||
|
|
||
| Map<String, Object> args = new HashMap<>(); | ||
| args.put("appName", "[DEFAULT]"); | ||
| MethodCall call = new MethodCall("Messaging#setNotificationDelegationEnabled", args); | ||
| RecordingResult result = new RecordingResult(); | ||
|
|
||
| plugin.onMethodCall(call, result); | ||
| result.await(); | ||
|
|
||
| assertFalse(result.succeeded.get()); | ||
| assertEquals("firebase_messaging", result.errorCode.get()); | ||
| assertNotNull(result.errorDetails.get()); | ||
| } | ||
|
|
||
| @Test | ||
| public void setNotificationDelegationEnabledPropagatesTaskFailure() throws Exception { | ||
| FakeDelegationHandler handler = new FakeDelegationHandler(); | ||
| handler.setError = new IllegalStateException("delegation failed"); | ||
| FlutterFirebaseMessagingPlugin plugin = new FlutterFirebaseMessagingPlugin(handler); | ||
|
|
||
| Map<String, Object> args = new HashMap<>(); | ||
| args.put("appName", "[DEFAULT]"); | ||
| args.put("enabled", true); | ||
| MethodCall call = new MethodCall("Messaging#setNotificationDelegationEnabled", args); | ||
| RecordingResult result = new RecordingResult(); | ||
|
|
||
| plugin.onMethodCall(call, result); | ||
| result.await(); | ||
|
|
||
| assertFalse(result.succeeded.get()); | ||
| assertEquals("firebase_messaging", result.errorCode.get()); | ||
| assertTrue(result.errorMessage.get().contains("delegation failed")); | ||
| } | ||
|
|
||
| @Test | ||
| public void isNotificationDelegationEnabledReturnsCurrentState() throws Exception { | ||
| String key = FlutterFirebaseMessagingUtils.IS_NOTIFICATION_DELEGATION_ENABLED; | ||
| FakeDelegationHandler handler = new FakeDelegationHandler(); | ||
| handler.enabled = true; | ||
| FlutterFirebaseMessagingPlugin plugin = new FlutterFirebaseMessagingPlugin(handler); | ||
|
|
||
| Map<String, Object> args = new HashMap<>(); | ||
| args.put("appName", "[DEFAULT]"); | ||
| MethodCall call = new MethodCall("Messaging#isNotificationDelegationEnabled", args); | ||
|
|
||
| RecordingResult enabledResult = new RecordingResult(); | ||
| plugin.onMethodCall(call, enabledResult); | ||
| enabledResult.await(); | ||
|
|
||
| assertTrue(enabledResult.succeeded.get()); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> enabledMap = (Map<String, Object>) enabledResult.successValue.get(); | ||
| assertEquals(Boolean.TRUE, enabledMap.get(key)); | ||
|
|
||
| handler.enabled = false; | ||
| RecordingResult disabledResult = new RecordingResult(); | ||
| plugin.onMethodCall(call, disabledResult); | ||
| disabledResult.await(); | ||
|
|
||
| assertTrue(disabledResult.succeeded.get()); | ||
| @SuppressWarnings("unchecked") | ||
| Map<String, Object> disabledMap = (Map<String, Object>) disabledResult.successValue.get(); | ||
| assertEquals(Boolean.FALSE, disabledMap.get(key)); | ||
| } | ||
|
|
||
| @Test | ||
| public void isNotificationDelegationEnabledPropagatesException() throws Exception { | ||
| FakeDelegationHandler handler = new FakeDelegationHandler(); | ||
| handler.getError = new IllegalStateException("status failed"); | ||
| FlutterFirebaseMessagingPlugin plugin = new FlutterFirebaseMessagingPlugin(handler); | ||
|
|
||
| Map<String, Object> args = new HashMap<>(); | ||
| args.put("appName", "[DEFAULT]"); | ||
| MethodCall call = new MethodCall("Messaging#isNotificationDelegationEnabled", args); | ||
| RecordingResult result = new RecordingResult(); | ||
|
|
||
| plugin.onMethodCall(call, result); | ||
| result.await(); | ||
|
|
||
| assertFalse(result.succeeded.get()); | ||
| assertEquals("firebase_messaging", result.errorCode.get()); | ||
| assertEquals("status failed", result.errorMessage.get()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
|
|
@@ -391,6 +391,32 @@ void main() { | |||||||
| ); | ||||||||
| }); | ||||||||
|
|
||||||||
| group('notification delegation', () { | ||||||||
| test( | ||||||||
| 'can disable and enable notification delegation on Android', | ||||||||
| () async { | ||||||||
| final originalValue = await messaging | ||||||||
| .isNotificationDelegationEnabled(); | ||||||||
| try { | ||||||||
| await messaging.setNotificationDelegationEnabled(false); | ||||||||
| expect(await messaging.isNotificationDelegationEnabled(), isFalse); | ||||||||
|
|
||||||||
| await messaging.setNotificationDelegationEnabled(true); | ||||||||
| final expected = sdkInt != null && sdkInt! >= 29; | ||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Avoid using the
Suggested change
References
|
||||||||
| expect(await messaging.isNotificationDelegationEnabled(), expected); | ||||||||
| } finally { | ||||||||
| await messaging.setNotificationDelegationEnabled(originalValue); | ||||||||
| } | ||||||||
| }, | ||||||||
| skip: kIsWeb || defaultTargetPlatform != TargetPlatform.android, | ||||||||
| ); | ||||||||
|
|
||||||||
| test('returns false and is a no-op on non-Android platforms', () async { | ||||||||
| await messaging.setNotificationDelegationEnabled(true); | ||||||||
| expect(await messaging.isNotificationDelegationEnabled(), isFalse); | ||||||||
| }, skip: !kIsWeb && defaultTargetPlatform == TargetPlatform.android); | ||||||||
| }); | ||||||||
|
|
||||||||
| test('a background message with no registered handler does not start ' | ||||||||
| 'the background service', () async { | ||||||||
| final probe = await probeBackgroundDelivery( | ||||||||
|
|
||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To prevent a potential
NullPointerException, perform a null check onargumentsbefore attempting to retrieve the'enabled'key.