0

I'm building an Android game which requires the user to be online while playing. I'd like to detect if a game was interrupted while playing ( the internet connection was lost \ the device was turned off, etc...),and if it was - this should be considered as a loss for the user, and be written in the database. The only thing I have in mind for now is to save this loss offline, and update it when the user goes back online.


I know I can detect app connection status like this, but it doesn't help be because I can only execute offline operations after this. I also know I can listen to writing events in the database like this, but it doesn't help me, because no child in being updated after the game is interrupted.


My question is: Is there a way to write a Cloud Function which can listen to specific app connection status? If there is no such a way, what are the other options I have?

Tal Barda
  • 4,067
  • 10
  • 28
  • 53

1 Answers1

1

You can use a onDisconnect() handler. This is a piece of code you run while the app is connected, that sets an operation for the server to run when it detects that the app has disconnected.

A simple example of this from the documentation:

DatabaseRef presenceRef = FirebaseDatabase.getInstance().getReference("disconnectmessage");
// Write a string when this client loses connection
presenceRef.onDisconnect().setValue("I disconnected!");

But I recommend that you read the full documentation on managing presence for more examples.

With this you can have a Cloud Function trigger on the operation of the onDisconnect handler and then update the game status.

Note that it may take a few minutes before the server detects the lost connection in case of a non-clean disconnect.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807