@@ -269,6 +269,7 @@ private enum ConnectionState {
269269 private static final String SERVER_DATA_TAG = "t" ;
270270 private static final String SERVER_DATA_WARNINGS = "w" ;
271271 private static final String SERVER_RESPONSE_DATA = "d" ;
272+ private static final String INVALID_APP_CHECK_TOKEN = "Invalid appcheck token" ;
272273
273274 /** Delay after which a established connection is considered successful */
274275 private static final long SUCCESSFUL_CONNECTION_ESTABLISHED_DELAY = 30 * 1000 ;
@@ -559,12 +560,21 @@ public void onDisconnect(Connection.DisconnectReason reason) {
559560
560561 @ Override
561562 public void onKill (String reason ) {
562- logger .warn (
563- "Firebase Database connection was forcefully killed by the server. Will not attempt"
564- + " reconnect. Reason: "
565- + reason );
563+ if (reason .equals (INVALID_APP_CHECK_TOKEN )
564+ && invalidAppCheckTokenCount < INVALID_TOKEN_THRESHOLD ) {
565+ invalidAppCheckTokenCount ++;
566+ logger .warn (
567+ "Detected invalid AppCheck token. Reconnecting ("
568+ + (INVALID_TOKEN_THRESHOLD - invalidAppCheckTokenCount )
569+ + " attempts remaining)" );
570+ } else {
571+ logger .warn (
572+ "Firebase Database connection was forcefully killed by the server. Will not attempt"
573+ + " reconnect. Reason: "
574+ + reason );
566575
567- interrupt (SERVER_KILL_INTERRUPT_REASON );
576+ interrupt (SERVER_KILL_INTERRUPT_REASON );
577+ }
568578 }
569579
570580 @ Override
@@ -1120,31 +1130,17 @@ private void sendAppCheckTokenHelper(final boolean restoreStateAfterComplete) {
11201130 String status = (String ) response .get (REQUEST_STATUS );
11211131 if (status .equals ("ok" )) {
11221132 invalidAppCheckTokenCount = 0 ;
1123- if (restoreStateAfterComplete ) {
1124- restoreState ();
1125- }
11261133 } else {
11271134 appCheckToken = null ;
11281135 forceAppCheckTokenRefresh = true ;
11291136 String reason = (String ) response .get (SERVER_RESPONSE_DATA );
11301137 logger .debug ("App check failed: " + status + " (" + reason + ")" );
1131-
11321138 // Note: We don't close the connection as the developer may not have
11331139 // enforcement enabled. The backend closes connections with enforcements.
1134- if (status .equals ("invalid_token" ) || status .equals ("permission_denied" )) {
1135- // We'll wait a couple times before logging the warning / increasing the
1136- // retry period since app check tokens will report as "invalid" if they're
1137- // just expired. Plus there may be transient issues that resolve themselves.
1138- invalidAppCheckTokenCount ++;
1139- if (invalidAppCheckTokenCount >= INVALID_TOKEN_THRESHOLD ) {
1140- // Set a long reconnect delay because recovery is unlikely.
1141- retryHelper .setMaxDelay ();
1142- logger .warn (
1143- "Provided app check credentials are invalid. This "
1144- + "usually indicates your FirebaseAppCheck was not initialized "
1145- + "correctly." );
1146- }
1147- }
1140+ }
1141+
1142+ if (restoreStateAfterComplete ) {
1143+ restoreState ();
11481144 }
11491145 };
11501146
0 commit comments