UILocalNotification presentNotificationNow Issue

时间:2012-10-09 00:00:14

标签: ios mkmapview uilocalnotification clregion

这个问题一直让我发疯。我想要做的是在用户进入某个区域时触发通知。但是,我想要的是,如果用户正在使用该应用程序显示警报消息,并且该应用程序是在后台显示本地通知。

这是我的代码:

- (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被触发后立即开始通知。但是现在我可以开始工作了。

我错过了什么吗?

1 个答案:

答案 0 :(得分:0)

我认为你正在检查错误位置的活动状态。我做了类似的事情,但是当通知触发时我在AppDelegate中检查活动状态。您应该拦截-didReceiveLocalNotification中的本地通知。如果状态为活动状态,请将UIAlertView推送到那里。如果从后台触发-didEnterRegion或-didExitRegion中的警报,则永远不会看到它。