【发布时间】:2014-06-27 04:12:16
【问题描述】:
场景
我在同一条街上有 3 个商家。每个商人都有一个信标。我希望最终用户在靠近 (CLProximityNear) 时收到通知。
源代码
AppDelegate.m
self.iBeaconManager = [[CLLocationManager alloc] init];
self.iBeaconManager.delegate = self;
NSUUID *proximityUUID = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];
CLBeaconRegion *region = [[CLBeaconRegion alloc] initWithProximityUUID:proximityUUID identifier:@"BeaconIdentifier"];
region.notifyEntryStateOnDisplay = YES;
[self.iBeaconManager startMonitoringForRegion:region];
委托
- (void)locationManager:(CLLocationManager *)manager didStartMonitoringForRegion:(CLRegion *)region
{
[self.iBeaconManager requestStateForRegion:region];
}
- (void)locationManager:(CLLocationManager *)manager didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
switch (state) {
case CLRegionStateInside: {
[self.iBeaconManager startRangingBeaconsInRegion:region];
notification.alertBody = [NSString stringWithFormat:@"You are inside region %@", region.identifier];
break;
}
case CLRegionStateOutside:
notification.alertBody = [NSString stringWithFormat:@"You are outside region %@", region.identifier];
case CLRegionStateUnknown:
default:
NSLog(@"Region unknown");
}
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
- (void)locationManager:(CLLocationManager *)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region
{
if ([beacons count] > 0) {
for (CLBeacon *beacon in beacons) {
NSMutableString *logText = [NSMutableString stringWithFormat:@"Beacon: major: %d minor: %d Distance: %f is", [beacon.major intValue], [beacon.minor intValue], beacon.accuracy];
switch (beacon.proximity) {
case CLProximityImmediate: // 0 - 20cm
[logText appendString:@" IMMEDIATE"];
break;
case CLProximityNear: // 20cm - 2m
[logText appendString:@" NEAR"];
break;
case CLProximityFar: // 2m - 70m
[logText appendString:@" FAR"];
break;
default:
[logText appendString:@" UNKNOWN"];
break;
}
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = logText;
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
}
问题
当我靠近信标时,我会收到无限的通知
问题
我希望这些最终用户在靠近时只收到一个通知。
例如您好,欢迎光临 A 店,我们只提供今天的超值折扣
然后当他们走远了,再次通知感谢您的光临。祝你有美好的一天
我应该将那个特定商家(信标)的当前状态/邻近度保存到NSUserDefaults吗?其实我试过了
CLProximity currentProximity = ... // 从保存在 NSUserDefaults 中的特定信标获取接近度
switch (beacon.proximity) {
case CLProximityImmediate: // 0 - 20cm
[logText appendString:@" IMMEDIATE"];
// if 1 second ago is Immediate, now still immediate
if (currentProximity == CLProximityImmediate) continue;
currentProximity = CLProximityImmediate;
break;
...
}
修改上面的代码后,当我按下主页按钮时没有收到通知(应用程序在后台运行)
编辑
我认为问题在于当应用程序在后台运行时,USUserDefaults 实际上无法正常工作。即使应用程序根本没有运行,我也想保持每个信标的接近状态。
有什么建议吗?
【问题讨论】:
标签: ios objective-c notifications ibeacon