Carnal
Carnal

Reputation: 22064

iOS - UILocalNotification does not fire (when set in background)

I have this code to schedule a UILocalNotification:

UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.userInfo = @{PLANNED_EVENT_ID : @"some id"};
localNotification.fireDate = someStartDate;
localNotification.alertBody = @"Some text";
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = UILocalNotificationDefaultSoundName;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

This method works as expected when I set the above when application is in foreground, but if I use this code when application is in background (pressed home button), then it is not fired at all?

Why on earth will this not work? How can I get the result I need? I need to able to use this code when application is in background.

Thanks!

To be more clear, in my AppDelegate i have the method

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)

When I get the first notification inside this method (which works, it is scheduled in a view controller), I schedule another notification in the same way (1 min from now). So I tried to have the application in foreground, then it worked, but when I had it in background, it didn't.

Tested on Simulator though.

Upvotes: 2

Views: 3544

Answers (1)

Wain
Wain

Reputation: 119021

The method

- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)

is called when the app opens after a local notification has been shown to the user and the user has opened the app from the notification. If the user doesn't open the app from the notification then this method will not be called. So your next notification is never actually added.

Upvotes: 1

Related Questions