基于位置的`local Notification`在`background中工作但不在`foreground`中工作

时间:2013-05-03 14:29:37

标签: ios uilocalnotification cllocation region background-foreground

只要位置靠近,通知就会激活,但只能在后台运行。即使触发事件发生在前景和后台。

- (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;

}

1 个答案:

答案 0 :(得分:0)

从didReceiveLocalNotification方法内部,您可以向用户显示警报,因为如果应用程序位于前台,则不会处理通知。

UIAlertView *alert = [[UIAlertView alloc]
        initWithTitle: @"Something"
        message: @"Something else you want to tell the user"
        delegate:self
        cancelButtonTitle:@"OK"
        otherButtonTitles:nil];
[alert show];