I have this code for local notification, and I have a scheduleNotification and clearNotification using my own method. These are the codes:
- (void)clearNotificat
NSArray *notifications = [[UIApplication sharedApplication] scheduledLocalNotifications];
for (UILocalNotification *not in notifications) {
NSString *dateString=[not.userInfo valueForKey:@"EndDate"];
if([dateString isEqualToString:@"CompareString"])
{
[[UIApplication sharedApplication] cancelLocalNotification:not];
}
}
Its Working Fine With Me.
Cheers..
(void)cancelLocalNotification:(NSString*)notificationID
{
// UILocalNotification *cancelThisNotification = nil;
// BOOL hasNotification = NO;
for (int j =0;j<[[[UIApplication sharedApplication]scheduledLocalNotifications]count]; j++)
{
UILocalNotification *someNotification = [[[UIApplication sharedApplication]scheduledLocalNotifications]objectAtIndex:j];
if([[someNotification.userInfo objectForKey:@"drdid"] isEqualToString:notificationID])
{
NSLog(@"id,notificationID(App) %@ %@ ",[someNotification.userInfo objectForKey:@"drdid"],notificationID);
NSLog(@"canceled notifications %@",someNotification);
[[UIApplication sharedApplication] cancelLocalNotification:someNotification];
}
}
}
I would suggest using the userInfo property on UILocalNotification, as others have mentioned. A simpler implementation that the accepted answer would be:
for(UILocalNotification* notification in [[UIApplication sharedApplication]scheduledLocalNotifications])
{
if([[notification.userInfo objectForKey:@"notification_identifier"] isEqualToString:@"notification_001"])
{
[[UIApplication sharedApplication] cancelLocalNotification:notification];
}
}
A for loop like this is much simpler. I'm not sure if it's more or less optimal, but it's certainly easier to read, and I assume you only have a few notifications to loop through anyway.