Skip to content

[Windows] Flutter app doesn't launch automatically when an API hit is received while the app is closed/killed. #32

@nilkanthp-interpay

Description

@nilkanthp-interpay

Hi
I'm encountering an issue where the Flutter app doesn't launch automatically when an API hit is received while the app is closed.

Expected Behavior:
App Running: No action should be taken.
App Closed: The app should launch in response to the API hit.

Actual Behavior:
App Running: The app functions as expected.
App Closed: The app doesn't launch.

Steps to Reproduce:
Close the app completely.
Trigger the API hit.

Additional Information:
Platform: Windows
Plugin Versions: launch_at_startup: ^0.3.1

Code:

PackageInfo packageInfo = await PackageInfo.fromPlatform();
    launchAtStartup.setup(
      appName: packageInfo.appName,
      appPath: Platform.resolvedExecutable,
    );
    await launchAtStartup.enable();

startServer();
```

```
void startServer() async {
  final server = await HttpServer.bind(
    InternetAddress.loopbackIPv4,
    8080,
  );
  print('Listening on localhost:${server.port}');
  SdkLogger.debug('Autostart-TAG', 'Listening on localhost:${server.port}');
  await for (HttpRequest request in server) {
    SdkLogger.debug('Autostart-TAG', 'Inside Listening on localhost:${request.method} : ${request.uri.path}');
    if (request.method == 'POST' && request.uri.path == '/sale') {
      print('Received API call to launch app');
      SdkLogger.debug('Autostart-TAG', 'Received API call to launch app');
      checkAndLaunchApp();
    }
    request.response
      ..write('Request received')
      ..close();
  }
}
```

```
Future<void> checkAndLaunchApp() async {
  // Define the name of the process to check
  const processName = 'myapp.exe';
  // Replace with your app's process name
  // Get the list of currently running processes
  final result = await Shell().run('tasklist');
  final processes = result.outText;
  // Check if the process is running
  SdkLogger.debug('Autostart-TAG', 'CHECK PROCESS:: => ${processes.contains(processName)}');
  if (!processes.contains(processName)) {
    // If not running, launch the app
    await Process.run('myapp.exe', []);
    // Replace with the actual path to your app
    print('App launched successfully.');
    AppConstant().toast('App Launch Successfully');
    SdkLogger.debug('Autostart-TAG', 'App launch successfully');
  } else {
    print('App is already running.');
    AppConstant().toast('App is already running');
    SdkLogger.debug('Autostart-TAG', 'App is already running');
  }
}
```


Note: 
 => await Process.run('C:\Program Files (x86)\myapp\myapp.exe', []);  // also try this where app install with full path instead of myapp.exe
 => this .exe path is also add in environment variable 
 
    
    

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions