From ba3ea172e524db6553d2011de2d384429798005e Mon Sep 17 00:00:00 2001 From: "B. Petersen" Date: Sat, 21 Sep 2024 23:39:34 +0200 Subject: [PATCH] use explicit FetchForegroundService this avoids potential issues with GenericForegroundService which eg. may block app start. --- .../notifications/FcmReceiveService.java | 23 ++----- src/main/AndroidManifest.xml | 4 ++ .../securesms/connect/DcEventCenter.java | 4 +- .../notifications/NotificationCenter.java | 1 + .../service/FetchForegroundService.java | 61 +++++++++++++++++++ .../service/GenericForegroundService.java | 2 +- 6 files changed, 73 insertions(+), 22 deletions(-) create mode 100644 src/main/java/org/thoughtcrime/securesms/service/FetchForegroundService.java diff --git a/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java b/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java index 999b762f7..ebaaf604c 100644 --- a/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java +++ b/src/gplay/java/org/thoughtcrime/securesms/notifications/FcmReceiveService.java @@ -17,20 +17,15 @@ import com.google.firebase.messaging.RemoteMessage; import org.thoughtcrime.securesms.ApplicationContext; import org.thoughtcrime.securesms.BuildConfig; -import org.thoughtcrime.securesms.R; -import org.thoughtcrime.securesms.service.GenericForegroundService; -import org.thoughtcrime.securesms.service.NotificationController; +import org.thoughtcrime.securesms.service.FetchForegroundService; import org.thoughtcrime.securesms.util.Util; public class FcmReceiveService extends FirebaseMessagingService { private static final String TAG = FcmReceiveService.class.getSimpleName(); private static final Object INIT_LOCK = new Object(); - private static final Object NOTIFICATION_CONTROLLER_LOCK = new Object(); - private static boolean initialized; private static volatile boolean triedRegistering; private static volatile String prefixedToken; - private static NotificationController notificationController; public static void register(Context context) { if (Build.VERSION.SDK_INT < 19) { @@ -101,23 +96,13 @@ public class FcmReceiveService extends FirebaseMessagingService { @Override public void onMessageReceived(@NonNull RemoteMessage remoteMessage) { Log.i(TAG, "FCM push notification received"); - synchronized (NOTIFICATION_CONTROLLER_LOCK) { - notificationController = GenericForegroundService.startForegroundTask(this, getString(R.string.connectivity_updating)); - if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { // we should complete within 20 seconds - notificationController.close(); - notificationController = null; - } + FetchForegroundService.start(this); + if (!ApplicationContext.dcAccounts.backgroundFetch(19)) { // we should complete within 20 seconds + FetchForegroundService.stop(this); } Log.i(TAG, "background fetch done"); } - public static void backgroundFetchDone() { - synchronized (NOTIFICATION_CONTROLLER_LOCK) { - notificationController.close(); - notificationController = null; - } - } - @Override public void onDeletedMessages() { Log.i(TAG, "FCM push notifications dropped"); diff --git a/src/main/AndroidManifest.xml b/src/main/AndroidManifest.xml index ec201402c..486980fcd 100644 --- a/src/main/AndroidManifest.xml +++ b/src/main/AndroidManifest.xml @@ -380,6 +380,10 @@ android:name=".service.GenericForegroundService" android:foregroundServiceType="dataSync" /> + + = Build.VERSION_CODES.O) { CHANNEL_CREATED.set(true); NotificationChannel channel = new NotificationChannel(NotificationCenter.CH_GENERIC,