I'm developing a battery utility app. My app is supposed to fire a local notification when a switch is turned on to notify user when battery is fully charged. However, i've a problem of getting a local notification when the phone is locked and during inactive state. Wondering whether the battery monitoring is running in the background when the phone is locked? Or is there any error in my code execution? I need help seriously. Can someone please help :(
Here's my codes
AppDelegate.m:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions{
// Override point for customization after application launch.
//Handle launching from a Notification
UILocalNotification *localNotification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
if(localNotification)
{
//Set icon badge number to 0
application.applicationIconBadgeNumber = 0;
}
return YES;}
-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification{
UIApplicationState state = [application applicationState];
if(state == UIApplicationStateActive)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Fully Charged"
message:notification.alertBody
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
}
//Set icon badge number to 0
application.applicationIconBadgeNumber = 0;}
-(void)ViewDidLoad{
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(batteryLevelChanged:) name:UIDeviceBatteryLevelDidChangeNotification object:nil];}
-(void)batteryLevelChanged:(NSNotification *)notification{
[self updateBatteryLevel];
if ([self.notifyFullyChargedSwitch isOn])
{
[self notifyFullyCharged];
}
}
-(void)updateBatteryLevel{
float batterylevel = [UIDevice currentDevice].batteryLevel;
static NSNumberFormatter *numberFormatter = nil;
if(numberFormatter == nil)
{
numberFormatter = [[NSNumberFormatter alloc] init];
[numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
[numberFormatter setMaximumFractionDigits:1];
}
NSNumber *levelObj = [NSNumber numberWithFloat:batterylevel];
}
-(void)notifyFullyCharged{
[UIDevice currentDevice].batteryMonitoringEnabled = YES;
float batterylevel = [UIDevice currentDevice].batteryLevel;
if(batterylevel == 1.0){
// Schedule the notification
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = nil;
localNotification.alertBody = @"Unplug your device. Your battery has been fully charged.";
localNotification.alertAction = @"Slide to view";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
localNotification.repeatInterval = NSMinuteCalendarUnit;
localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}}