I have implemented local notification for checking battery status. If the battery level is drop by 1% then local notification is received.This works for both i.e. app is in foreground or background for iOS version below 9. When I update device OS to iOS 9,then I received local notification in foreground but unable to receive notification in background of the Application. Following is the code which are used to implement.
- (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];
}
In displayNotification
method we display notification.
I also enable background mode of the app i.e. shown in screenshot.
Any help would be appreciated. Thanks in advance.
You have enabled background fetch mode, which means that your app can receive remote notifications and perform network requests while in the background.
Without using questionable and App Store rejectable methods, it is not possible to do what you want. Apple specifically doesn't want you to be able to run your application in the background unless you have one of their approved use cases.