Skip to content
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

Initialize fails on first call #76

Open
breuerss opened this issue Jul 6, 2023 · 1 comment
Open

Initialize fails on first call #76

breuerss opened this issue Jul 6, 2023 · 1 comment
Assignees
Labels
bug Something isn't working

Comments

@breuerss
Copy link

breuerss commented Jul 6, 2023

Describe the bug
The result of

bool initialized = await FlutterBackground.initialize(androidConfig: androidConfig);

is always false, since

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?): Boolean {
    try {
        if (alreadyCalled || requestCode != PermissionHandler.PERMISSION_CODE_IGNORE_BATTERY_OPTIMIZATIONS) {
            return false
        }

        alreadyCalled = true

        // THIS IS ALWAYS FALSE
        onSuccess(resultCode == Activity.RESULT_OK)
    } catch (ex: Exception) {
        onError("flutter_background.PermissionHandler", "Error while waiting for user to disable battery optimizations", ex.localizedMessage)
    }

    return true
}

resultCode always contains 0 no matter what the answer.

Even ignoring the return value of initialize in Flutter would not work, since the plugin does not continue to initialize itself.

To Reproduce
On the first run with following code:

await FlutterBackground.initialize(androidConfig: androidConfig);

if (await FlutterBackground.hasPermissions) {
  await FlutterBackground.enableBackgroundExecution();
}

Confirm the allow permission request.

The application throws an exception and the background execution is not enabled.
The second walk through works, since the permission is then already granted.

Expected behavior

Since the same code works on Android 11 I guess the solution is to find a way to get a proper result code from the permission request. I don't know how to achieve this.

As an alternative for me also works to ignore the result code and always return true.

Smartphone:
Observed with:

  • Device: Motorola neo 30
  • OS: Android 12

Seems to work properly on

  • Device: ACEPAD A145
  • OS: Android 11

Thanks for your nice plugin.

@breuerss breuerss added the bug Something isn't working label Jul 6, 2023
@JulianAssmann
Copy link
Owner

Thanks for creating the issue. Can you please upgrade to the new version of the plugin (1.3.0), update your AndroidManifest.xml according to the new README (specifically including the foreground service types) and report back?

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

No branches or pull requests

2 participants