Skip to content

Commit

Permalink
Addressing Firebase onBackgroundMessage bug - untested
Browse files Browse the repository at this point in the history
firebase/flutterfire#9689

Made `channel` a regular field (non-static) and only set `backgroundChannel` (a static field) if it is null.  This should keep the first `backgroundChannel` alive.

Note that the `backgroundChannel` has to be static because it is called from the DownloadWorker, and it has no reference to the plugin.
  • Loading branch information
781flyingdutchman committed Jan 23, 2023
1 parent 4504ce8 commit 33c47a9
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ import kotlin.concurrent.write

/** BackgroundDownloaderPlugin */
class BackgroundDownloaderPlugin : FlutterPlugin, MethodCallHandler {
private var channel: MethodChannel? = null

companion object {
const val TAG = "BackgroundDownloaderPlugin"
const val keyTasksMap = "com.bbflight.background_downloader.taskMap"
private var channel: MethodChannel? = null
var backgroundChannel: MethodChannel? = null
val prefsLock = ReentrantReadWriteLock()
private lateinit var workManager: WorkManager
Expand All @@ -30,10 +31,15 @@ class BackgroundDownloaderPlugin : FlutterPlugin, MethodCallHandler {
}

override fun onAttachedToEngine(flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
if (backgroundChannel == null) {
// only set background channel once, as it has to be static field
// and per https://github.com/firebase/flutterfire/issues/9689 other
// plugins can create multiple instances of the plugin
backgroundChannel = MethodChannel(flutterPluginBinding.binaryMessenger,
"com.bbflight.background_downloader.background")
}
channel = MethodChannel(flutterPluginBinding.binaryMessenger,
"com.bbflight.background_downloader")
backgroundChannel = MethodChannel(flutterPluginBinding.binaryMessenger,
"com.bbflight.background_downloader.background")
channel?.setMethodCallHandler(this)
workManager = WorkManager.getInstance(flutterPluginBinding.applicationContext)
prefs = PreferenceManager.getDefaultSharedPreferences(
Expand All @@ -50,7 +56,6 @@ class BackgroundDownloaderPlugin : FlutterPlugin, MethodCallHandler {
override fun onDetachedFromEngine(binding: FlutterPlugin.FlutterPluginBinding) {
channel?.setMethodCallHandler(null)
channel = null
backgroundChannel = null
}

override fun onMethodCall(call: MethodCall, result: Result) {
Expand Down
4 changes: 4 additions & 0 deletions example/integration_test/downloader_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ void main() {
} on FileSystemException {}
});

tearDown(() async {
FileDownloader.destroy();
});

group('Initialization', () {
test('initialize', () {
// confirm asserts work
Expand Down

0 comments on commit 33c47a9

Please sign in to comment.