Skip to content

fix(messaging, android): stop app from crashing when DartCallback or Context is null #12842

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
merged 5 commits into from
May 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public class FlutterFirebaseMessagingBackgroundExecutor implements MethodCallHan
*/
public static void setCallbackDispatcher(long callbackHandle) {
Context context = ContextHolder.getApplicationContext();
if (context == null) {
Log.e(TAG, "Context is null, cannot continue.");
return;
}
SharedPreferences prefs =
context.getSharedPreferences(FlutterFirebaseMessagingUtils.SHARED_PREFERENCES_KEY, 0);
prefs.edit().putLong(CALLBACK_HANDLE_KEY, callbackHandle).apply();
Expand Down Expand Up @@ -176,6 +180,12 @@ public void startBackgroundIsolate(long callbackHandle, FlutterShellArgs shellAr
// lookup will fail.
FlutterCallbackInformation flutterCallback =
FlutterCallbackInformation.lookupCallbackInformation(callbackHandle);

if (flutterCallback == null) {
Log.e(TAG, "Failed to find registered callback");
return;
}

DartExecutor executor = backgroundFlutterEngine.getDartExecutor();
initializeMethodChannel(executor);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ private void initInstance(BinaryMessenger messenger) {

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
ContextHolder.setApplicationContext(binding.getApplicationContext());
initInstance(binding.getBinaryMessenger());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,12 @@ public class FlutterFirebaseMessagingReceiver extends BroadcastReceiver {
public void onReceive(Context context, Intent intent) {
Log.d(TAG, "broadcast received for message");
if (ContextHolder.getApplicationContext() == null) {
ContextHolder.setApplicationContext(context.getApplicationContext());
Context aContext = context;
if (context.getApplicationContext() != null) {
aContext = context.getApplicationContext();
}

ContextHolder.setApplicationContext(aContext);
}

if (intent.getExtras() == null) {
Expand Down
Loading