是否使用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;
謝謝
什麼是睡眠模式? – rckoenes
屏幕關閉。 – RicardoDeus
你在背景中做這個嗎? – NinjaLikesCheez