2013-11-15 46 views
1

我正在測試iBeacons,並試圖設置一個通知,告知我何時進入信標範圍以及何時離開該區域。 當iPhone/iPad未處於睡眠模式時,它可以正常工作,但當通知沒有喚醒設備時,我會收到通知,但沒有聲音/振動。UILocalNotification沒有在睡眠模式下發射

下面是我設置的通知:

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

我該如何解決這個問題?

+0

什麼是睡眠模式? – rckoenes

+0

屏幕關閉。 – RicardoDeus

+0

你在背景中做這個嗎? – NinjaLikesCheez

回答

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出現小messy.And = TRUE;

謝謝

+0

對不起,我沒有注意到你的答案。但無論如何謝謝你試圖幫助! – RicardoDeus