Send a Pause RPC on OnApplicationPause

I have a real-time multiplayer game for iOS. I’m trying to pause the game for all players when any player multitasks out of the app (OnApplicationPause).

I have a RequestPauseGame method that sends an RPC to the server, which sends one to all the players, which pauses their games. This all works.

protected void OnApplicationPause( bool paused )
{
	//pause game when applicaiton is paused (enters background)
	if (isGameRunning && paused)
	{
		RequestPauseGame();
	}
}

My problem is, after OnApplicationPause fires, the game thread is slept, so my RPC method doesn’t actually send. Then when I return to the app, the RPC goes through and the game is paused network-wide.

The only solution I can think of is to constantly send RPCs to the server from each client, so the server knows they’re still there. Then if I don’t hear back from a client after a specific timeout, I know they’re paused. If they stay paused for too long, my server automatically assumes they disconnected.

I want to avoid sending tons of RPCs though, as I’m trying to decrease network usage as is. Anyone have alternative suggestions on how to get around the thread sleeping of OnApplicationPause?

I haven’t figured out how to do this with the built-in Unity networking, but with Photon, you can add the following line after your RPC call:

PhotonNetwork.networkingPeer.SendOutgoingCommands();