0

この問題は私を夢中にさせています。私がやろうとしているのは、ユーザーがリージョンに入ったときに通知をトリガーすることです。ただし、ユーザーがアプリを使用している場合はアラートメッセージを表示し、アプリがバックグラウンドにある場合はローカル通知を表示する必要があります。

これが私のコードです:

- (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。しかし今、私はそれを機能させることができます。

私は何かが足りないのですか?

4

1 に答える 1

0

間違った場所でアクティブな状態をチェックしていると思います。私は非常に似たようなことをしていますが、通知が発生したときに AppDelegate でアクティブな状態をチェックします。でローカル通知を傍受する必要があり-didReceiveLocalNotificationます。状態がアクティブな場合は、そこに UIAlertView をプッシュします。バックグラウンドから -didEnterRegion または -didExitRegion からアラートを発生させた場合、それは表示されません。

于 2012-10-10T00:15:59.717 に答える