ios - iBeacon send notification for multiple beacon -
scenario
i have 3 merchants in same street. each merchant hold beacon. want end users notify once go near (clproximitynear
).
source code
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];
delegate
- (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 inside region %@", region.identifier]; break; } case clregionstateoutside: notification.alertbody = [nsstring stringwithformat:@"you 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) { (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]; } } }
problem
i unlimited notifications when go close beacon
questions
i want end users 1 notification when go near.
e.g. hi, welcome shop a, offer great discount today
then when go far, notify again thank visiting. have nice day
should save current state/proximity particular merchant (beacon) nsuserdefaults
? have try that
clproximity currentproximity = ... // proximity particular beacon saved in nsuserdefaults
switch (beacon.proximity) { case clproximityimmediate: // 0 - 20cm [logtext appendstring:@" immediate"]; // if 1 second ago immediate, still immediate if (currentproximity == clproximityimmediate) continue; currentproximity = clproximityimmediate; break; ... }
after modified code above, no notification when press home button (app running in background)
edit
i think problem when app running on background, ususerdefaults
not works. want keep proximity state every beacon when app not running @ all.
any suggestion?
you need logic here.
you should set flag ibeacons. when perticular ibeacons comes near should set flag ibeacons near. clproximitynear
flag off when goes far clproximityfar.
so need below step.
- set flag ibeacons when comes near (show notiication).
- flag off when goes far.
make sure ibeacons need manage seperate flags
Comments
Post a Comment