UILocalNotification未在睡眠模式下触发

时间:2013-11-15 13:55:44

标签: ios uilocalnotification ibeacon

我正在测试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];

如何解决这个问题?

1 个答案:

答案 0 :(得分: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;

由于