Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Event listeners stops working after another plugin initialization #550

Open
estevez-dev opened this issue Sep 11, 2019 · 16 comments
Open

Event listeners stops working after another plugin initialization #550

estevez-dev opened this issue Sep 11, 2019 · 16 comments

Comments

@estevez-dev
Copy link

estevez-dev commented Sep 11, 2019

System info

Issue occurs on: Android
Plugin version: 0.3.7
Flutter doctor output:

[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Linux, locale en_US.UTF-8)
 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
[✓] Android Studio (version 3.5)
[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.

Steps to Reproduce

  1. Use webview plugin with onUrlChanged listener attached
  2. Add android_alarm_manager or workmanager plugin to your project
  3. Initialize android_alarm_manager or workmanager and create an alrarm or task.

main.dart example:

...
import 'package:flutter_webview_plugin/flutter_webview_plugin.dart';
import 'package:workmanager/workmanager.dart' as workManager;

...
void callbackDispatcher() {
  workManager.Workmanager.executeTask((backgroundTask) {
    print("Native called background task: $backgroundTask"); //simpleTask will be emitted here.
    return Future.value(true);
  });
}

void main() async {
  FlutterError.onError = (errorDetails) {
    Logger.e( "${errorDetails.exception}");
    if (Logger.isInDebugMode) {
      FlutterError.dumpErrorToConsole(errorDetails);
    }
  };

  runZoned(() {
    workManager.Workmanager.initialize(
        callbackDispatcher,
        isInDebugMode: true
    ); 
    workManager.Workmanager.registerPeriodicTask("1", "simpleTask", frequency: Duration(hours: 1)); //Comment this line to make webview events work
    runApp(new HAClientApp());

  }, onError: (error, stack) {
    Logger.e("$error");
    Logger.e("$stack");
    if (Logger.isInDebugMode) {
      debugPrint("$stack");
    }
  });
}

class HAClientApp extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: "Application",
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      initialRoute: "/",
      routes: {
        "/": (context) => MainPage(title: 'HA Client'),
        "/login": (context) => WebviewScaffold(
          url: "${ConnectionManager().oauthUrl}",
          appBar: new AppBar(
            title: new Text("Login"),
          ),
        ),
      },
    );
  }
}
...

Webview calling somewhere in code:

final flutterWebviewPlugin = new FlutterWebviewPlugin();
flutterWebviewPlugin.onUrlChanged.listen((String url) {
    print("Url changed to: $url");
});
Navigator.of(context).pushNamed('/login');

Comment workManager.Workmanager.registerPeriodicTask and webview onUrlChanged will start working. Also need to mention that android_alarm_manager and workmanager register itself in java application class. For example:

package com.keyboardcrumbs.hassclient;

import io.flutter.app.FlutterApplication;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.PluginRegistrantCallback;
import io.flutter.plugins.GeneratedPluginRegistrant;
import be.tramckrijte.workmanager.WorkmanagerPlugin;

public class Application extends FlutterApplication implements PluginRegistrantCallback {
    @Override
    public void onCreate() {
        super.onCreate();
        WorkmanagerPlugin.setPluginRegistrantCallback(this);
    }

    @Override
    public void registerWith(PluginRegistry registry) {
        GeneratedPluginRegistrant.registerWith(registry);
    }
}

Logs

No errors in log

$ flutter doctor -v
[✓] Flutter (Channel stable, v1.7.8+hotfix.4, on Linux, locale en_US.UTF-8)
    • Flutter version 1.7.8+hotfix.4 at /home/estevez/sdk/flutter
    • Framework revision 20e59316b8 (8 weeks ago), 2019-07-18 20:04:33 -0700
    • Engine revision fee001c93f
    • Dart version 2.4.0

 
[✓] Android toolchain - develop for Android devices (Android SDK version 29.0.2)
    • Android SDK at /home/estevez/Android/Sdk
    • Android NDK location not configured (optional; useful for native profiling support)
    • Platform android-29, build-tools 29.0.2
    • Java binary at: /home/estevez/bin/android-studio/jre/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)
    • All Android licenses accepted.

[✓] Android Studio (version 3.5)
    • Android Studio at /home/estevez/bin/android-studio
    • Flutter plugin version 39.0.3
    • Dart plugin version 191.8423
    • Java version OpenJDK Runtime Environment (build 1.8.0_202-release-1483-b49-5587405)

[!] Connected device
    ! No devices available

! Doctor found issues in 1 category.
@charafau
Copy link
Collaborator

Thanks for the issue, I was also reading discussion from other plugin. Is it only that one plugin or for whole alarm manager ?

@estevez-dev
Copy link
Author

estevez-dev commented Sep 13, 2019

I reproduced this issue only with those two plugins. The only difference is that for workmanager you need to call registerPeriodicTask and for alarm manager you just need to initialize it.

@estevez-dev
Copy link
Author

Hi @charafau , there is some additional info available from workmanager plugin developer: fluttercommunity/flutter_workmanager#64 (comment)

@charafau
Copy link
Collaborator

thanks for investigation, not sure how to fix it yet, needs more fixing

@timrijckaert
Copy link

If we do not re-register your plugins inside the background Isolate then the stream in your main Isolate will still receive update from the flutter_webview.

However, as a user you want to be able to use your plugins also in the background.

@estevez-dev
Copy link
Author

@charafau may be it is possible to re-create event streams? For example with some publicly available method that will close the streams and create a new one? I now, I need to reattach listeners to the streams then, but this is not a problem. If it is possible lets test it in separate branch, I'm ready for testing ;-)

@charafau
Copy link
Collaborator

@estevez-dev probably yes.. but my time is very limited and I barely manage to do reviews and commits to project currently. If you create PR with fixes, that would be really helpful

@estevez-dev
Copy link
Author

@charafau I totally understand you! Will try to find some time to look at the code.

@Abarth91
Copy link

Any News?

@estevez-dev
Copy link
Author

No. I was forced to stop using webview plugin because of this issue. Moved to Chrome custom tabs.

@estevez-dev
Copy link
Author

Still would be good this to be fixed.

@oliverbytes
Copy link

I can confirm this plugin https://pub.dev/packages/flutter_downloader also conflicts with this... listeners are not fired

@estevez-dev
Copy link
Author

By the way I don't have this issue anymore since workmanager moved to Flutter embedding v2

@oliverbytes
Copy link

By the way I don't have this issue anymore since workmanager moved to Flutter embedding v2

But on iOS it's still present.

@sebalaursen
Copy link

sebalaursen commented Jan 13, 2021

Hi
Same thing happens after receiving sharing intents on iOS with receive_sharing_intent. After the app is opened by sharing it's possible to launch one url with events being emitted but after closing and trying to launch another url all events stop but the webview itself is loading normally.

@sebalaursen
Copy link

Also the same issue is happening with audio_service (package for background audio play) when playing an audio then launching a url, same, no events.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants