Skip to content

Commit cc7bc88

Browse files
test background upload
1 parent d5a86cb commit cc7bc88

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

plugin.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
3636
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
3737
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
38+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
39+
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
3840
</config-file>
3941
<config-file target="AndroidManifest.xml" parent="/manifest/application">
4042
<meta-data android:name="DATABASE" android:value="cordova-plugin-background-upload.db" />

src/android/UploadTask.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,16 @@ public Result doWork() {
195195
}
196196

197197
startTime = System.currentTimeMillis();
198-
// Register me
199-
uploadForegroundNotification.progress(getId(), 0f);
200-
handleNotification();
198+
// Register me and start foreground service IMMEDIATELY
199+
// This must be done before any blocking operations to ensure uploads continue when app is killed
200+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
201+
uploadForegroundNotification.progress(getId(), 0f);
202+
setForegroundAsync(uploadForegroundNotification.getForegroundInfo(getApplicationContext()));
203+
} else {
204+
// Android 12+: Still need to start foreground service for WorkManager to continue after app kill
205+
uploadNotification.updateProgress();
206+
setForegroundAsync(uploadNotification.getForegroundInfo());
207+
}
201208

202209
// Start call
203210
currentCall = httpClient.newCall(request);
@@ -306,7 +313,10 @@ private void handleProgress(long bytesWritten, long totalBytes) {
306313
}
307314

308315
float percent = (float) bytesWritten / (float) totalBytes;
309-
UploadForegroundNotification.progress(getId(), percent);
316+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
317+
UploadForegroundNotification.progress(getId(), percent);
318+
}
319+
// On Android 12+, progress is tracked via WorkManager progress data
310320

311321
Log.i(TAG, "handleProgress: " + getId() + " Progress: " + (int) (percent * 100f));
312322

0 commit comments

Comments
 (0)