在后台安排的UILocalNotification不会触发

时间:2013-11-17 23:45:26

标签: ios iphone objective-c cocoa-touch uilocalnotification

我有一个使用位置服务在后台运行的应用程序(通过重要位置更新)。每当更新重要位置时,都会调用- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations,然后调用[[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:...来启动后台任务。接下来,我尝试触发本地通知:

UILocalNotification *notif = [[UILocalNotification alloc] init];
[notif setAlertAction:@"Test"];
[[UIApplication sharedApplication] presentLocalNotificationNow:notif];

但是,不会向用户显示任何通知。是否无法从后台发送本地通知?任何帮助表示赞赏。

谢谢,

1 个答案:

答案 0 :(得分:1)

你需要开火日期:

UILocalNotification *notification = [[UILocalNotification alloc] init];
notification.alertBody = text;
notification.fireDate = [[NSDate alloc] initWithTimeIntervalSinceNow:0];
notification.timeZone = [NSTimeZone defaultTimeZone];
notification.repeatInterval = 0;
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];