I just found out that the region didEnterRegion
return is the one that I start monitoring not the one belong to the beacon that app just saw it.and I was going to use this region to get my custom msg depend on the major&minor for this region.
But it is not possible in my case , so I start ranging when didEnterRegion
get called ,then in didRangeBeacons
which return array of beacons
I got the text msg that belong to this beacon from localDataBase "depend on Major&minor"
And this is my Code : this code work when in app in background and get notify but when app close never get notify
-(void)beaconManager:(id)manager didRangeBeacons:(NSArray *)beacons inRegion:(CLBeaconRegion *)region{
[self handelBeaconsNotification:beacons];
}
-(NSString *) getBeaconFromLocalData:(NSNumber *)regionMajor andRegionMin:(NSNumber *)regionMinor{
PFQuery *query = [PFQuery queryWithClassName:@"Beacons"];
NSArray *allRegions_id = [[NSArray alloc] initWithObjects:[NSNumber numberWithInt:1], nil];
[query whereKey:@"region_id" containedIn:allRegions_id];
NSLog(@"%@",regionMajor);
NSLog(@"%@",regionMinor);
[query whereKey:@"major" equalTo:regionMajor];
[query whereKey:@"minor" equalTo:regionMinor];
[query fromLocalDatastore];
NSArray *data = [query findObjects];
return [[data objectAtIndex:0] valueForKey:@"enter_message"];
}
-(void) handelBeaconsNotification:(NSArray *)beaconsArray{
for (int i = 0 ; i<beaconsArray.count; i++) {
NSString *currentBeaconKey = [NSString stringWithFormat:@"%@-%@",[beaconsArray[i] major],[beaconsArray[i] minor]];
if ([beaconsDictionary valueForKey:currentBeaconKey] == nil) {
NSMutableDictionary *dateDic = [[NSMutableDictionary alloc] init];
[dateDic setValue:[NSDate date] forKey:@"StartVisit"];
[dateDic setValue:nil forKey:@"EndVisit"];
[beaconsDictionary setValue:dateDic forKey:currentBeaconKey];
UILocalNotification *notification = [UILocalNotification new];
notification.alertBody = [self getBeaconFromLocalData:[beaconsArray[i] major] andRegionMin:[beaconsArray[i] minor]];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.fireDate = [NSDate dateWithTimeIntervalSinceNow:2];
notification.timeZone = [NSTimeZone defaultTimeZone];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
}
}
For More Detail visit StackOverFlow