From c319281fd5b3c48d912630946601df37ed546375 Mon Sep 17 00:00:00 2001 From: Nan Date: Tue, 8 Feb 2022 22:16:44 -0800 Subject: [PATCH] Use `ActivityAware` to get the Activity context After the changes to support Android V2 embedding, the plugin is set up in `onAttachedToEngine` and the Application Context is not an instance of Activity. When this context is eventually passed to `setAppId`, we end up with the current activity is null which leads to some problems. This is only a problem on the first run of the app. This commit implements the `ActivityAware` interface and in `onAttachedToActivity`, updates the context to the new activity (https://docs.flutter.dev/development/packages-and-plugins/plugin-api-migration). In addition, use `activeContext()` instead of `context()` for the registrar for Flutter's v1 embedding to get the application context. Doing some testing shows the former gives, for example, the `MainActivity` while the latter gives `FlutterApplication`. --- .../onesignal/flutter/OneSignalPlugin.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java b/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java index a63622c7..97dcd1d4 100644 --- a/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java +++ b/android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java @@ -3,6 +3,9 @@ import android.annotation.SuppressLint; import android.content.Context; +import io.flutter.embedding.engine.plugins.activity.ActivityAware; +import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding; + import com.onesignal.OSDeviceState; import com.onesignal.OSEmailSubscriptionObserver; import com.onesignal.OSEmailSubscriptionStateChanges; @@ -42,6 +45,7 @@ public class OneSignalPlugin extends FlutterRegistrarResponder implements FlutterPlugin, MethodCallHandler, + ActivityAware, OneSignal.OSNotificationOpenedHandler, OneSignal.OSInAppMessageClickHandler, OSSubscriptionObserver, @@ -96,12 +100,29 @@ private void onDetachedFromEngine() { OneSignal.setInAppMessageClickHandler(null); } + @Override + public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) { + this.context = binding.getActivity(); + } + + @Override + public void onDetachedFromActivity() { + } + + @Override + public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) { + } + + @Override + public void onDetachedFromActivityForConfigChanges() { + } + // This static method is only to remain compatible with apps that don’t use the v2 Android embedding. @Deprecated() @SuppressLint("Registrar") public static void registerWith(Registrar registrar) { final OneSignalPlugin plugin = new OneSignalPlugin(); - plugin.init(registrar.context(), registrar.messenger()); + plugin.init(registrar.activeContext(), registrar.messenger()); // Create a callback for the flutterRegistrar to connect the applications onDestroy registrar.addViewDestroyListener(new PluginRegistry.ViewDestroyListener() {