Estimote:didRangeBeacons在后台没有被调用?

时间:2014-01-29 17:49:58

标签: objective-c ibeacon estimote


我想知道为什么如果我的信标接近是立即 - 当应用程序在后台时我没有收到通知。在前台模式下,一切正常。

#import "ESTViewController.h"
#import <ESTBeaconManager.h>

@interface ESTViewController () <ESTBeaconManagerDelegate>

@property (nonatomic, strong) ESTBeaconManager* beaconManager;
@property (nonatomic, strong) ESTBeacon* selectedBeacon;
@property (weak, nonatomic) IBOutlet UILabel *outputLabel;

@end

@implementation ESTViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    /////////////////////////////////////////////////////////////
    // setup Estimote beacon manager

    // craete manager instance
    self.beaconManager = [[ESTBeaconManager alloc] init];
    self.beaconManager.delegate = self;
    self.beaconManager.avoidUnknownStateBeacons = YES;

    // create sample region with major value defined
    ESTBeaconRegion* region = [[ESTBeaconRegion alloc] initWithProximityUUID:ESTIMOTE_PROXIMITY_UUID identifier: @"EstimoteSampleRegion" ];

    NSLog(@"TODO: Update the ESTBeaconRegion with your major / minor number and enable background app refresh in the Settings on your device for the NotificationDemo to work correctly.");

    // start looking for estimote beacons in region
    [self.beaconManager startMonitoringForRegion:region];
    [self.beaconManager startRangingBeaconsInRegion:region];

    [self.beaconManager requestStateForRegion:region];

    // setup view
    self.outputLabel.text = @"Seachring for reagion";

}

-(void)beaconManager:(ESTBeaconManager *)manager
   didDetermineState:(CLRegionState)state
           forRegion:(ESTBeaconRegion *)region
{

    // NSLog(@"Region: %@", region);

    if(state == CLRegionStateInside)
    {
        NSLog(@"State: CLRegionStateInside");

    }
    else
    {
        NSLog(@"State: CLRegionOutside");
    }
}

-(void)beaconManager:(ESTBeaconManager *)manager
     didRangeBeacons:(NSArray *)beacons
            inRegion:(ESTBeaconRegion *)region
{
    if([beacons count] > 0)
    {

        self.selectedBeacon = [beacons firstObject]; // get the closest


        NSString* labelText = [NSString stringWithFormat:
                               @"Major: %i, Minor: %i\nRegion: ",
                               [self.selectedBeacon.major unsignedShortValue],
                               [self.selectedBeacon.minor unsignedShortValue]];

        // calculate and set new y position
        switch (self.selectedBeacon.proximity)
        {
            case CLProximityUnknown:
                labelText = [labelText stringByAppendingString: @"Unknown"];
                break;
            case CLProximityImmediate:
                labelText = [labelText stringByAppendingString: @"Immediate"];

                [self fireNotification];

                break;
            case CLProximityNear:
                labelText = [labelText stringByAppendingString: @"Near"];
                break;
            case CLProximityFar:
                labelText = [labelText stringByAppendingString: @"Far"];
                break;

            default:
                break;
        }

        self.outputLabel.text = labelText;
    }
}


-(void)fireNotification {
    NSLog(@"Fire Notification from background");

    // present local notification
    UILocalNotification *notification = [[UILocalNotification alloc] init];
    notification.alertBody = @"You are very close to the beacon";
    notification.soundName = UILocalNotificationDefaultSoundName;

    [[UIApplication sharedApplication] presentLocalNotificationNow:notification];

    // Request a server...
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

1 个答案:

答案 0 :(得分:4)

对于iOS,iBeacons的范围通常仅适用于前台。检查CLProximityImmediate的代码是一种测距回调方法。

如果你的应用程序在后台,它只会在进入/退出受监控的iBeacon区域后接收五秒钟的测距回调。

此原则适用于标准iOS CoreLocation API。当您使用专有的Estimote SDK时,同样的限制也适用,因为它是标准API的包装。