每当我们进入或离开某个地区时,我都会显示本地通知
locationManager:didRangeBeacons:inRegion method of CLLocationManagerDelegate:
像这样
//local notification sent from transmitter to receiver
switch (beacon.proximity)
{
case CLProximityNear:
message = @"You are near the ibeacon device";
bgColor = [UIColor blueColor];
break;
case CLProximityImmediate:
message = @"You are very closer to the device..";
bgColor = [UIColor colorWithRed:.0f green:.0f blue:230.0f alpha:1.0f];
break;
}
现在,当接收方检测到发送方并发送本地通知时,接收方将获取本地通知。另外,我想将本地通知设置为一天,即接收方只能从发送方接收一次消息。非常感谢宝贵的建议。
注意:我将iPad mini用作发射器,将iPhone 5用作接收器。
最佳答案
您需要跟踪在didRangeBeaconsInRegion
方法中检测到每个信标的时间,并使用软件过滤器忽略某些信标(如果最近发现过的话)。
here.显示了如何执行此操作的示例。其核心是:
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLRegion *)region
{
for (CLBeacon *beacon in beacons) {
Boolean shouldSendNotification = NO;
NSDate *now = [NSDate date];
NSString *beaconKey = [NSString stringWithFormat:@"%@_%ld_%ld", [beacon.proximityUUID UUIDString], (long) beacon.major, (long) beacon.minor];
NSLog(@"Ranged UUID: %@ Major:%ld Minor:%ld RSSI:%ld", [beacon.proximityUUID UUIDString], (long)beacon.major, (long)beacon.minor, (long)beacon.rssi);
if ([beaconLastSeen objectForKey:beaconKey] == Nil) {
NSLog(@"This beacon has never been seen before");
shouldSendNotification = YES;
}
else {
NSDate *lastSeen = [beaconLastSeen objectForKey:beaconKey];
NSTimeInterval secondsSinceLastSeen = [now timeIntervalSinceDate:lastSeen];
NSLog(@"This beacon was last seen at %@, which was %.0f seconds ago", lastSeen, secondsSinceLastSeen);
if (secondsSinceLastSeen < 3600*24 /* one day in seconds */) {
shouldSendNotification = YES;
}
}
if (shouldSendNotification) {
[self sendLocalNotification];
}
}
}