gpt4 book ai didi

ios - iBeacon 为多个信标发送通知

转载 作者:行者123 更新时间:2023-11-29 12:42:55 24 4
gpt4 key购买 nike

场景

我在同一条街上有 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 时,标志将关闭。

因此您需要执行以下步骤。

  1. 将所有 iBeacon 的标志设置为接近时(显示通知)。
  2. 在走得远时发出信号。

确保您需要为所有 iBeacon 管理单独的标志

关于ios - iBeacon 为多个信标发送通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24443816/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com