Skip to content

Commit

Permalink
fix(2637): addressing Firebase onBackgroundMessage handler bug which …
Browse files Browse the repository at this point in the history
…breaks plugin's platform channel for sending method call from Android.

Issue has been fixed by creating the two method-channels in plugin's android class. One static method channel is reserved for Android to Dart communication and other is for Dart to Android communication.

Fixes, firebase/flutterfire#9689
  • Loading branch information
shivamsharma2710 committed Feb 5, 2023
1 parent c5e2580 commit 8db6f34
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ public class CleverTapPlugin implements ActivityAware,

private Activity activity;

private MethodChannel channel;
private MethodChannel methodChannel;

private static MethodChannel nativeToDartMethodChannel;

private CleverTapAPI cleverTapAPI;

Expand All @@ -88,6 +90,7 @@ public class CleverTapPlugin implements ActivityAware,
public static void registerWith(Registrar registrar) {
CleverTapPlugin plugin = new CleverTapPlugin();
plugin.setupPlugin(registrar.context(), null, registrar);
plugin.activity = ((Activity) registrar.activeContext());
}

public CleverTapPlugin() {
Expand Down Expand Up @@ -141,7 +144,8 @@ public void onDetachedFromActivityForConfigChanges() {

@Override
public void onDetachedFromEngine(@NonNull FlutterPluginBinding binding) {
channel = null;
methodChannel = null;
nativeToDartMethodChannel = null;
}

@Override
Expand Down Expand Up @@ -955,7 +959,7 @@ private void initializeInbox(Result result) {
}

private void invokeMethodOnUiThread(final String methodName, final String cleverTapID) {
final MethodChannel channel = this.channel;
final MethodChannel channel = nativeToDartMethodChannel;
if (channel == null) {
Log.d(TAG, "methodChannel in invokeMethodOnUiThread(String) is null");
return;
Expand All @@ -970,7 +974,7 @@ private void invokeMethodOnUiThread(final String methodName, final String clever
}

private void invokeMethodOnUiThread(final String methodName, final Map map) {
final MethodChannel channel = this.channel;
final MethodChannel channel = nativeToDartMethodChannel;
if (channel == null) {
Log.d(TAG, "methodChannel in invokeMethodOnUiThread(Map) is null");
return;
Expand All @@ -980,7 +984,7 @@ private void invokeMethodOnUiThread(final String methodName, final Map map) {

@SuppressWarnings("SameParameterValue")
private void invokeMethodOnUiThread(final String methodName, final ArrayList list) {
final MethodChannel channel = this.channel;
final MethodChannel channel = nativeToDartMethodChannel;
if (channel == null) {
Log.d(TAG, "methodChannel in invokeMethodOnUiThread(ArrayList) is null");
return;
Expand Down Expand Up @@ -1433,16 +1437,25 @@ private void setPushToken(MethodCall call, Result result, PushType type) {
}
}

private void setupPlugin(Context context, BinaryMessenger messenger, Registrar registrar) {
private MethodChannel getMethodChannel(String channelName, BinaryMessenger messenger, Registrar registrar) {
if (registrar != null) {
//V1 setup
this.channel = new MethodChannel(registrar.messenger(), "clevertap_plugin");
this.activity = ((Activity) registrar.activeContext());
return new MethodChannel(registrar.messenger(), channelName);
} else {
//V2 setup
this.channel = new MethodChannel(messenger, "clevertap_plugin");
return new MethodChannel(messenger, channelName);
}
this.channel.setMethodCallHandler(this);
}

private void setupPlugin(Context context, BinaryMessenger messenger, Registrar registrar) {
this.methodChannel = getMethodChannel("clevertap_plugin", messenger, registrar);
if (nativeToDartMethodChannel == null) {
// set nativeToDartMethodChannel channel once, as it has to be static field
// and per https://github.com/firebase/flutterfire/issues/9689 because other
// plugins can create multiple instances of the plugin.
nativeToDartMethodChannel = getMethodChannel("clevertap_plugin/native_to_dart", messenger, registrar);
}
this.methodChannel.setMethodCallHandler(this);
this.context = context.getApplicationContext();
this.cleverTapAPI = CleverTapAPI.getDefaultInstance(this.context);
if (this.cleverTapAPI != null) {
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ packages:
path: ".."
relative: true
source: path
version: "1.5.4"
version: "1.5.5"
clock:
dependency: transitive
description:
Expand Down
3 changes: 2 additions & 1 deletion lib/clevertap_plugin.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,15 @@ class CleverTapPlugin {
cleverTapPushClickedPayloadReceivedHandler;

static const MethodChannel _channel = const MethodChannel('clevertap_plugin');
static const MethodChannel _nativeToDartMethodChannel = const MethodChannel('clevertap_plugin/native_to_dart');

static final CleverTapPlugin _clevertapPlugin =
new CleverTapPlugin._internal();

factory CleverTapPlugin() => _clevertapPlugin;

CleverTapPlugin._internal() {
_channel.setMethodCallHandler(_platformCallHandler);
_nativeToDartMethodChannel.setMethodCallHandler(_platformCallHandler);
}

Future _platformCallHandler(MethodCall call) async {
Expand Down

0 comments on commit 8db6f34

Please sign in to comment.