作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在同一条街上有 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
实际上不起作用。即使应用程序根本没有运行,我也想为每个信标保持接近状态。
有什么建议吗?
最佳答案
你需要在这里做一些逻辑。
您应该为所有 iBeacon 设置标志。当一个特定的 iBeacons 靠近时,你应该设置这个 iBeacons 靠近的标志。 CLProximityNear
当它到达远 CLProximityFar 时,标志将关闭。
因此您需要执行以下步骤。
确保您需要为所有 iBeacon 管理单独的标志
关于ios - iBeacon 为多个信标发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443816/
我是一名优秀的程序员,十分优秀!