Ios7 CLProximityNear时显示后台通知

Ios7 CLProximityNear时显示后台通知,ios7,core-location,ibeacon,Ios7,Core Location,Ibeacon,当我的应用程序在后台,设备进入iBeacon的某个区域,并且当它们的位置接近通知工作时,我尝试显示通知,但它会以1秒的间隔持续显示: - (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{ NSLog(@"Entered beacon region"); [self.locationManager startRangingBeaconsInRegion:self

当我的应用程序在后台,设备进入iBeacon的某个区域,并且当它们的位置接近通知工作时,我尝试显示通知,但它会以1秒的间隔持续显示:

- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region{
    NSLog(@"Entered beacon region");
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}

- (void) locationManager:(CLLocationManager *)manager didExitRegion:(CLRegion *)region{
    NSLog(@"Left region");
    [self.locationManager stopRangingBeaconsInRegion:self.beaconRegion];
}

- (void) locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region {
    [self.locationManager startRangingBeaconsInRegion:self.beaconRegion];
}
- (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {
    CLBeacon *beacon = [[CLBeacon alloc] init];
    beacon = [beacons lastObject];
    self.uuidLabel.text = beacon.proximityUUID.UUIDString;

    if(beacon.proximity == CLProximityUnknown) {
        distanceLabel.text = @"Unknown Proximity";
        [self.view setBackgroundColor:[UIColor grayColor]];
    } else if (beacon.proximity == CLProximityImmediate) {
        distanceLabel.text = @"Immediate";
        [self.view setBackgroundColor:[UIColor redColor]];
    } else if (beacon.proximity == CLProximityNear) {
        distanceLabel.text = @"Near";
        [self.view setBackgroundColor:[UIColor orangeColor]];
        UILocalNotification *inRange = [[UILocalNotification alloc] init];
        inRange.alertBody = [NSString stringWithFormat:@"Entered region!"];
        inRange.soundName = UILocalNotificationDefaultSoundName;
        [[UIApplication sharedApplication] presentLocalNotificationNow:inRange];
    } else if (beacon.proximity == CLProximityFar) {
        distanceLabel.text = @"Far";
        [self.view setBackgroundColor:[UIColor blueColor]];

    }
}

在显示通知后是否应该有方法调用来告诉应用程序它已经显示,并且在用户超出范围并再次返回之前不要继续调用didRangeBeacons方法?

可以这样解决多个通知:

如果只希望发送一次通知,只需定义一个
alreadyDisplayed
标志,该标志在发送通知后设置,然后在发送前检查其值

像这样:

BOOL alreadyDisplayed = NO;

...

- (void) locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region {

  ...

  else if (beacon.proximity == CLProximityNear) {
    distanceLabel.text = @"Near";
    if (!alreadyDisplayed) {
      [self.view setBackgroundColor:[UIColor orangeColor]];
      alreadyDisplayed = YES;
      UILocalNotification *inRange = [[UILocalNotification alloc] init];
      inRange.alertBody = [NSString stringWithFormat:@"Entered region!"];
      inRange.soundName = UILocalNotificationDefaultSoundName;
      [[UIApplication sharedApplication] presentLocalNotificationNow:inRange];
    }
  }

...

}
但您还有第二个问题:

如果你想在背景中这样做,就像你的问题标题所暗示的那样,这根本不起作用。问题是,在iOS检测到你的手机在后台进入了iBeacon区域后,它只允许手机运行5秒钟,然后就可以进入睡眠状态。由于iBeacon的射程约为50米,因此最有可能的情况是,当您处于50米射程的边缘时,这5秒的间隔将开始。用户不太可能走得太快,以至于在你的应用程序进入睡眠前的5秒内进入“近”距离。因此,当你在后台时,通常不可能根据特定的接近程度采取特定的行动


也就是说,如果您希望在前台执行此操作,那么如果您进行更改以避免通知每秒都会出现,这将很好地工作。

在对信标进行测距时,您已经确定,
locationManager:didRangeBeacons:inRegion
将每秒调用一次。每次,
信标
参数将包含所有可见信标的数组


取决于你的应用程序是否包含逻辑来确定新的信标是否可见,或者你是否已经通知了用户。我建议您存储以前发现的信标数组,每次调用
locationManager:didRangeBeacons:inRegion
时,您都要将列表与
beacons
参数的内容进行比较。然后,您应该能够知道是否找到了任何新的信标。

因此,最好先计算出用户何时跨越边界,然后计算出他们跨越区域之间经过了多长时间,以确保这是显示通知的适当时间?我不确定这是否有帮助。在后台,你的应用程序只能在穿越边界后运行5秒钟。如果用户步行到“附近”需要15秒,那么您的应用程序将无法运行,您根本无法显示通知。如果你等到下一次你的应用程序被唤醒,那就太晚了。不幸的是,这是iOS上iBeacons的一个真正的限制,我不相信有解决办法。要在特定距离触发,您的应用程序必须位于前台。