Skip to content

Commit 1a80ef7

Browse files
committed
Add ForegroundInfo support for upload notifications
1 parent 774e330 commit 1a80ef7

2 files changed

Lines changed: 29 additions & 4 deletions

File tree

src/android/UploadNotification.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
import androidx.annotation.IntegerRes;
1515
import androidx.annotation.RequiresApi;
1616
import androidx.core.app.NotificationCompat;
17+
import androidx.work.ForegroundInfo;
1718
import androidx.work.WorkInfo;
1819
import androidx.work.WorkManager;
20+
import android.content.pm.ServiceInfo;
1921

2022
import java.util.Collections;
2123
import java.util.List;
@@ -82,7 +84,7 @@ public static Notification createNotification(NotificationCompat.Builder notific
8284
Notification notification = notificationBuilder.build();
8385
notification.flags |= Notification.FLAG_NO_CLEAR;
8486
notification.flags |= Notification.FLAG_ONGOING_EVENT;
85-
return notification;
87+
return notification;
8688
}
8789

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

105107
// TODO: click intent open app
106-
@SuppressLint("ResourceType") NotificationCompat.Builder uploadNotificationBuilder = new NotificationCompat.Builder(context, UploadTask.NOTIFICATION_CHANNEL_ID)
108+
@SuppressLint("ResourceType")
109+
NotificationCompat.Builder uploadNotificationBuilder = new NotificationCompat.Builder(context,
110+
UploadTask.NOTIFICATION_CHANNEL_ID)
107111
.setContentTitle(notificationTitle)
108112
.setTicker(notificationTitle)
109113
.setSmallIcon(notificationIconRes)
@@ -117,4 +121,13 @@ private static NotificationCompat.Builder getUploadNotification(final Context co
117121

118122
return uploadNotificationBuilder;
119123
}
124+
125+
public ForegroundInfo getForegroundInfo() {
126+
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
127+
return new ForegroundInfo(notificationId, notificationBuilder.build(),
128+
ServiceInfo.FOREGROUND_SERVICE_TYPE_DATA_SYNC);
129+
} else {
130+
return new ForegroundInfo(notificationId, notificationBuilder.build());
131+
}
132+
}
120133
}

src/android/UploadTask.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.webkit.MimeTypeMap;
88

99
import androidx.annotation.NonNull;
10+
import androidx.work.ForegroundInfo;
1011
import androidx.work.Data;
1112
import androidx.work.Worker;
1213
import androidx.work.WorkerParameters;
@@ -155,7 +156,7 @@ public UploadTask(@NonNull Context context, @NonNull WorkerParameters workerPara
155156
@NonNull
156157
@Override
157158
public Result doWork() {
158-
if(!hasNetworkConnection()) {
159+
if (!hasNetworkConnection()) {
159160
return Result.retry();
160161
}
161162

@@ -395,12 +396,23 @@ private void handleNotification() {
395396
Log.d(TAG, "Upload Notification");
396397
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
397398
setForegroundAsync(uploadForegroundNotification.getForegroundInfo(getApplicationContext()));
398-
} else {
399+
} else {
399400
uploadNotification.updateProgress();
400401
}
401402
Log.d(TAG, "Upload Notification Exit");
402403
}
403404

405+
@NonNull
406+
@Override
407+
public ForegroundInfo getForegroundInfo() {
408+
Log.d(TAG, "getForegroundInfo: Promoting to foreground service");
409+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.S) {
410+
return uploadForegroundNotification.getForegroundInfo(getApplicationContext());
411+
} else {
412+
return uploadNotification.getForegroundInfo();
413+
}
414+
}
415+
404416
private synchronized boolean hasNetworkConnection() {
405417
ConnectivityManager connectivityManager = (ConnectivityManager) getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
406418
if((connectivityManager == null) || (connectivityManager.getActiveNetworkInfo() == null) || (connectivityManager.getActiveNetworkInfo().isConnectedOrConnecting() == false)) {

0 commit comments

Comments
 (0)