后台 UILocalNotification - iOS 9
UILocalNotification in background - iOS 9
我有一个导航应用程序在获取位置更新时使用 UILocalNotification
。
当应用程序在 iOS 9 台设备(锁定屏幕/按下主屏幕按钮)上进入后台时 - 它停止接收通知。适用于 iOS 8 台设备。
当应用程序在前台而不是在后台时(当然,这要重要得多),我会收到通知。
代码看起来像这样:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *currLocation = [locations lastObject];
CLLocationDistance dist = [currLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude]];
BOOL gotToDestination = dist < 60;
if (gotToDestination) {
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.alertBody = [NSString stringWithFormat:@"You have arrived!"];
n1.soundName = UILocalNotificationDefaultSoundName;
n1.fireDate = [NSDate date];
[[UIApplication sharedApplication] presentLocalNotificationNow:n1];
}
}
- 我一直请求授权
- 我检查了背景模式 -> 位置更新
- 我已经注册了用户通知
感谢您的帮助。
好的,我知道了。
问题不在于 UILocalNotification
,而在于位置管理器。
从 iOS 9 开始,CLLocationManager
有一个新的 BOOL 属性 - allowsBackgroundLocationUpdates
默认为 NO.
如果您想继续获取位置更新,您可能需要将其设置为“是”。
这解决了我的问题。
我有一个导航应用程序在获取位置更新时使用 UILocalNotification
。
当应用程序在 iOS 9 台设备(锁定屏幕/按下主屏幕按钮)上进入后台时 - 它停止接收通知。适用于 iOS 8 台设备。
当应用程序在前台而不是在后台时(当然,这要重要得多),我会收到通知。
代码看起来像这样:
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations
{
CLLocation *currLocation = [locations lastObject];
CLLocationDistance dist = [currLocation distanceFromLocation:[[CLLocation alloc] initWithLatitude:self.coordinate.latitude longitude:self.coordinate.longitude]];
BOOL gotToDestination = dist < 60;
if (gotToDestination) {
UILocalNotification* n1 = [[UILocalNotification alloc] init];
n1.alertBody = [NSString stringWithFormat:@"You have arrived!"];
n1.soundName = UILocalNotificationDefaultSoundName;
n1.fireDate = [NSDate date];
[[UIApplication sharedApplication] presentLocalNotificationNow:n1];
}
}
- 我一直请求授权
- 我检查了背景模式 -> 位置更新
- 我已经注册了用户通知
感谢您的帮助。
好的,我知道了。
问题不在于 UILocalNotification
,而在于位置管理器。
从 iOS 9 开始,CLLocationManager
有一个新的 BOOL 属性 - allowsBackgroundLocationUpdates
默认为 NO.
如果您想继续获取位置更新,您可能需要将其设置为“是”。
这解决了我的问题。