Reputation: 227
Users will only get notified when they are close enough to the beacon, since then didEnterRegion dose not work properly. My code is like this:
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];
}
But the notification will keep sending to users. How can I only get notified once?
Upvotes: 1
Views: 99
Reputation: 751
Interface:
@property (nonatomic) BOOL userNotified;
Implementation:
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;
}
Using this method the code will be executed only once per app launch.
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];
}
});
Upvotes: 3