During performFetchWithCompletionHandler
we are canceling local notifications that were set during previous runs of the app and setting updated ones.
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *notif in notifications) {
if (/*some condition is met re the notif*/) {
[[UIApplication sharedApplication] cancelLocalNotification:notif];
}
}
}
...
UILocalNotification *localNotif = [[UILocalNotification alloc] init];
...
[[UIApplication sharedApplication] scheduleLocalNotification:localNotif];
When the line of code for cancelLocalNotification:notif
is called, another old local notification sometimes pop even though it has already popped by iOS in the past. Since I am not setting this notification as if iOS doesn't remember that it has already popped that notification.
It doesn't always happens, but we still didn't manage to figure out when this happens or the cause of this. Any thoughts?
**EDIT:**I just want to be clear that the strange issue I am seeing is that calling cancelLocalNotification for ANY notification sometimes causes other notification to pop. This happens only in rare cases when I had two notification set for the same time exactly.
I don't know the structure of your app, but if it's possible, it's a lot easier for you to cancel all local notifications instead of looping through them. Write:
[[UIApplication sharedApplication] cancelAllLocalNotifications];
If you only want to cancel a subset of all of the local notifications instead of all local notifications, I'd recommend:
This is the simplest way of doing it since, by doing so, you don't have to store any references to the local notifications you've set in order to cancel a subset of notifications.
This is also helpful if you need to schedule more than 64 local notifications. Apple only allows for scheduling 64 local notifications at a time (the 64 local notifications with the earliest fire date). Queueing any subsequent local notifications are discarded. If you have designed your app such that it cancels all local notifications and enqueues relevant local notifications again, then the 64 local notification limit becomes less of an issue.