Battery state notifications, when app is in the background

Does anyone know how battery state notification (UIDevice.batteryStateDidChangeNotification) is supposed to work regarding app foreground/background state?

Assume there is no other reason why the app is running in the background. I have enabled UIDevice.current.isBatteryMonitoringEnabled when the app was in the foreground. What should happen if the external power is later connected or removed when the app is in the background? The docs don't mention this.

Possibilities include

  1. I don't get a notification, so I should check the state myself when the app next comes to the foreground.
  2. I'll get a notification when the app next comes to the foreground, if the state changed while it was in the background.
  3. The app will be woken up in the background to receive the notification.
  4. The app will be kept running in the background while isBatteryMonitoringEnabled is true.

It looks as if it's doing either 3 or 4, which I find a bit surprising. But is this influenced by the fact that it's connected (wirelessly) to the debugger?

Answered by Engineer in 813687022

Hi,

When you run your app from the debugger your app cannot be terminated since the debugger attached to it and keeps it alive.

Your app will not be woken up by battery notifications but you can use things like location updates, push notifications and background fetch to periodically get this information since these APIs will wake your app in the background (at least when the device battery is not low). Some APIs like APNs may not deliver if the battery is low, as an example.

Hopefully this helps.

Rico


WWDR | DTS | Software Engineer

Hi,

When you run your app from the debugger your app cannot be terminated since the debugger attached to it and keeps it alive.

Your app will not be woken up by battery notifications but you can use things like location updates, push notifications and background fetch to periodically get this information since these APIs will wake your app in the background (at least when the device battery is not low). Some APIs like APNs may not deliver if the battery is low, as an example.

Hopefully this helps.

Rico


WWDR | DTS | Software Engineer

Your app will not be woken up by battery notifications

Are you sure about that? It does not seem to be supported by my experiments.

batteryStateDidChangeNotification and such internal notifications (which are different than User Notifications) will not wake up (activate) an app which is in a suspended mode.

These notifications will only be delivered to the apps if it is already actively running (whether in the foreground or background) at the time the notification is fired.

Unless there is a reason for an app to be continuously running in the background (for example a navigation or music app) it is unlikely that the batteryStateDidChangeNotification will coincide with the few seconds of active time an app with some background execution time may have.

Debugging aside, while it is technically possible for a production app to receive the batteryStateDidChangeNotifications, the practicalities of iOS background app life cycle will make this work less than how you might be expecting it.


Argun Tekant /  DTS Engineer / Core Technologies

On further investigation it seems that:

  • If the device is not connected to the debugger, case (2) above applies i.e. when the app is resumed it gets a notification if the state is different than when it was suspended.

  • If the device is connected to the debugger (wirelessly), case 3 or 4 applies, i.e. I see the events in real-time in the debugger console.

The important thing is that I don't need to do anything special when the app resumes in order to know the current battery state.

Please let me know if you think this is wrong.

Battery state notifications, when app is in the background
 
 
Q