Skip to content

Commit

Permalink
Handle devices disallowing start of FcmFetchService.
Browse files Browse the repository at this point in the history
Some devices are overzealous with battery management and disallow
starting services even when they're in response to a high-priority FCM
message (which should be allowed). So in these situations, we just
fall back to what we were doing before.
  • Loading branch information
greyson-signal committed Jun 1, 2020
1 parent 989b071 commit c4ec0c9
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
@@ -1,10 +1,12 @@
package org.thoughtcrime.securesms.gcm;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.IBinder;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.firebase.messaging.RemoteMessage;
Expand Down Expand Up @@ -70,25 +72,29 @@ public void onDestroy() {
}

private void fetch() {
retrieveMessages(this);

if (activeCount.decrementAndGet() == 0) {
Log.e(TAG, "stopping");
stopSelf();
}
}

static void retrieveMessages(@NonNull Context context) {
BackgroundMessageRetriever retriever = ApplicationDependencies.getBackgroundMessageRetriever();
boolean success = retriever.retrieveMessages(this, new RestStrategy(), new RestStrategy());
boolean success = retriever.retrieveMessages(context, new RestStrategy(), new RestStrategy());

if (success) {
Log.i(TAG, "Successfully retrieved messages.");
} else {
if (Build.VERSION.SDK_INT >= 26) {
Log.w(TAG, "Failed to retrieve messages. Scheduling on the system JobScheduler (API " + Build.VERSION.SDK_INT + ").");
FcmJobService.schedule(this);
FcmJobService.schedule(context);
} else {
Log.w(TAG, "Failed to retrieve messages. Scheduling on JobManager (API " + Build.VERSION.SDK_INT + ").");
ApplicationDependencies.getJobManager().add(new PushNotificationReceiveJob(this));
ApplicationDependencies.getJobManager().add(new PushNotificationReceiveJob(context));
}
}

if (activeCount.decrementAndGet() == 0) {
Log.e(TAG, "stopping");
stopSelf();
}
}
}

Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.Intent;
import android.os.Build;

import androidx.annotation.NonNull;

Expand All @@ -10,7 +11,9 @@

import org.thoughtcrime.securesms.dependencies.ApplicationDependencies;
import org.thoughtcrime.securesms.jobs.FcmRefreshJob;
import org.thoughtcrime.securesms.jobs.PushNotificationReceiveJob;
import org.thoughtcrime.securesms.logging.Log;
import org.thoughtcrime.securesms.messages.RestStrategy;
import org.thoughtcrime.securesms.registration.PushChallengeRequest;
import org.thoughtcrime.securesms.util.TextSecurePreferences;

Expand Down Expand Up @@ -43,7 +46,12 @@ public void onNewToken(String token) {
}

private static void handleReceivedNotification(Context context) {
context.startService(new Intent(context, FcmFetchService.class));
try {
context.startService(new Intent(context, FcmFetchService.class));
} catch (Exception e) {
Log.w(TAG, "Failed to start service. Falling back to legacy approach.");
FcmFetchService.retrieveMessages(context);
}
}

private static void handlePushChallenge(@NonNull String challenge) {
Expand Down

0 comments on commit c4ec0c9

Please sign in to comment.