appsflyer-flutter-plugin icon indicating copy to clipboard operation
appsflyer-flutter-plugin copied to clipboard

Manage multiple instance of plugin

Open jogboms opened this issue 2 years ago • 0 comments

Issue

When this plugin is used together with package:firebase_messaging, it creates multiple instances of the plugin. Why? because firebase_messaging creates a new FlutterEngine which in turn registers the plugin for the second time thus creating a new instance. This would also be the case when using multiple FlutterEngines anyway.

Before this change, when the app is placed in the background using the back button, the initial instance even though alive contains an already detached instance of the engine hence the messenger initialized with the instance's MethodChannel is detached from the framework and as such the callbacks are never called. This is a leak I currently could not track down with limited time.

Resolution

Even though I might consider this level of instance book-keeping somewhat of a "hack", I actually found a similar pattern from other plugins like audio_session. I could spend for time investigating the leak but for now, I only have this as a quick solution.

I believe the listeners are only created once in the init phase on the native SDK, I can't tell as it isn't OSS but it would be nice if that could be resync somehow.

It might also be related to https://github.com/AppsFlyerSDK/appsflyer-flutter-plugin/issues/172 https://github.com/AppsFlyerSDK/appsflyer-flutter-plugin/issues/141 https://github.com/AppsFlyerSDK/appsflyer-flutter-plugin/issues/109

jogboms avatar Mar 15 '22 07:03 jogboms