react-native-background-actions icon indicating copy to clipboard operation
react-native-background-actions copied to clipboard

My app is Getting Crashed once i start my background service please tell me whats the issue

Open Yashi1919 opened this issue 1 year ago • 11 comments

this is my android manifest.xml please resolve this issue i have to upload this in oplaystore please

<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />

<!-- Foreground Service Permissions -->
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS" tools:targetApi="m" />

<!-- Location Permissions -->
<!-- ACCESS_FINE_LOCATION allows fine (GPS) location access -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<!-- ACCESS_BACKGROUND_LOCATION should be requested only if your app truly requires background location access -->
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION"
    tools:targetApi="r" />

<!-- Storage Permissions - Use Scoped Storage instead of broad permissions -->
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" tools:remove="android:maxSdkVersion" />
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" tools:targetApi="30"/>

<application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/wallo"
    android:roundIcon="@mipmap/wallo"
    android:allowBackup="false"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

    <!-- Main Activity -->
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

        <intent-filter android:label="filter_react_native">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="wallodynamo" />
        </intent-filter>
    </activity>

    <!-- Foreground Service Declaration -->
    <service
        android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
        android:exported="false"
        android:foregroundServiceType="location|mediaPlayback|dataSync"
        android:permission="android.permission.BIND_JOB_SERVICE"
        tools:replace="android:foregroundServiceType">
    </service>


</application>

Yashi1919 avatar Sep 24 '24 00:09 Yashi1919

You need foreground service permissions for every foreground service type, that you use.

For instance I use dataSync and Microphone

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />

mikkeldanielsen avatar Sep 25 '24 11:09 mikkeldanielsen

<!-- Existing permissions -->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>                                                                                                                  
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<!-- Add permissions for specific foreground service types -->
<!-- Example for Camera Service -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Add other specific permissions as needed -->

<application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/wallo"
    android:roundIcon="@mipmap/wallo"
    android:allowBackup="false"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter android:label="filter_react_native">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="wallodynamo" />
        </intent-filter>
    </activity>

    <!-- Service Declaration -->
    <service
        android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
        android:exported="false"
        android:foregroundServiceType="location|mediaPlayback|dataSync"
        android:permission="android.permission.BIND_JOB_SERVICE"
        tools:replace="android:foregroundServiceType">
    </service>

</application>
still getting crashed may i am not asking permissions can anyone help me with the permissions that i have to ask from user to run my foreground service

Yashi1919 avatar Sep 26 '24 00:09 Yashi1919

I am also getting same issue.

chandanpradhanin avatar Sep 26 '24 11:09 chandanpradhanin

<!-- Existing permissions -->
<uses-permission android:name="android.permission.REQUEST_IGNORE_BATTERY_OPTIMIZATIONS"/>
<uses-permission android:name="android.permission.VIBRATE"/>
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_CONNECTED_DEVICE"/>                                                                                                                  
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_LOCATION" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_MEDIA_PLAYBACK" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
<!-- Add permissions for specific foreground service types -->
<!-- Example for Camera Service -->
<uses-permission android:name="android.permission.CAMERA" />
<!-- Add other specific permissions as needed -->

<application
    android:name=".MainApplication"
    android:label="@string/app_name"
    android:icon="@mipmap/wallo"
    android:roundIcon="@mipmap/wallo"
    android:allowBackup="false"
    android:theme="@style/AppTheme"
    android:supportsRtl="true">

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|screenSize|smallestScreenSize|uiMode"
        android:launchMode="singleTask"
        android:windowSoftInputMode="adjustResize"
        android:exported="true">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <intent-filter android:label="filter_react_native">
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.BROWSABLE" />
            <data android:scheme="wallodynamo" />
        </intent-filter>
    </activity>

    <!-- Service Declaration -->
    <service
        android:name="com.asterinet.react.bgactions.RNBackgroundActionsTask"
        android:exported="false"
        android:foregroundServiceType="location|mediaPlayback|dataSync"
        android:permission="android.permission.BIND_JOB_SERVICE"
        tools:replace="android:foregroundServiceType">
    </service>

</application>

still getting crashed may i am not asking permissions can anyone help me with the permissions that i have to ask from user to run my foreground service

If you use Android Studio, you can see what happens when your app crashes. In the crash/error log you should see what permission you need to request.

mikkeldanielsen avatar Sep 26 '24 11:09 mikkeldanielsen

The crash issue I encountered was resolved by following these steps:

Step 1: Reinstall the Latest Version Ensure you are using the latest version of the lib, which is 4.0.1.

Step 2: Add Foreground Service Permissions Include the following permissions in your AndroidManifest.xml file to allow foreground services:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

Step 3: Add Service Declaration in Manifest In the section of your AndroidManifest.xml, add the following line to declare the foreground service. If this line already exists, ensure it is exactly as shown below:

<application>
        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync" />
    </application>

Final Manifest Example: After making the above changes, your manifest file should resemble this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <application>
        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync" />
    </application>
</manifest>

These steps resolved the crash issue for me. I hope this helps others facing similar problems.

chandanpradhanin avatar Sep 27 '24 17:09 chandanpradhanin

While the app is in the background, the microphone isn't working. However, it functions properly when the app is in the foreground, where both camera and microphone permissions are granted.

how can i solve this @chandanpradhanin @mikkeldanielsen @Yashi1919 ?

vishalyad16 avatar Oct 01 '24 12:10 vishalyad16

You need foreground service permissions for every foreground service type, that you use.

For instance I use dataSync and Microphone

    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_MICROPHONE" />

How can I handle the background issue since the microphone isn’t working when the app is in the background?

vishalyad16 avatar Oct 01 '24 12:10 vishalyad16

Not able to release app playstore blocking the release requesting for a video with foreground service, please help with solution! BTW why do we need FOREGROUND_SERVICE_DATA_SYNC?? @mikkeldanielsen @Yashi1919 @chandanpradhanin

vishalyad16 avatar Oct 23 '24 05:10 vishalyad16

I am getting the same issue after migrating app to React Native 0.75.3 @mikkeldanielsen @Yashi1919 @chandanpradhanin

ravindraguptacapgemini avatar Oct 23 '24 14:10 ravindraguptacapgemini

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions"> <uses-permission android:name="android.permission.FOREGROUND_SERVICE" /> <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" /> <application> <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync"/> </application> </manifest>

While the app is in the background, the microphone isn't working. However, it functions properly when the app is in the foreground, where both camera and microphone permissions are granted.

how can i solve this ?

Hi @chandanpradhanin, @mikkeldanielsen, and @Yashi1919,

I’m facing an issue where the microphone stops working when the app is in the background, even though the camera and microphone permissions are correctly granted when the app is in the background.

Can you please assist in resolving this? It would be greatly appreciated if someone could help or provide any guidance on how to fix this behavior.

Thanks in advance!

vishalyad16 avatar Jan 27 '25 06:01 vishalyad16

The crash issue I encountered was resolved by following these steps:

Step 1: Reinstall the Latest Version Ensure you are using the latest version of the lib, which is 4.0.1.

Step 2: Add Foreground Service Permissions Include the following permissions in your AndroidManifest.xml file to allow foreground services:

<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />

Step 3: Add Service Declaration in Manifest In the section of your AndroidManifest.xml, add the following line to declare the foreground service. If this line already exists, ensure it is exactly as shown below:

<application>
        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync" />
    </application>

Final Manifest Example: After making the above changes, your manifest file should resemble this:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.asterinet.react.bgactions">
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
    <uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
    <application>
        <service android:name=".RNBackgroundActionsTask" android:foregroundServiceType="dataSync" />
    </application>
</manifest>

These steps resolved the crash issue for me. I hope this helps others facing similar problems.

i have already done these steps, still the app is crashing after some time?

vincon09 avatar Mar 25 '25 13:03 vincon09