Skip to content

onMessageFromUnity not working in Android - 1.3.0-beta.1 #47

Open
@Oppenheimr

Description

@Oppenheimr

Is there an existing issue for this?

  • I have searched the existing issues

Unity version

Unity 6000.0 LTS

Common issues

  • I have checked the common issues

Flutter version

3.32.1

Description

Hi guys, we are using this plugin with following dependencies;

  flutter_embed_unity: ^1.3.0-beta.1
  flutter_embed_unity_6000_0_android: ^1.2.1-beta.1

Our flutter code;

EmbedUnity(
              onMessageFromUnity: (String message) {
                print(message);
                try {
                  final decoded = json.decode(message);
                  if (decoded is Map<String, dynamic> &&
                      decoded.containsKey('game') &&
                      decoded.containsKey('reward') &&
                      decoded.containsKey('score')) {
                    final String game = decoded['game'];
                    final int reward = decoded['reward'];
                    final int score = decoded['score'];

                    addRewardToUser(game, reward);
                    sendScore(game, score);
                  }
                } catch (e) {
                  print("Invalid JSON or unexpected format: $e");
                }
              },
            ),

Our C# code;

    public static class UnityFlutterCommunication
    {
        /// <summary>
        /// Sends game statistics to Flutter, waits briefly, then quits the application.
        /// </summary>
        /// <param name="data">GameStatistic object to be sent</param>
        public static async void BackToFlutter(GameStatistic data)
        {
            if (data == null)
            {
                Debug.LogWarning("GameStatistic data is null. Cannot send to Flutter.");
                return;
            }

            try
            {
                var json = data.ToJson();
                Debug.Log($"BackToFlutter, sending data: {json}");
                SendToFlutter.Send(json);
                await Task.Delay(300); // Ensure message is sent before quitting
                //Application.Quit();
            }
            catch (Exception e)
            {
                Debug.LogError($"[BackToFlutter] Exception occurred: {e}");
                // You can trigger a fallback or log this externally
            }
        }
    }

    [Serializable]
    public class GameStatistic
    {
        public string game;
        public int reward;
        public int score;

        public GameStatistic(string game, int reward, int score)
        {
            this.game = game;
            this.reward = reward;
            this.score = score;
        }

        /// <summary>
        /// Converts the GameStatistic object to JSON string.
        /// </summary>
        public string ToJson()
        {
            return JsonUtility.ToJson(this);
        }
    }

im calling;

            try
            {
                var json = data.ToJson();
                Debug.Log($"BackToFlutter, sending data: {json}");
                SendToFlutter.Send(json);
                await Task.Delay(300); // Ensure message is sent before quitting
                //Application.Quit();
            }
            catch (Exception e)
            {
                Debug.LogError($"[BackToFlutter] Exception occurred: {e}");
                // You can trigger a fallback or log this externally
            }

This implementation is working perfectly on ios we tested it with @aykutuludag but it is not working on android.

Minimum reproducible example (MRE)

No response

What platforms are you seeing the problem on?

Android

Devices

Xiaomi Redmi note 13 - not worked. onMessageFromUnity not called
Iphone 14 - works perfectly

Anything else?

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingduplicateThis issue or pull request already exists

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions