Skip to content

Commit

Permalink
fix(messaging, android): stop app from crashing when DartCallback o…
Browse files Browse the repository at this point in the history
…r `Context` is `null` (#12842)
  • Loading branch information
russellwheatley committed May 29, 2024
1 parent 9659749 commit 1424757
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 1 deletion.
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

0 comments on commit 1424757

Please sign in to comment.