From 0facdc0497be48e952ff3c006baa78f1507b5151 Mon Sep 17 00:00:00 2001 From: Greyson Parrelli Date: Tue, 15 Aug 2023 09:00:57 -0400 Subject: [PATCH] Fix foreground service in PushNotificationReceiveJob. --- .../jobs/PushNotificationReceiveJob.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java index b7efcac3d6..61c9168008 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java +++ b/app/src/main/java/org/thoughtcrime/securesms/jobs/PushNotificationReceiveJob.java @@ -1,5 +1,7 @@ package org.thoughtcrime.securesms.jobs; +import android.os.Build; + import androidx.annotation.NonNull; import androidx.annotation.Nullable; @@ -8,8 +10,10 @@ import org.thoughtcrime.securesms.R; import org.thoughtcrime.securesms.jobmanager.Job; import org.thoughtcrime.securesms.jobmanager.impl.NetworkConstraint; import org.thoughtcrime.securesms.messages.WebSocketDrainer; +import org.thoughtcrime.securesms.notifications.NotificationChannels; import org.thoughtcrime.securesms.service.DelayedNotificationController; import org.thoughtcrime.securesms.service.GenericForegroundService; +import org.thoughtcrime.securesms.service.NotificationController; import org.whispersystems.signalservice.api.push.exceptions.PushNetworkException; import java.io.IOException; @@ -47,8 +51,19 @@ public final class PushNotificationReceiveJob extends BaseJob { public void onRun() throws IOException { boolean success; - try (DelayedNotificationController unused = GenericForegroundService.startForegroundTaskDelayed(context, context.getString(R.string.BackgroundMessageRetriever_checking_for_messages), 300, R.drawable.ic_signal_refresh)) { - success = WebSocketDrainer.blockUntilDrainedAndProcessed(); + if (Build.VERSION.SDK_INT < 31) { + try (DelayedNotificationController unused = GenericForegroundService.startForegroundTaskDelayed(context, context.getString(R.string.BackgroundMessageRetriever_checking_for_messages), 300, R.drawable.ic_signal_refresh)) { + success = WebSocketDrainer.blockUntilDrainedAndProcessed(); + } + } else { + try { + try (NotificationController unused = GenericForegroundService.startForegroundTask(context, context.getString(R.string.BackgroundMessageRetriever_checking_for_messages), NotificationChannels.getInstance().OTHER, R.drawable.ic_signal_refresh)) { + success = WebSocketDrainer.blockUntilDrainedAndProcessed(); + } + } catch (UnableToStartException e) { + Log.w(TAG, "Failed to start foreground service. Running in the background."); + success = WebSocketDrainer.blockUntilDrainedAndProcessed(); + } } if (success) {