当应用程序处于后台模式时,电池电量通知不起作用
Battery level notification not working when app is in background mode
我已经实现了用于检查电池状态的本地通知。如果电池电量下降 1%,则本地通知 received.This 对两者都有效,即应用程序在 iOS 版本低于 9 时处于前台或后台。
当我将设备 OS 更新为 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
方法中我们显示通知。
我还启用了应用程序的后台模式,如屏幕截图所示。
如有任何帮助,我们将不胜感激。提前致谢。
您已启用后台获取模式,这意味着您的应用可以在后台接收远程通知并执行网络请求。
不使用有问题的和App Store拒绝的方法,是做不到你想做的。 Apple 特别不希望您能够 运行 在后台运行您的应用程序,除非您有他们批准的用例之一。
我已经实现了用于检查电池状态的本地通知。如果电池电量下降 1%,则本地通知 received.This 对两者都有效,即应用程序在 iOS 版本低于 9 时处于前台或后台。 当我将设备 OS 更新为 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
方法中我们显示通知。
我还启用了应用程序的后台模式,如屏幕截图所示。
如有任何帮助,我们将不胜感激。提前致谢。
您已启用后台获取模式,这意味着您的应用可以在后台接收远程通知并执行网络请求。
不使用有问题的和App Store拒绝的方法,是做不到你想做的。 Apple 特别不希望您能够 运行 在后台运行您的应用程序,除非您有他们批准的用例之一。