通知应该在位置接近时激活,但它只在后台工作。即使触发事件同时发生在前景和背景中。
- (void)locationManager:(CLLocationManager *)manager didEnterRegion:(CLRegion *)region {
//implement local notification:
UIApplication *app = [UIApplication sharedApplication];
UILocalNotification *notification = [[UILocalNotification alloc] init];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
if (notification == nil)
return;
notification.fireDate = [NSDate date];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.alertBody = [selectedTask discription];
notification.alertAction = [selectedTask taskName];
notification.soundName = UILocalNotificationDefaultSoundName;
notification.applicationIconBadgeNumber = 1;
[app presentLocalNotificationNow:notification];
localNotification = nil;
}
从didReceiveLocalNotification方法内部,你可以向用户显示一个警告,因为如果应用程序在前台,通知不会被处理。
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle: @"Something"
message: @"Something else you want to tell the user"
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];