Repository: flutter
Issue: iOS: Method invocations from native side get lost if sent early, instead of being buffered
Labels: platform-ios,engine,a: platform-views,has reproducible steps,P2,found in release: 2.8,found in release: 2.9,team-ios,triaged-ios

## Steps to Reproduce

1. flutter create bug
2. copy first code sample to `bug/lib/main.dart`
3. copy second code sample to `bug/ios/Runner/AppDelegate.swift`
4. open project in XCode and set IOS Deployment Target to 10.0
5. flutter run

**Expected results:**

The native-to-flutter call should be buffered and carried out as soon as the Flutter side is ready. The log should contain "Flutter side received method call: qux".

**Actual results:**

The method is not called. The inverse flutter-to-native call works as expected and the corresponding log message appears in the log.

The [documentation](https://api.flutter.dev/objcdoc/Classes/FlutterMethodChannel.html#/c:objc(cs)FlutterMethodChannel(im)resizeChannelBuffer:) for `FlutterMethodChannel.resizeBuffer` states:

> Adjusts the number of messages that will get buffered when sending messages to channels that aren’t fully set up yet. 
> For example, the engine isn’t running yet or the channel’s message handler isn’t set up on the Dart side yet.

<details>
<summary>Code sample</summary>

<!--
      Please create a minimal reproducible sample that shows the problem
      and attach it below between the lines with the backticks.

      To create it you can use `flutter create bug` command and update the `main.dart` file.

      Alternatively, you can use https://dartpad.dev/
      which is capable of creating and running small Flutter apps.

      Without this we will unlikely be able to progress on the issue, and because of that
      we regretfully will have to close it.
-->


bug/lib/main.dart

```dart
import 'dart:developer' as developer;

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatefulWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  static const _methodChannel = MethodChannel('com.example/foo');

  MyAppState() {
    _methodChannel.setMethodCallHandler((call) async {
      developer.log('Flutter side received method call: ${call.method}');
    });

    _methodChannel.invokeMethod('bar');
  }

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(home: Scaffold());
  }
}
```

bug/ios/Runner/AppDelegate.swift

```swift
import UIKit
import Flutter
import os

@UIApplicationMain
@objc class AppDelegate: FlutterAppDelegate {
    
    override func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        GeneratedPluginRegistrant.register(with: self)
        
        let controller = self.window?.rootViewController as! FlutterViewController
        let methodChannel = FlutterMethodChannel(name: "com.example/foo", binaryMessenger: controller.binaryMessenger)
        
        methodChannel.resizeBuffer(256)
        methodChannel.setMethodCallHandler() { call, result in
            os_log("Native side received method call: %s", call.method)
            result(nil)
        }
        
        methodChannel.invokeMethod("qux", arguments: nil)
        
        return super.application(application, didFinishLaunchingWithOptions: launchOptions)
    }
}
```

</details>

<details>
  <summary>Logs</summary>

```
[ +209 ms] com.example.bug: 19613
[        ] Waiting for observatory port to be available...
[ +699 ms] Observatory URL on device: http://127.0.0.1:58364/0q0pgsZkxEY=/
[  +14 ms] Caching compiled dill
[ +110 ms] Connecting to service protocol: http://127.0.0.1:58364/0q0pgsZkxEY=/
[ +201 ms] Native side received method call: bar
[  +63 ms] Launching a Dart Developer Service (DDS) instance at http://127.0.0.1:0, connecting to VM service at http://127.0.0.1:58364/0q0pgsZkxEY=/.
[ +106 ms] DDS is listening at http://127.0.0.1:58367/yFaUD6jPm2k=/.
[  +76 ms] Successfully connected to service protocol: http://127.0.0.1:58364/0q0pgsZkxEY=/
[  +34 ms] DevFS: Creating new filesystem on the device (null)
[  +23 ms] DevFS: Created new filesystem on the device (file:///Users/user/Library/Developer/CoreSimulator/Devices/E5F8DBC3-FA2A-460B-B0C0-D8FC99E8E34B/data/Containers/Data/Application/FC663638-5E91-4239-BC5D-B8E1EF27FF5A/tmp/bugUWqHtw/bug/)
[   +2 ms] Updating assets
[  +88 ms] Syncing files to device iPhone 11...
[   +1 ms] <- reset
[        ] Compiling dart to kernel with 0 updated files
[   +2 ms] <- recompile package:bug/main.dart e22d5742-126e-4e24-b89b-d53bce342ebd
[        ] <- e22d5742-126e-4e24-b89b-d53bce342ebd
[  +88 ms] Updating files.
[        ] DevFS: Sync finished
[        ] Syncing files to device iPhone 11... (completed in 94ms)
[        ] Synced 0.0MB.
[   +2 ms] <- accept
[   +3 ms] Connected to _flutterView/0x7fce31829e20.
[   +3 ms] Flutter run key commands.
[   +1 ms] r Hot reload. 🔥🔥🔥
[        ] R Hot restart.
[        ] h List all available interactive commands.
[        ] d Detach (terminate "flutter run" but leave application running).
[        ] c Clear the screen
[        ] q Quit (terminate the application on the device).
[        ] 💪 Running with sound null safety 💪
[   +1 ms] An Observatory debugger and profiler on iPhone 11 is available at: http://127.0.0.1:58367/yFaUD6jPm2k=/
[ +212 ms] The Flutter DevTools debugger and profiler on iPhone 11 is available at: http://127.0.0.1:9100?uri=http://127.0.0.1:58367/yFaUD6jPm2k=/
```

```
Analyzing bug...
No issues found! (ran in 2.3s)
```

```
[✓] Flutter (Channel stable, 2.8.0, on macOS 12.0.1 21A559 darwin-x64, locale en-DE)
    • Flutter version 2.8.0 at /usr/local/Caskroom/flutter/2.8.0/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision cf44000065 (13 days ago), 2021-12-08 14:06:50 -0800
    • Engine revision 40a99c5951
    • Dart version 2.15.0

[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.2)
    • Android SDK at /Users/user/Library/Android/sdk
    • Platform android-30, build-tools 30.0.2
    • ANDROID_HOME = /Users/user/Library/Android
    • ANDROID_SDK_ROOT = /Users/user/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.10.2

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2020.3)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.10+0-b96-7281165)

[✓] VS Code (version 1.59.1)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension can be installed from:
      🔨 https://marketplace.visualstudio.com/items?itemName=Dart-Code.flutter

[✓] Connected device (3 available)
    • iPhone 11 (mobile) • E5F8DBC3-FA2A-460B-B0C0-D8FC99E8E34B • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-2 (simulator)
    • macOS (desktop)    • macos                                • darwin-x64     • macOS 12.0.1 21A559 darwin-x64
    • Chrome (web)       • chrome                               • web-javascript • Google Chrome 96.0.4664.110

• No issues found!
```

</details>