diff --git a/CHANGELOG.md b/CHANGELOG.md index fdaf1ad0..b1b6fc49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,6 @@ +## [4.1.3](https://github.com/spoonconsulting/cordova-plugin-background-upload/compare/4.1.2...4.1.3) (2026-01-24) +* **android:** Prevent background upload from stopping when the app is terminated + ## [4.1.2](https://github.com/spoonconsulting/cordova-plugin-background-upload/compare/4.1.1...4.1.2) (2024-11-05) * **android:** Return upload start and end time in upload response * **iOS:** Return upload start and end time in upload response diff --git a/package-lock.json b/package-lock.json index 99d0eae6..17c1012f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@spoonconsulting/cordova-plugin-background-upload", - "version": "4.1.2", + "version": "4.1.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@spoonconsulting/cordova-plugin-background-upload", - "version": "4.1.2", + "version": "4.1.3", "license": "Apache-2.0", "devDependencies": { "cordova-paramedic": "git+https://github.com/apache/cordova-paramedic.git", diff --git a/package.json b/package.json index d02fac77..a52a7fee 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@spoonconsulting/cordova-plugin-background-upload", - "version": "4.1.2", + "version": "4.1.3", "description": "Cordova plugin for uploading file in the background", "cordova": { "id": "@spoonconsulting/cordova-plugin-background-upload", diff --git a/plugin.xml b/plugin.xml index 69290c1d..2424d374 100644 --- a/plugin.xml +++ b/plugin.xml @@ -1,5 +1,5 @@ - + Cordova Background Upload Plugin Background Upload plugin for Cordova ISC @@ -29,6 +29,8 @@ + + diff --git a/src/android/UploadNotification.java b/src/android/UploadNotification.java index 092ce707..c970a751 100644 --- a/src/android/UploadNotification.java +++ b/src/android/UploadNotification.java @@ -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; @@ -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) @@ -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) @@ -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()); + } } diff --git a/src/android/UploadTask.java b/src/android/UploadTask.java index 9fd1c5c0..ec57fe36 100644 --- a/src/android/UploadTask.java +++ b/src/android/UploadTask.java @@ -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; @@ -155,7 +156,7 @@ public UploadTask(@NonNull Context context, @NonNull WorkerParameters workerPara @NonNull @Override public Result doWork() { - if(!hasNetworkConnection()) { + if (!hasNetworkConnection()) { return Result.retry(); } @@ -395,12 +396,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)) {