Skip to content

Commit c319281

Browse files
committed
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`.
1 parent a9660d4 commit c319281

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

android/src/main/java/com/onesignal/flutter/OneSignalPlugin.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import android.annotation.SuppressLint;
44
import android.content.Context;
55

6+
import io.flutter.embedding.engine.plugins.activity.ActivityAware;
7+
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
8+
69
import com.onesignal.OSDeviceState;
710
import com.onesignal.OSEmailSubscriptionObserver;
811
import com.onesignal.OSEmailSubscriptionStateChanges;
@@ -42,6 +45,7 @@ public class OneSignalPlugin
4245
extends FlutterRegistrarResponder
4346
implements FlutterPlugin,
4447
MethodCallHandler,
48+
ActivityAware,
4549
OneSignal.OSNotificationOpenedHandler,
4650
OneSignal.OSInAppMessageClickHandler,
4751
OSSubscriptionObserver,
@@ -96,12 +100,29 @@ private void onDetachedFromEngine() {
96100
OneSignal.setInAppMessageClickHandler(null);
97101
}
98102

103+
@Override
104+
public void onAttachedToActivity(@NonNull ActivityPluginBinding binding) {
105+
this.context = binding.getActivity();
106+
}
107+
108+
@Override
109+
public void onDetachedFromActivity() {
110+
}
111+
112+
@Override
113+
public void onReattachedToActivityForConfigChanges(@NonNull ActivityPluginBinding binding) {
114+
}
115+
116+
@Override
117+
public void onDetachedFromActivityForConfigChanges() {
118+
}
119+
99120
// This static method is only to remain compatible with apps that don’t use the v2 Android embedding.
100121
@Deprecated()
101122
@SuppressLint("Registrar")
102123
public static void registerWith(Registrar registrar) {
103124
final OneSignalPlugin plugin = new OneSignalPlugin();
104-
plugin.init(registrar.context(), registrar.messenger());
125+
plugin.init(registrar.activeContext(), registrar.messenger());
105126

106127
// Create a callback for the flutterRegistrar to connect the applications onDestroy
107128
registrar.addViewDestroyListener(new PluginRegistry.ViewDestroyListener() {

0 commit comments

Comments
 (0)