3

I am using Firebase appcheck in my android app. It works great, except that it consistently fails if I start the app in airplane mode then disable airplane mode (i.e., simulating opening the app without a network connection, then gaining a network connection).

When I don't have connectivity, I repeatedly get:

D/PersistentConnection: pc_0 - Trying to fetch auth token

D/PersistentConnection: pc_0 - Trying to fetch app check token

D/PersistentConnection: pc_0 - Successfully fetched token, opening connection

D/Connection: conn_2 - Opening a connection

D/WebSocket: ws_2 - WebSocket error.

D/PersistentConnection: pc_0 - Got on disconnect due to OTHER

When I regain connectivity, I get:

W/PersistentConnection: pc_1 - Firebase Database connection was forcefully killed by the server. Will not attempt reconnect. Reason: Invalid appcheck token

I am initializing app check in my MainActivity onCreate() method as follows. I have tried moving the following code into a network connectivity observer (as documented in https://firebase.google.com/docs/database/android/offline-capabilities#section-connection-state), but it did not work.

FirebaseApp.initializeApp(/*context=*/this)
val firebaseAppCheck = FirebaseAppCheck.getInstance()

firebaseAppCheck.installAppCheckProviderFactory(
    SafetyNetAppCheckProviderFactory.getInstance()
)
firebaseAppCheck.setTokenAutoRefreshEnabled(true)

My code for accessing the real-time database is as follows:

private val postListener : ValueEventListener = object : ValueEventListener {

        override fun onDataChange(dataSnapshot: DataSnapshot) {

            Timber.d("MetaData", "loadPost:onDataChange")

        }

        override fun onCancelled(databaseError: DatabaseError) {
            // Getting Post failed, log a message
            Timber.d("MetaData", "loadPost:onCancelled", databaseError.toException())
        }
    }

...

database.reference.child("databasename").addValueEventListener(postListener)

More broadly, is there a better way to be setting up the appcheck token in such a way that the app can recover from a bad token?

Unfortunately, this appcheck stuff is pretty much a black box to me. Any hints on how to approach this or debug it would be helpful.

0 Answers0