1

我正在测试 iBeacons 并尝试设置通知何时进入信标范围以及何时离开该区域。当 iPhone/iPad 未处于睡眠模式时,它工作正常,但当通知未唤醒设备时,我收到通知但没有声音/振动。

这是我设置通知的方式:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:@"Exited the region %i", self.i++];
notification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];

我该如何解决这个问题?

4

1 回答 1

0

您在本地通知中使用 scheduleLocalNotification 而不是 presentLocalNotificationNow 吗?我有这段代码工作正常。

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    localNotification = [[UILocalNotification alloc] init];
    beaconsArray=[[NSArray alloc] init];
    // Do any additional setup after loading the view, typically from a nib.

    // Initialize location manager and set ourselves as the delegate
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;

    // Create a NSUUID with the same UUID as the broadcasting beacon
    NSUUID *uuid = [[NSUUID alloc] initWithUUIDString:@"B9407F30-F5F8-466E-AFF9-25556B57FE6D"];


    // Setup a new region with that UUID and same identifier as the broadcasting beacon
    self.myBeaconRegion = [[CLBeaconRegion alloc] initWithProximityUUID:uuid
                                                             identifier:@"com.appcoda.testregion"];





    // Tell location manager to start monitoring for the beacon region
    [self.locationManager startMonitoringForRegion:self.myBeaconRegion];


    // Check if beacon monitoring is available for this device
    if (![CLLocationManager isMonitoringAvailableForClass:[CLBeaconRegion class]]) {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Monitoring not available" message:nil delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]; [alert show]; return;
    }
}


- (void)locationManager:(CLLocationManager*)manager didEnterRegion:(CLRegion *)region
{
    // We entered a region, now start looking for our target beacons!
    self.statusLabel.text = @"Finding beacons.";
    [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];

}

-(void)locationManager:(CLLocationManager*)manager didExitRegion:(CLRegion *)region
{
    // Exited the region
    self.statusLabel.text = @"None found.";
    [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];

}

- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state forRegion:(CLRegion *)region
{

    if (state == CLRegionStateInside)
    {

        [self _sendEnterLocalNotification];
        [self.locationManager startRangingBeaconsInRegion:self.myBeaconRegion];


    }
    else
    {
        [self _sendExitLocalNotification];
        [self.locationManager stopRangingBeaconsInRegion:self.myBeaconRegion];


    }

}

- (void)_sendEnterLocalNotification
{
    if (!_isInsideRegion)
    {
        UILocalNotification *notice = [[UILocalNotification alloc] init];

        notice.alertBody = @"Inside Estimote beacon region!";
        notice.alertAction = @"Open";

        [[UIApplication sharedApplication] scheduleLocalNotification:notice];
    }

    _isInsideRegion = YES;
}

- (void)_sendExitLocalNotification
{
    if (_isInsideRegion)
    {
        UILocalNotification *notice = [[UILocalNotification alloc] init];

        notice.alertBody = @"Left Estimote beacon region!";
        notice.alertAction = @"Open";

        [[UIApplication sharedApplication] scheduleLocalNotification:notice];
    }

    _isInsideRegion = NO;
}

-(void)locationManager:(CLLocationManager*)manager
       didRangeBeacons:(NSArray*)beacons
              inRegion:(CLBeaconRegion*)region
{
      CLBeacon *foundBeacon = [beacons firstObject];
           NSString *meter=[NSString stringWithFormat:@"%.2fm",foundBeacon.accuracy];
//    // Beacon found!

    switch (foundBeacon.proximity) {
        case CLProximityNear:
            proximityString = @"Near";


            break;
        case CLProximityImmediate:
            proximityString = @"Immediate";

        case CLProximityFar:
            proximityString = @"Far";
            break;
        case CLProximityUnknown:
        default:
            proximityString = @"Unknown";
            break;
    }



    self.statusLabel.text =[NSString stringWithFormat:@"%@ Beacon found! at distance %@ %@ with major %@ and minor %@ and rssi power %ld with proximity %ld",foundBeacon.proximityUUID.UUIDString,meter,proximityString,foundBeacon.major ,foundBeacon.minor,(long)foundBeacon.rssi,foundBeacon.proximity];


}
}

对不起,如果看起来有点乱。如果你想在后台有通知,那么你可以使用 self.myBeaconRegion.notifyEntryStateOnDisplay = true;

谢谢

于 2013-12-18T11:12:58.257 回答