- Home /
Unity mobile "Low Battery" notification problem (IOS)
Hi,
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()?
Answer by JVene · Aug 28, 2018 at 11:30 AM
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.
Okay, thank you! Would be cool if unity added a state for "local notifications" like calls, low battery messages etc.
In a sense, those are available, but Unity isn't where you get that. When you want something native, you go native. There is a tendency for students to think only Unity commands and messages are available, because it is a Unity application, but in reality you're writing a C# application that is driven by and consumes the Unity framework. You can make any call you like, though it is much more difficult to read native messages, you are able to write native plugins for each platform (it is an advanced concept)
Your answer

Follow this Question
Related Questions
How to get current battery life on mobile device 3 Answers
Disable/enable local notifications on Android and iOS 0 Answers
Unity 3.5 allows terrains on iPad2 and newer iOS devices, what's the max terrain size? 0 Answers
Game view performance VS actual build performance 2 Answers
Maximum RAM memory exceeded causes app to crash in iOS 1 Answer