Skip to content

Fix messages from Unity on Android when using multiple Flutter engines. #46

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

timbotimbo
Copy link
Collaborator

Bug Description

Fixes #35 and #13

Adding certain plugins to your project will result in Unity messages not being received on Android.
This is because these plugins launch additional Flutter Engines for background functionality, which triggers onAttachedToEngine multiple times.
This causes Unity to send messages to a method channel attached to the wrong Flutter engine.

Some of the many plugins that appear to be capable of doing this:

  • android_alarm_manager_plus
  • firebase_messaging
  • flutter_pushed_messaging
  • flutter_local_notifications
  • audio_service

This happens for both FlutterActivity and FlutterFragmentActivity.

Flutter has an open issue that this is expected behaviour and needs documentation in the plugin docs.

Reproduction steps

  1. Download and run the example using Unity 2022.3.
    Tapping on the Flutter logo increments the number in the text "has been touched x times".
    The onAttachedToEngine log occurs once in the debug console.
  2. Add the following plugin to pubspec.yaml
    android_alarm_manager_plus: ^4.0.7
  3. Modify main.dart to initialize this plugin.
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await AndroidAlarmManager.initialize(); // comment or uncomment this line to disable or enable the bug.
  runApp(const ExampleApp());
}
  1. Now the plugin runs, but tapping the Flutter logo does NOT increment the number.
    The debug console contains 3 logs of onAttachedToEngine.

You can only test this with Unity 6000 if you apply #45 first , otherwise touch input is simply broken.

Fix

Only setup the method channel for Unity when there is an Activity attached to the Flutter engine.
The additional Flutter engines used in these plugins do not have an Activity.

The method channel is now created in onAttachedToActivity.

The changes to the file in the 2022_3 and 6000_0 android plugins are identical.

Alternative

I posted an alternative quick fix here. Which creates a method channel for all Flutter engines and sends the message from Unity to all engines.

The fix in this PR leads to fewer redundant messages being passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

The plugin stops recieveing messages from unity if a plugin "flutter_pushed_messaging" is added to the dependency.
1 participant