Я внедрил локальное уведомление для проверки состояния батареи. Если уровень заряда батареи падает на 1%, тогда принимается местное уведомление. Это работает как для приложения, так и для приложения на переднем плане или для версии для iOS ниже 9. Когда я обновляю ОС устройства до iOS 9, я получил локальное уведомление на переднем плане, но не может получать уведомление в фоновом режиме приложения. Ниже приведен код, который используется для реализации.Уведомление об уровне заряда аккумулятора не работает, когда приложение находится в фоновом режиме
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Enable monitoring of battery status
**[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];**
// Request to be notified when battery charge or state changes
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBatteryStatus) name:UIDeviceBatteryLevelDidChangeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(checkBatteryStatus) name:UIDeviceBatteryStateDidChangeNotification object:nil];
}
- (void)applicationDidEnterBackground:(UIApplication *)application {
// Request to be notified when battery charge or state changes
[[UIDevice currentDevice] setBatteryMonitoringEnabled:YES];
**[[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceBatteryLevelDidChangeNotification object:nil userInfo:nil];**
**[[NSNotificationCenter defaultCenter] postNotificationName:UIDeviceBatteryStateDidChangeNotification object:nil userInfo:nil];**
}
- (void)checkBatteryStatus
{
notifyAlarm = [[UILocalNotification alloc] init];
notifyAlarm.alertBody = @“battery alert";
notifyAlarm.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notifyAlarm];
[self displayNotification];
}
displayNotification
способ мы отображаем уведомление.
Я также включаю фоновый режим приложения, то есть показано на снимке экрана.
Любая помощь будет оценена. Заранее спасибо.