Skip to content

Commit

Permalink
feat(messaging): use FlutterEngineGroup to improve performance of bac…
Browse files Browse the repository at this point in the history
…kground handlers (#9867)

Co-authored-by: Russell Wheatley <russellwheatley85@gmail.com>
  • Loading branch information
Lyokone and russellwheatley committed Nov 8, 2022
1 parent 294e108 commit 2e9deac
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 32 deletions.
Expand Up @@ -14,7 +14,7 @@
import androidx.annotation.NonNull;
import com.google.firebase.messaging.RemoteMessage;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.FlutterEngineGroup;
import io.flutter.embedding.engine.dart.DartExecutor;
import io.flutter.embedding.engine.dart.DartExecutor.DartCallback;
import io.flutter.embedding.engine.loader.FlutterLoader;
Expand All @@ -24,7 +24,6 @@
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.view.FlutterCallbackInformation;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
Expand Down Expand Up @@ -115,7 +114,7 @@ public void startBackgroundIsolate() {
if (isNotRunning()) {
long callbackHandle = getPluginCallbackHandle();
if (callbackHandle != 0) {
startBackgroundIsolate(callbackHandle, null);
startBackgroundIsolate(callbackHandle);
}
}
}
Expand All @@ -138,7 +137,7 @@ public void startBackgroundIsolate() {
* handle does not resolve to a Dart callback then this method does nothing.
* </ul>
*/
public void startBackgroundIsolate(long callbackHandle, FlutterShellArgs shellArgs) {
public void startBackgroundIsolate(long callbackHandle) {
if (backgroundFlutterEngine != null) {
Log.e(TAG, "Background isolate already started.");
return;
Expand All @@ -157,19 +156,10 @@ public void startBackgroundIsolate(long callbackHandle, FlutterShellArgs shellAr
String appBundlePath = loader.findAppBundlePath();
AssetManager assets = ContextHolder.getApplicationContext().getAssets();
if (isNotRunning()) {
if (shellArgs != null) {
Log.i(
TAG,
"Creating background FlutterEngine instance, with args: "
+ Arrays.toString(shellArgs.toArray()));
backgroundFlutterEngine =
new FlutterEngine(
ContextHolder.getApplicationContext(), shellArgs.toArray());
} else {
Log.i(TAG, "Creating background FlutterEngine instance.");
backgroundFlutterEngine =
new FlutterEngine(ContextHolder.getApplicationContext());
}
Log.i(TAG, "Creating background FlutterEngine instance.");
backgroundFlutterEngine =
new FlutterEngineGroup(ContextHolder.getApplicationContext())
.createAndRunDefaultEngine(ContextHolder.getApplicationContext());
// We need to create an instance of `FlutterEngine` before looking up the
// callback. If we don't, the callback cache won't be initialized and the
// lookup will fail.
Expand Down
Expand Up @@ -10,7 +10,6 @@
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.firebase.messaging.RemoteMessage;
import io.flutter.embedding.engine.FlutterShellArgs;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -52,13 +51,13 @@ public static void enqueueMessageProcessing(Context context, Intent messageInten
* </ul>
*/
@SuppressWarnings("JavadocReference")
public static void startBackgroundIsolate(long callbackHandle, FlutterShellArgs shellArgs) {
public static void startBackgroundIsolate(long callbackHandle) {
if (flutterBackgroundExecutor != null) {
Log.w(TAG, "Attempted to start a duplicate background isolate. Returning...");
return;
}
flutterBackgroundExecutor = new FlutterFirebaseMessagingBackgroundExecutor();
flutterBackgroundExecutor.startBackgroundIsolate(callbackHandle, shellArgs);
flutterBackgroundExecutor.startBackgroundIsolate(callbackHandle);
}

/**
Expand Down
Expand Up @@ -25,7 +25,6 @@
import com.google.firebase.FirebaseApp;
import com.google.firebase.messaging.FirebaseMessaging;
import com.google.firebase.messaging.RemoteMessage;
import io.flutter.embedding.engine.FlutterShellArgs;
import io.flutter.embedding.engine.plugins.FlutterPlugin;
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
Expand Down Expand Up @@ -430,19 +429,9 @@ public void onMethodCall(final MethodCall call, @NonNull final Result result) {
userCallbackHandle = Long.valueOf((Integer) arg2);
}

FlutterShellArgs shellArgs = null;
if (mainActivity != null) {
// Supports both Flutter Activity types:
// io.flutter.embedding.android.FlutterFragmentActivity
// io.flutter.embedding.android.FlutterActivity
// We could use `getFlutterShellArgs()` but this is only available on `FlutterActivity`.
shellArgs = FlutterShellArgs.fromIntent(mainActivity.getIntent());
}

FlutterFirebaseMessagingBackgroundService.setCallbackDispatcher(pluginCallbackHandle);
FlutterFirebaseMessagingBackgroundService.setUserCallbackHandle(userCallbackHandle);
FlutterFirebaseMessagingBackgroundService.startBackgroundIsolate(
pluginCallbackHandle, shellArgs);
FlutterFirebaseMessagingBackgroundService.startBackgroundIsolate(pluginCallbackHandle);
methodCallTask = Tasks.forResult(null);
break;
case "Messaging#getInitialMessage":
Expand Down

0 comments on commit 2e9deac

Please sign in to comment.