この問題は私を夢中にさせています。私がやろうとしているのは、ユーザーがリージョンに入ったときに通知をトリガーすることです。ただし、ユーザーがアプリを使用している場合はアラートメッセージを表示し、アプリがバックグラウンドにある場合はローカル通知を表示する必要があります。
これが私のコードです:
- (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
。しかし今、私はそれを機能させることができます。
私は何かが足りないのですか?