Skip to content

Commit

Permalink
move backgroundFetch() to FetchForegroundService
Browse files Browse the repository at this point in the history
  • Loading branch information
r10s committed Sep 22, 2024
1 parent 4dc5324 commit dff2d98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,6 @@ public static String getToken() {
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@
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;

Expand All @@ -38,6 +43,7 @@ public static void stop(Context context) {

@Override
public void onCreate() {
Log.i(TAG, "Creating fetch service");
super.onCreate();

Notification notification = new NotificationCompat.Builder(this, NotificationCenter.CH_GENERIC)
Expand All @@ -46,6 +52,18 @@ public void onCreate() {
.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
Expand Down

0 comments on commit dff2d98

Please sign in to comment.