用户只有在离信标足够近时才会收到通知,因为那时 didEnterRegion 无法正常工作。
我的代码是这样的:
if ([region isKindOfClass:[CLBeaconRegion class]] && ([beaconRegionInStringFormat isEqualToString:@"Immediate"] || [beaconRegionInStringFormat isEqualToString:@"Near"]))
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Please open the application";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
但通知将继续发送给用户。我怎样才能只收到一次通知?
最佳答案
1. 给你的 Controller 添加一个标志
界面:@property (nonatomic) BOOL userNotified;
执行:
if (!self.userNotified && [region isKindOfClass:[CLBeaconRegion class]] && ([beaconRegionInStringFormat isEqualToString:@"Immediate"] || [beaconRegionInStringFormat isEqualToString:@"Near"]))
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Please open the application";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
self.userNotified = YES;
}
2. 使用 GCD (Grand Central Dispatch)
使用此方法,每次应用启动时代码只会执行一次。
static dispatch_once_t onceToken;
dispatch_once (&onceToken, ^{
if ([region isKindOfClass:[CLBeaconRegion class]] && ([beaconRegionInStringFormat isEqualToString:@"Immediate"] || [beaconRegionInStringFormat isEqualToString:@"Near"]))
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = @"Please open the application";
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
}
});
关于ios - 当 iBeacon 范围为 "Immediate"或 "near"时如何通知用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652357/