kopia lustrzana https://github.com/ryukoposting/Signal-Android
Improve resiliance of FCM fetch.
rodzic
a3802d0af0
commit
a1d444fc19
|
@ -40,8 +40,11 @@ object FcmFetchManager {
|
||||||
@Volatile
|
@Volatile
|
||||||
private var startedForeground = false
|
private var startedForeground = false
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return True if a service was successfully started, otherwise false.
|
||||||
|
*/
|
||||||
@JvmStatic
|
@JvmStatic
|
||||||
fun enqueue(context: Context, foreground: Boolean) {
|
fun enqueue(context: Context, foreground: Boolean): Boolean {
|
||||||
synchronized(this) {
|
synchronized(this) {
|
||||||
try {
|
try {
|
||||||
if (foreground) {
|
if (foreground) {
|
||||||
|
@ -63,8 +66,11 @@ object FcmFetchManager {
|
||||||
}
|
}
|
||||||
} catch (e: IllegalStateException) {
|
} catch (e: IllegalStateException) {
|
||||||
Log.w(TAG, "Failed to start service!", e)
|
Log.w(TAG, "Failed to start service!", e)
|
||||||
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun fetch(context: Context) {
|
private fun fetch(context: Context) {
|
||||||
|
|
|
@ -78,18 +78,25 @@ public class FcmReceiveService extends FirebaseMessagingService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void handleReceivedNotification(Context context, @Nullable RemoteMessage remoteMessage) {
|
private static void handleReceivedNotification(Context context, @Nullable RemoteMessage remoteMessage) {
|
||||||
|
boolean enqueueSuccessful;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
long timeSinceLastRefresh = System.currentTimeMillis() - SignalStore.misc().getLastFcmForegroundServiceTime();
|
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));
|
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) {
|
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());
|
SignalStore.misc().setLastFcmForegroundServiceTime(System.currentTimeMillis());
|
||||||
} else {
|
} else {
|
||||||
FcmFetchManager.enqueue(context, false);
|
enqueueSuccessful = FcmFetchManager.enqueue(context, false);
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} 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);
|
FcmFetchManager.retrieveMessages(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -56,8 +56,8 @@ public class FcmRefreshJob extends BaseJob {
|
||||||
this(new Job.Parameters.Builder()
|
this(new Job.Parameters.Builder()
|
||||||
.setQueue("FcmRefreshJob")
|
.setQueue("FcmRefreshJob")
|
||||||
.addConstraint(NetworkConstraint.KEY)
|
.addConstraint(NetworkConstraint.KEY)
|
||||||
.setMaxAttempts(1)
|
.setMaxAttempts(3)
|
||||||
.setLifespan(TimeUnit.MINUTES.toMillis(5))
|
.setLifespan(TimeUnit.HOURS.toMillis(6))
|
||||||
.setMaxInstancesForFactory(1)
|
.setMaxInstancesForFactory(1)
|
||||||
.build());
|
.build());
|
||||||
}
|
}
|
||||||
|
@ -109,7 +109,7 @@ public class FcmRefreshJob extends BaseJob {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onFailure() {
|
public void onFailure() {
|
||||||
Log.w(TAG, "GCM reregistration failed after retry attempt exhaustion!");
|
Log.w(TAG, "FCM reregistration failed after retry attempt exhaustion!");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Ładowanie…
Reference in New Issue