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

Crash on android when running in background #91

Open
brunolau opened this issue Dec 15, 2022 · 7 comments
Open

Crash on android when running in background #91

brunolau opened this issue Dec 15, 2022 · 7 comments

Comments

@brunolau
Copy link

Scenario:
The cordova app is set to KeepRunning=true and app is in background. one track stops playing, next one loads automatically, so we want to update the mediaControls (by calling the create method), we however receive following crash:

image

It's because the app attempts to start foreground service from background app, which is not allowed in newer versions of android (i guess 12+).

This is however not required if there is an already active music controls notification existing.

We temporarily tackled this by wrapping the setForeground call into try-catch and everything works fine, I however understand this would need a more generic approach. (e.g. some global on error callback or something like this)....or checking if a notification is active and if is, not attempting to restart....either way - cordova plugin should not crash the app.

Could this somehow be fixed or is it out of scope of your plugin usage?

@nettiteeri
Copy link

Facing the same issue

@CYL0728
Copy link

CYL0728 commented Feb 22, 2023

Facing the same issue also. anyone found solution on this issue?

@inviton
Copy link

inviton commented Feb 22, 2023

What we did is wrapping both methods in MusicControlsServiceConnection.java in try-catch as well as MusicControls.java => execute => create => getThreadpool => run

image

@f18nfz
Copy link

f18nfz commented Mar 3, 2023

I've noticed this issue too (as Android 12 onward doesn't allow starting services in the background). I noticed that if the user disables battery optimisation it seems to resolve the issue, however I think music apps are not allowed to call ACTION_IGNORE_BATTERY_OPTIMIZATION_SETTINGS and ask users to do this.
Not sure if the logic can be changed to use WorkManager or AlarmManager?

Thanks for the try-catch workaround suggestions :) 👍🏼

@Carlosps
Copy link

What we did is wrapping both methods in MusicControlsServiceConnection.java in try-catch as well as MusicControls.java => execute => create => getThreadpool => run

image

Would you please be able to create a pull request with this fix?

@brunolau
Copy link
Author

I don't really think so as it's not a proper fix. Proper fix would require an error callback so that application can react. empty try-catch is just a hack we used so that our app doesn't crash as we have guaranteed in our code, music controls are created only when app is in foreground

@brunolau
Copy link
Author

@ghenry22 Would some android specific method in plugin API, e.g. "setNotificationCreateErrorCallback" or something like that be considered an acceptable fix? I could create a PR with this if this approach is ok with you

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

6 participants