kopia lustrzana https://github.com/ryukoposting/Signal-Android
Improve resiliance of FCM fetch.
rodzic
a3802d0af0
commit
a1d444fc19
|
@ -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) {
|
||||
|
@ -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) {
|
||||
|
|
|
@ -78,18 +78,25 @@ public class FcmReceiveService extends FirebaseMessagingService {
|
|||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -56,8 +56,8 @@ public class FcmRefreshJob extends BaseJob {
|
|||
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());
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ public class FcmRefreshJob extends BaseJob {
|
|||
|
||||
@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
|
||||
|
|
Ładowanie…
Reference in New Issue