From 9fc4bfb97020e1a4cb1f7fa8f3ae601957a072ca Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sun, 22 Sep 2024 12:37:01 +0200 Subject: [PATCH] move backgroundFetch() to FetchForegroundService --- .../notifications/FcmReceiveService.java | 8 -------- .../service/FetchForegroundService.java | 18 ++++++++++++++++++ 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java b/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java index 85953bbf4..8aecf54a8 100644 --- a/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java +++ b/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java @@ -97,14 +97,6 @@ public class FcmReceiveService extends FirebaseMessagingService { public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { Log.i(TAG, "FCM push notification received"); FetchForegroundService.start(this); - - // we should complete within 20 seconds, - // see https://firebase.google.com/docs/cloud-messaging/android/receive - if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { - FetchForegroundService.stop(this); - } - - Log.i(TAG, "background fetch done"); } @Override diff --git a/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java b/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java index ca10d3277..ddb97310b 100644 --- a/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java +++ b/src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java @@ -5,15 +5,20 @@ import android.app.Service; import android.content.Context; import android.content.Intent; import android.os.IBinder; +import android.util.Log; import androidx.annotation.Nullable; import androidx.core.app.NotificationCompat; import androidx.core.content.ContextCompat; +import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.R; +import org.thoughtcrime.securesms.notifications.FcmReceiveService; import org.thoughtcrime.securesms.notifications.NotificationCenter; +import org.thoughtcrime.securesms.util.Util; public final class FetchForegroundService extends Service { + private static final String TAG = FcmReceiveService.class.getSimpleName(); private static final Object SERVICE_LOCK = new Object(); private static Intent service; @@ -38,6 +43,7 @@ public final class FetchForegroundService extends Service { @Override public void onCreate() { + Log.i(TAG, "Creating fetch service"); super.onCreate(); Notification notification = new NotificationCompat.Builder(this, NotificationCenter.CH_GENERIC) @@ -46,6 +52,18 @@ public final class FetchForegroundService extends Service { .build(); startForeground(NotificationCenter.ID_FETCH, notification); + + // Start explicit fetch only after we marked ourselves as requiring foreground; + // this may help we on getting network and time adequately + // Fetch is started in background to not block the UI. + // We then run not longer than the max. of 20 seconds, + // see https://firebase.google.com/docs/cloud-messaging/android/receive . + Util.runOnAnyBackgroundThread(() -> { + Log.i(TAG, "Starting fetch"); + if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { + FetchForegroundService.stop(this); + } + }); } @Override