Kyle Rosenbluth
Kyle Rosenbluth

Reputation: 1682

UILocalNotification scheduled in background doesn't fire

I have an app that runs in the background using location services (via Significant Location updates). Whenever a significant location is updated, - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations gets called and I then call [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:... to start my background task. Next, I try to fire a local notification:

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

However, no notification gets presented to the user. Are local notifications not able to be sent from the background? Any help is appreciated.

Thanks,

Upvotes: 4

Views: 2130

Answers (1)

user1201586
user1201586

Reputation: 11

You need fire date:

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

Upvotes: 1

Related Questions