Home > Blockchain >  unity realtime game search while app is in background
unity realtime game search while app is in background

Time:12-11

i have a unity mobile multiplayer game, using firebase for matchmaking, when the users press the play button it adds them as available for machmake

 mDatabaseRef.Child("matchMake").Child(auth.CurrentUser.UserId).Child("playerName").SetValueAsync(auth.CurrentUser.DisplayName);

and remove them when the user press the cancel game search button, but i also want to remove them when they close the app (even if they don't press the cancel game button) using

    private void OnApplicationQuit()
{
    removePlayer();
}

void OnApplicationPause(bool pause)
{
    removePlayer();
}
    public void removePlayer()
{
    mDatabaseRef.Child("matchMake").Child(auth.CurrentUser.UserId).RemoveValueAsync();
}

so what i want is to keep them in matchmake if they add the app in background (and thinking of informing them about finding game with push notifications) but remove them if they close the app from App Switcher (or even if the system quit it to free up resources). The problem is that while i can remove the "removeplayer" from OnApplicationPause that called when the app goes in background, it doesn't call the OnApplicationQuit if the app close from app switcher, this behaviour is kinda correct according to the documents

Warning: If the user suspends your application on a mobile platform, the operating system can quit the application to free up resources. In this case, depending on the operating system, Unity might be unable to call this method. On mobile platforms, it is best practice to not rely on this method to save the state of your application.

is there any way to do what i want ?
Thanks !!!

CodePudding user response:

Firebase Realtime Database has so-called onDisconnect handlers that allow you to send a write instruction to the server while you are online, that the server then executes once it detects that the client has disconnected.

Somehow onDisconnect is not documented for the Unity, so I recommend checking the Android documentation to learn how onDisconnect works, and then implementing it based on the reference documentation for onDisconnect in Unity.

  • Related