Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Aws Sdk 2.12.3 TransferService Usage

We were using version 2.4.2 of Amazon SDK for file upload and downloads. Recently we updated it to 2.12.3.

In older versions, any upload or download task used to start TransferService internally in the background which handled network connectivity monitoring among other things.

But to support Android 8 optimizations of limiting background services, in the latest version, the SDK has stopped calling TransferService internally and encourages the developer to call it explicitly. One recommended place to call the service being Application class's onCreate. TransferService has been reduced to merely being an initializer for another class TransferNetworkLossHandler which extends BroadcastReceiver which handles pause/resume of downloads in case of connectivity changes. One major difference in starting TransferService in older and newer SDK is that earlier it was started using startService method. But now Amazon docs recommend starting with "startForegroundService" with a Notification object on 8.0 device and above.

Our app uploads debugging logs every night in the background irrespective of whether the app is being used or not at that time. After the SDK update, we compulsorily have to set up a notification while TransferService is running on Oreo devices. We want to use Aws SDK to upload logs but do not want to show a notification as we feel it will give a bad experience to our users. Calling just startService without a notification object will work when the app is being started by the user. But when the app is not running or not in memory, our upload log task is triggered which calls Application class's onCreate which in turn calls startService which causes a crash as startService cannot be called when the app is in the background.

To avoid this, we tried to bypass the use of TransferService and initializing TransferNetworkLossHandler ourselves in Application class. This seems to work when the app is in use or in memory. But if we swipe out the app, log upload doesn't work when called in the background. Logs aren't helpful either. There are no error logs and we are getting proper observer ids but the files aren't being uploaded to the bucket.

We are using the following code to bypass TransferService and initialize TransferNetworkLossHandler in Application class's onCreate.

TransferNetworkLossHandler.getInstance(getApplicationContext());

and to initiate a dummy network check after initialization, we use

TransferNetworkLossHandler.getInstance(getApplicationContext()).onReceive(context, new Intent().setAction(ConnectivityManager.CONNECTIVITY_ACTION)); 

so that based on network connectivity, downloads can be paused or resumed by the SDK.

Is there any way we can use Amazon SDK to upload/download without using notification on 8.0 and above devices?.

Also, any links related to best practices for such use cases is highly appreciated.

like image 910
Raunak K Avatar asked Mar 14 '19 07:03

Raunak K


Video Answer


1 Answers

registerReceiver(TransferNetworkLossHandler.getInstance(getApplicationContext()), new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION));

I put this code in my Application class or in the activity I need to listen for network updates.

I had a problem in which when I was offline, my uploads would hang in WAITING_FOR_NETWORK state and never resume. Now it works fine.

Source: https://github.com/aws-amplify/aws-sdk-android/issues/899

like image 149
Rafael Silva Avatar answered Oct 06 '22 23:10

Rafael Silva