Skip to content

Commit

Permalink
Improve resiliance of FCM fetch.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Jul 5, 2022
1 parent 236e3f7 commit dda28f8
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
Expand Up @@ -40,8 +40,11 @@ object FcmFetchManager {
@Volatile
private var startedForeground = false

/**
* @return True if a service was successfully started, otherwise false.
*/
@JvmStatic
fun enqueue(context: Context, foreground: Boolean) {
fun enqueue(context: Context, foreground: Boolean): Boolean {
synchronized(this) {
try {
if (foreground) {
Expand All @@ -63,8 +66,11 @@ object FcmFetchManager {
}
} catch (e: IllegalStateException) {
Log.w(TAG, "Failed to start service!", e)
return false
}
}

return true
}

private fun fetch(context: Context) {
Expand Down
Expand Up @@ -78,18 +78,25 @@ public void onSendError(@NonNull String s, @NonNull Exception e) {
}

private static void handleReceivedNotification(Context context, @Nullable RemoteMessage remoteMessage) {
boolean enqueueSuccessful;

try {
long timeSinceLastRefresh = System.currentTimeMillis() - SignalStore.misc().getLastFcmForegroundServiceTime();
Log.d(TAG, String.format(Locale.US, "[handleReceivedNotification] API: %s, FeatureFlag: %s, RemoteMessagePriority: %s, TimeSinceLastRefresh: %s ms", Build.VERSION.SDK_INT, FeatureFlags.useFcmForegroundService(), remoteMessage != null ? remoteMessage.getPriority() : "n/a", timeSinceLastRefresh));

if (FeatureFlags.useFcmForegroundService() && Build.VERSION.SDK_INT >= 31 && remoteMessage != null && remoteMessage.getPriority() == RemoteMessage.PRIORITY_HIGH && timeSinceLastRefresh > FCM_FOREGROUND_INTERVAL) {
FcmFetchManager.enqueue(context, true);
enqueueSuccessful = FcmFetchManager.enqueue(context, true);
SignalStore.misc().setLastFcmForegroundServiceTime(System.currentTimeMillis());
} else {
FcmFetchManager.enqueue(context, false);
enqueueSuccessful = FcmFetchManager.enqueue(context, false);
}
} catch (Exception e) {
Log.w(TAG, "Failed to start service. Falling back to legacy approach.", e);
Log.w(TAG, "Failed to start service.", e);
enqueueSuccessful = false;
}

if (!enqueueSuccessful) {
Log.w(TAG, "Failed to start service. Falling back to legacy approach.");
FcmFetchManager.retrieveMessages(context);
}
}
Expand Down
Expand Up @@ -56,8 +56,8 @@ public FcmRefreshJob() {
this(new Job.Parameters.Builder()
.setQueue("FcmRefreshJob")
.addConstraint(NetworkConstraint.KEY)
.setMaxAttempts(1)
.setLifespan(TimeUnit.MINUTES.toMillis(5))
.setMaxAttempts(3)
.setLifespan(TimeUnit.HOURS.toMillis(6))
.setMaxInstancesForFactory(1)
.build());
}
Expand Down Expand Up @@ -109,7 +109,7 @@ public void onRun() throws Exception {

@Override
public void onFailure() {
Log.w(TAG, "GCM reregistration failed after retry attempt exhaustion!");
Log.w(TAG, "FCM reregistration failed after retry attempt exhaustion!");
}

@Override
Expand Down

0 comments on commit dda28f8

Please sign in to comment.