Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>
<uses-permission android:name="android.permission.FOREGROUND_SERVICE" />
<uses-permission android:name="android.permission.FOREGROUND_SERVICE_DATA_SYNC" />
</config-file>
<config-file target="AndroidManifest.xml" parent="/manifest/application">
<meta-data android:name="DATABASE" android:value="cordova-plugin-background-upload.db" />
Expand Down
17 changes: 15 additions & 2 deletions src/android/UploadNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@
import androidx.annotation.IntegerRes;
import androidx.annotation.RequiresApi;
import androidx.core.app.NotificationCompat;
import androidx.work.ForegroundInfo;
import androidx.work.WorkInfo;
import androidx.work.WorkManager;
import android.content.pm.ServiceInfo;

import java.util.Collections;
import java.util.List;
Expand Down Expand Up @@ -82,7 +84,7 @@ public static Notification createNotification(NotificationCompat.Builder notific
Notification notification = notificationBuilder.build();
notification.flags |= Notification.FLAG_NO_CLEAR;
notification.flags |= Notification.FLAG_ONGOING_EVENT;
return notification;
return notification;
}

@RequiresApi(api = Build.VERSION_CODES.O)
Expand All @@ -103,7 +105,9 @@ private static NotificationCompat.Builder getUploadNotification(final Context co
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, notificationIntent, pendingIntentFlag);

// TODO: click intent open app
@SuppressLint("ResourceType") NotificationCompat.Builder uploadNotificationBuilder = new NotificationCompat.Builder(context, UploadTask.NOTIFICATION_CHANNEL_ID)
@SuppressLint("ResourceType")
NotificationCompat.Builder uploadNotificationBuilder = new NotificationCompat.Builder(context,
UploadTask.NOTIFICATION_CHANNEL_ID)
.setContentTitle(notificationTitle)
.setTicker(notificationTitle)
.setSmallIcon(notificationIconRes)
Expand All @@ -117,4 +121,13 @@ private static NotificationCompat.Builder getUploadNotification(final Context co

return uploadNotificationBuilder;
}

public ForegroundInfo getForegroundInfo() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
return new ForegroundInfo(notificationId, notificationBuilder.build(),
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
}

return new ForegroundInfo(notificationId, notificationBuilder.build());
}
}
33 changes: 27 additions & 6 deletions src/android/UploadTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import android.webkit.MimeTypeMap;

import androidx.annotation.NonNull;
import androidx.work.ForegroundInfo;
import androidx.work.Data;
import androidx.work.Worker;
import androidx.work.WorkerParameters;
Expand Down Expand Up @@ -155,7 +156,7 @@ public UploadTask(@NonNull Context context, @NonNull WorkerParameters workerPara
@NonNull
@Override
public Result doWork() {
if(!hasNetworkConnection()) {
if (!hasNetworkConnection()) {
return Result.retry();
}

Expand Down Expand Up @@ -194,9 +195,16 @@ public Result doWork() {
}

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

// Start call
currentCall = httpClient.newCall(request);
Expand Down Expand Up @@ -305,7 +313,10 @@ private void handleProgress(long bytesWritten, long totalBytes) {
}

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

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

Expand Down Expand Up @@ -395,12 +406,22 @@ private void handleNotification() {
Log.d(TAG, "Upload Notification");
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
setForegroundAsync(uploadForegroundNotification.getForegroundInfo(getApplicationContext()));
} else {
} else {
uploadNotification.updateProgress();
}
Log.d(TAG, "Upload Notification Exit");
}

@NonNull
@Override
public ForegroundInfo getForegroundInfo() {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
return uploadForegroundNotification.getForegroundInfo(getApplicationContext());
}

return uploadNotification.getForegroundInfo();
}

private synchronized boolean hasNetworkConnection() {
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
if((connectivityManager == null) || (connectivityManager.getActiveNetworkInfo() == null) || (connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting() == false)) {
Expand Down
Loading