Search code examples
iosswiftnotificationsbattery

iOS Notification push with battery level


I'm a student developing an iOS app, and I want to display a simple banner notification on the device, when a specific battery level is reached.

I found some interesting things (http://www.appcoda.com/local-notifications-ios8/) with local notifications, but I don't get a part of the implementation...

When doing the notification setup, we have to write this :

localNotification.fireDate = datePicker.date

But I guess that there is an other way than a precise date and time to set an activation event for the notification, and so I want to do it by checking the battery level.

When some people write this :

NSNotificationCenter.defaultCenter().addObserver(...

I can't find a complete code with the next part of the execution, because I guess there is a part where we add buttons for the pop-up.

Thanks for your help !


Solution

  • To make it short: You can't.
    Monitoring battery usage is possible, but as soon as your app is suspended by iOS (because the user locked the phone or put your app in background), you won't be able to execute any code.
    You can use Background Modes to prevent iOS from suspending your app, but if you don't have a feature that requires those background modes, your app will be rejected by Apple.

    About local notifications: the only way to trigger them is with a date. You can't hook them up to some event.