Skip to content

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

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
nilkanthp-interpay opened this issue Nov 20, 2024 · 0 comments

Comments

@nilkanthp-interpay
Copy link

nilkanthp-interpay commented Nov 20, 2024

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 
 
    
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant