Ionic 4 - Android 9 - App crash on permission requests

So i have this app that i’m working on for a few months now, and i used to test it on my Samsung Galaxy S8. The problem i have presents itselft since i received the update to Android Pie (version 9, sdk 28). The app is written in Angular 7.

Basically whenever a pop-up that is not part of the ionic app needs to come on screen, the app crashes, without any log errors. The pop-ups can be for example the runtime request permission confirmation, or the login form autocomplete feature (native of android).

Opening the generated android project in android studio and debugging it turns out that the cause of the crash is the FOREGROUND_SERVICE permission not granted to the app. As stated from google release note beheaviouor changes page any app that targets Android 9 or higher and use foreground services must request the FOREGROUND_SERVICE permission.

Android studio debug logcat:

Adding the following rule to the config.xml: <preference name="android-targetSdkVersion" value="27" />, and running the app on the same device (android 9) results in the following error:

Even using the cordova-plugin-android-permissions to request at runtime the aformentioned permission ends having the same problem, the app crashes. (tough to test it i actually had to edit the permissions array in the ionic-native npm module, since it’s not even there, i guess beacuse it’s a normal level permission that shouldn’t need user action to be granted).

I also tested the app on a Xiaomi MI A2 Lite with android 9 on it, and the behaviour is the same. Testing it on another samsung phone, with android 8 still on it, it works as expected, no problems whatsoever.

I searched the web a lot for a solution to this, but i wasn’t able to find any valid resource.

I’m not sure what i should do now, i can’t get the app to work properly on Android 9 devices.

Thank you in advance for any help in this matter

@ABonino: Hi Did you get any solution for this. I am also facing the same issue… Kindly, help me on the same

Hello, yes I just recently found a solution. With the new Cordova Android 8.0.0 you can edit the android-manifes.xml adding proerties the to config.xml in the Inioc project.

I solved my problems adding the following statement to the ‘platform name=android’ tag:

tag:

   <platform name="android">
        <allow-intent href="market:*" />
        <edit-config file="AndroidManifest.xml" mode="merge" target="/manifest/uses-permission" xmlns:android="http://schemas.android.com/apk/res/android">
            <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
        </edit-config>
        // ....Other configs omitted
    </platform>

You need to declare that the app will need the FOREGROUND_PERMISSION, wich is a breaking change they added to the Android SDK 28.

I hope this will solve your poblem too.

1 Like

Hello, thanks for your reply. In my case, problem was related to back-ground plugin. I disabled back-ground mode as it was not that much needed for my application and everything started working smoothly.

1 Like

Thanks, Its work well :wink: