Repository navigation
fix(messaging,android): skip the background service when no handler is registered #18942
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
Merged
SelaseKay
merged 5 commits into
main
from
fix/messaging-android-background-handler-gate
Nov 16, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
af15582
fix(messaging,android): skip the background service when no handler i…
SelaseKay 7d296ec
test(messaging,android): cover the background handler gate on a device
SelaseKay a8c41d2
chore(messaging): drop comments that restate the background gate
SelaseKay 073cebe
chore(messaging): drop the remaining comments that restate the gate
SelaseKay ffc1ae7
fix(messaging,android): log a skipped background push
SelaseKay 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
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
46 changes: 46 additions & 0 deletions
46
...d/src/main/java/io/flutter/plugins/firebase/messaging/BackgroundHandlerStartDecision.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,46 @@ | ||
| // 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; | ||
|
|
||
| final class BackgroundHandlerStartDecision { | ||
| enum Action { | ||
| START, | ||
| SKIP, | ||
| SKIP_AND_CLEAR | ||
| } | ||
|
|
||
| private BackgroundHandlerStartDecision() {} | ||
|
|
||
| static Action decide(long callbackHandle, Boolean manifestEnabled) { | ||
| if (Boolean.FALSE.equals(manifestEnabled)) { | ||
| return Action.SKIP_AND_CLEAR; | ||
| } | ||
| if (callbackHandle == 0L) { | ||
| return Action.SKIP; | ||
| } | ||
| return Action.START; | ||
| } | ||
|
|
||
| /** | ||
| * The manifest value for the background handler, or null when the entry is absent or not a | ||
| * boolean. Android stores {@code android:value="false"} as a boolean, and sometimes as the string | ||
| * {@code "false"}. | ||
| */ | ||
| static Boolean manifestValue(Object raw) { | ||
| if (raw instanceof Boolean) { | ||
| return (Boolean) raw; | ||
| } | ||
| if (raw instanceof String) { | ||
| String value = (String) raw; | ||
| if ("true".equalsIgnoreCase(value)) { | ||
| return Boolean.TRUE; | ||
| } | ||
| if ("false".equalsIgnoreCase(value)) { | ||
| return Boolean.FALSE; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
| } |
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
113 changes: 113 additions & 0 deletions
113
...in/java/io/flutter/plugins/firebase/messaging/FlutterFirebaseMessagingBackgroundGate.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,113 @@ | ||
| // 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 android.content.Context; | ||
| import android.content.pm.ApplicationInfo; | ||
| import android.content.pm.PackageManager; | ||
| import android.util.Log; | ||
| import androidx.annotation.Nullable; | ||
| import androidx.annotation.VisibleForTesting; | ||
| import java.util.concurrent.CountDownLatch; | ||
| import java.util.concurrent.Executor; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| final class FlutterFirebaseMessagingBackgroundGate { | ||
| static final String METADATA_KEY = "firebase_messaging_background_handler_enabled"; | ||
|
|
||
| private static final String TAG = "FLTFireBGExecutor"; | ||
| private static final Object manifestLock = new Object(); | ||
| private static final Executor clearExecutor = Executors.newSingleThreadExecutor(); | ||
|
|
||
| private static boolean manifestRead = false; | ||
| @Nullable private static Boolean manifestEnabled; | ||
|
|
||
| private FlutterFirebaseMessagingBackgroundGate() {} | ||
|
|
||
| static BackgroundHandlerStartDecision.Action decide(Context context) { | ||
| Context appContext = applicationContext(context); | ||
| return BackgroundHandlerStartDecision.decide( | ||
| FlutterFirebaseMessagingBackgroundExecutor.readPluginCallbackHandle(appContext), | ||
| manifestEnabled(appContext)); | ||
| } | ||
|
|
||
| static boolean handlerDisabled(Context context) { | ||
| return Boolean.FALSE.equals(manifestEnabled(applicationContext(context))); | ||
| } | ||
|
|
||
| static void clearHandles(Context context) { | ||
| FlutterFirebaseMessagingBackgroundExecutor.clearCallbackHandles(applicationContext(context)); | ||
| } | ||
|
|
||
| /** Preference writes must not run on the main thread. Service startup calls this instead. */ | ||
| static void clearHandlesAsync(Context context) { | ||
| Context appContext = applicationContext(context); | ||
| clearExecutor.execute(() -> clearHandles(appContext)); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static void awaitClearForTests() throws InterruptedException { | ||
| CountDownLatch latch = new CountDownLatch(1); | ||
| clearExecutor.execute(latch::countDown); | ||
| if (!latch.await(5, TimeUnit.SECONDS)) { | ||
| throw new AssertionError("callback handle clear did not finish"); | ||
| } | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| static void resetForTests() { | ||
| synchronized (manifestLock) { | ||
| manifestRead = false; | ||
| manifestEnabled = null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Boolean manifestEnabled(Context context) { | ||
| synchronized (manifestLock) { | ||
| if (manifestRead) { | ||
| return manifestEnabled; | ||
| } | ||
| } | ||
| if (context == null) { | ||
| return null; | ||
| } | ||
| Boolean value = loadManifestEnabled(context); | ||
| synchronized (manifestLock) { | ||
| if (!manifestRead) { | ||
| manifestEnabled = value; | ||
| manifestRead = true; | ||
| } | ||
| return manifestEnabled; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Boolean loadManifestEnabled(Context context) { | ||
| try { | ||
| ApplicationInfo info = | ||
| context | ||
| .getPackageManager() | ||
| .getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA); | ||
| if (info.metaData == null || !info.metaData.containsKey(METADATA_KEY)) { | ||
| return null; | ||
| } | ||
| return BackgroundHandlerStartDecision.manifestValue(info.metaData.get(METADATA_KEY)); | ||
| } catch (PackageManager.NameNotFoundException exception) { | ||
| Log.w(TAG, "Could not read " + METADATA_KEY + " from the manifest.", exception); | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
| @Nullable | ||
| private static Context applicationContext(Context context) { | ||
| if (context == null) { | ||
| return null; | ||
| } | ||
| Context appContext = context.getApplicationContext(); | ||
| return appContext != null ? appContext : context; | ||
| } | ||
| } | ||
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.
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.
On some custom or unstable Android ROMs, calling
PackageManagermethods can throw other runtime exceptions (such asDeadSystemExceptionor generalRuntimeException). It is safer to catchExceptiongenerally to prevent unexpected crashes when reading manifest metadata.