这是我的代码:
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region
{
UIApplication *app = [UIApplication sharedApplication];
if (app.applicationState == UIApplicationStateActive)
{
NSURL *musicFile = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"Alarm" ofType:@"caf"]];
AVAudioPlayer *audioFile = [[AVAudioPlayer alloc] init];
audioFile = [[AVAudioPlayer alloc] initWithContentsOfURL:musicFile error:NULL];
[audioFile play];
UIAlertView *arrivingDestinationAlert = [[UIAlertView alloc] initWithTitle: @"Arriving" message:[NSString stringWithFormat:@"Alert"] delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[arrivingDestinationAlert show];
}else if(app.applicationState == UIApplicationStateBackground || app.applicationState == UIApplicationStateInactive)
{
UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = [NSString stringWithFormat:@"%d to reach your destination", self.distToRemind];
notification.alertAction = @"Destination Approaching";
notification.hasAction = YES;
notification.soundName = UILocalNotificationDefaultSoundName;
[app presentLocalNotificationNow:notification];
}
[manager stopMonitoringForRegion:region];
}
我以前做的是工作。午餐是本地通知而不询问应用程序的状态是后台还是活动状态。我只是在didEnterRegion
被触发后立即开始通知。但是现在我可以开始工作了。
我错过了什么吗?
答案 0 :(得分:0)
我认为你正在检查错误位置的活动状态。我做了类似的事情,但是当通知触发时我在AppDelegate中检查活动状态。您应该拦截-didReceiveLocalNotification
中的本地通知。如果状态为活动状态,请将UIAlertView推送到那里。如果从后台触发-didEnterRegion或-didExitRegion中的警报,则永远不会看到它。