使用UILocalNotification如何在电池充满电时收到通知?

时间:2014-03-16 14:00:11

标签: ios objective-c uilocalnotification

我正在开发一款电池实用程序。我的应用程序应该在开关打开时发出本地通知,以在电池充满电时通知用户。但是,当手机被锁定和处于非活动状态时,我遇到了获取本地通知的问题。想知道手机锁定时电池监控是否在后台运行?或者我的代码执行有任何错误吗?我需要认真帮助。有人可以帮忙:(

这是我的代码

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];
}}

0 个答案:

没有答案