aws-amplify / aws-sdk-android

AWS SDK for Android. For more information, see our web site:
https://docs.amplify.aws
Other
1.03k stars 549 forks source link

[Android 14+] InvalidForegroundServiceTypeException #3617

Closed shivprakash86 closed 3 weeks ago

shivprakash86 commented 1 month ago

Describe the bug After upgrading our project to targetSdkVersion version from 33 to 34 we are getting crash InvalidForegroundServiceTypeException: Starting FGS with type none callerApp=ProcessRecord{a64075a 8552:com.xx.xx/u0a196} targetSDK=34 has been prohibited.

android.app.InvalidForegroundServiceTypeException: Starting FGS with type none callerApp=ProcessRecord{a64075a 8552:com.xx.xx/u0a196} targetSDK=34 has been prohibited
                                                                                                        at android.app.InvalidForegroundServiceTypeException$1.createFromParcel(InvalidForegroundServiceTypeException.java:53)
                                                                                                        at android.app.InvalidForegroundServiceTypeException$1.createFromParcel(InvalidForegroundServiceTypeException.java:49)
                                                                                                        at android.os.Parcel.readParcelableInternal(Parcel.java:5075)
                                                                                                        at android.os.Parcel.readParcelable(Parcel.java:5057)
                                                                                                        at android.os.Parcel.createExceptionOrNull(Parcel.java:3237)
                                                                                                        at android.os.Parcel.createException(Parcel.java:3226)
                                                                                                        at android.os.Parcel.readException(Parcel.java:3209)
                                                                                                        at android.os.Parcel.readException(Parcel.java:3151)
                                                                                                        at android.app.IActivityManager$Stub$Proxy.setServiceForeground(IActivityManager.java:7164)
                                                                                                        at android.app.Service.startForeground(Service.java:863)
                                                                                                        at androidx.work.impl.foreground.SystemForegroundService$Api31Impl.startForeground(SystemForegroundService.java:194)
                                                                                                        at androidx.work.impl.foreground.SystemForegroundService$1.run(SystemForegroundService.java:130)
                                                                                                        at android.os.Handler.handleCallback(Handler.java:959)
                                                                                                        at android.os.Handler.dispatchMessage(Handler.java:100)
                                                                                                        at android.os.Looper.loopOnce(Looper.java:232)
                                                                                                        at android.os.Looper.loop(Looper.java:317)
                                                                                                        at android.app.ActivityThread.main(ActivityThread.java:8674)
                                                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                                                        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:580)
                                                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:886)
has context menu

To Reproduce A code sample or steps:

**Manifest file code**
<service
            android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
            android:enabled="true" />
**Java code**
context.startForegroundService(transferService);

Which AWS service(s) are affected? AWS s3 media upload service

Environment Information (please complete the following information):

lawmicha commented 1 month ago

Hi @shivprakash86, thanks for opening this issue, we'll investigate and provide an update when we can.

mattcreaser commented 1 month ago

I believe the TransferService needs to pass the FOREGROUND_SERVICE_TYPE_DATA_SYNC flag to startForeground to be compliant with new API 34 requirements. We also need to add the FOREGROUND_SERVICE_DATA_SYNC permission to the manifest file.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    startForeground(ongoingNotificationId, userProvidedNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
    startForeground(ongoingNotificationId, userProvidedNotification);
}
shivprakash86 commented 1 month ago

I believe the TransferService needs to pass the FOREGROUND_SERVICE_TYPE_DATA_SYNC flag to startForeground to be compliant with new API 34 requirements. We also need to add the FOREGROUND_SERVICE_DATA_SYNC permission to the manifest file.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    startForeground(ongoingNotificationId, userProvidedNotification, ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
} else {
    startForeground(ongoingNotificationId, userProvidedNotification);
}

I think this change should be in your s3 SDK TransferService class onStartCommand method

mattcreaser commented 1 month ago

👍 That's exactly right @shivprakash86. We'll update this very soon for a release next week.

shivprakash86 commented 1 month ago

@mattcreaser Thanks we are waiting.

mattcreaser commented 1 month ago

The fix has been merged and we will prioritize getting it released soon. Please note the additional changes you will need to make to your AndroidManifest.xml file noted in the PR description:

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

    <application>
        <service
            android:name="com.amazonaws.mobileconnectors.s3.transferutility.TransferService"
            android:foregroundServiceType="dataSync" />

    </application>
shivprakash86 commented 1 month ago

@mattcreaser Yes .

shivprakash86 commented 3 weeks ago

@mattcreaser is this release above issue?

mattcreaser commented 3 weeks ago

Hey @shivprakash86 sorry that no one updated this issue, but this fix was released in version 2.76.1.