@@ -269,6 +269,7 @@ private enum ConnectionState {
269
269
private static final String SERVER_DATA_TAG = "t" ;
270
270
private static final String SERVER_DATA_WARNINGS = "w" ;
271
271
private static final String SERVER_RESPONSE_DATA = "d" ;
272
+ private static final String INVALID_APP_CHECK_TOKEN = "Invalid appcheck token" ;
272
273
273
274
/** Delay after which a established connection is considered successful */
274
275
private static final long SUCCESSFUL_CONNECTION_ESTABLISHED_DELAY = 30 * 1000 ;
@@ -559,12 +560,21 @@ public void onDisconnect(Connection.DisconnectReason reason) {
559
560
560
561
@ Override
561
562
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 );
566
575
567
- interrupt (SERVER_KILL_INTERRUPT_REASON );
576
+ interrupt (SERVER_KILL_INTERRUPT_REASON );
577
+ }
568
578
}
569
579
570
580
@ Override
@@ -1120,31 +1130,17 @@ private void sendAppCheckTokenHelper(final boolean restoreStateAfterComplete) {
1120
1130
String status = (String ) response .get (REQUEST_STATUS );
1121
1131
if (status .equals ("ok" )) {
1122
1132
invalidAppCheckTokenCount = 0 ;
1123
- if (restoreStateAfterComplete ) {
1124
- restoreState ();
1125
- }
1126
1133
} else {
1127
1134
appCheckToken = null ;
1128
1135
forceAppCheckTokenRefresh = true ;
1129
1136
String reason = (String ) response .get (SERVER_RESPONSE_DATA );
1130
1137
logger .debug ("App check failed: " + status + " (" + reason + ")" );
1131
-
1132
1138
// Note: We don't close the connection as the developer may not have
1133
1139
// 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 ();
1148
1144
}
1149
1145
};
1150
1146
0 commit comments