currently i’m working an on app for IOS and Android. Its an online game that connects to the server all 3 to 5 sec. If the user closes the app completely (so OnApplicationExit() gets called) the user gets kicked out of the game room.If the user softcloses the app (so OnApplicationPause() gets called) the same result should happen, and i works.
But last time testing it on my IPhone i got the “Low Battery” notification what calls the OnApplicationPause() func. too and kicks the player what shouldnt happen.
How can i solve the problem with the notification? Is there a better alternative than OnApplicationPause()?
OnApplicationPause is filled with exceptions to the rule (read the docs on it). Frankly, it’s a bit of a mess. The behavior is platform dependent, which is a warning to me that the message isn’t reliable (if it behaves differently by platform, it could very well behave differently in different or future versions of one platform - experience suggests it).
I don’t find documentation on a battery low message, but you might experiment by adding an OnApplicationFocus to see if there’s a pattern you can recognize. I doubt it, but maybe iOS sends some focus transition you might recognize.
Otherwise, inside OnApplicationPause, read the SystemInfo.batterylevel. You have to make a judgment call here. The level Unity returns is from 0 to 1, which is to say that 0.10 is 10% battery, 0.15 is 15% battery. You have to select a level that makes sense. If at or below some threshold, assume this is a battery low system message, and skip that code which you want to avoid.
As a side note, it is unfortunate, and sometimes you can’t avoid it, but when you can you should avoid platform specific behavior even if that means you have leave the play “not” kicked out on all pauses, just so the app behaves well at battery low pause. This seems to me one of those occasions where you have to prioritize overall behavior over that you’d prefer by design, leaving what is most logical. Since the pause behavior is given to you by the device/platform, you can’t really stop that. It seems intercepting and interpreting accurately why pause is called does not work in all occasions, so I’d say kicking the player out on pause should not be dependent on why pause is called, since it is going to be unreliable. Checking battery status might help, but you can’t be sure if the status happens to be coincident with a player pause.