I have a problem with one aspect of managing LocalNotifications - deleting single notification in iOS 6.
I am able to create a LocalNotification with text content and fire date and it works.
On iOS 5 notification occurs as an AlertView on home screen with two buttons, and dissapears after touching either. I can cancel the fire of notification using [[UIApplication sharedApplication] cancelLocalNotification:theNotification]
. There is no problem here.
In iOS 6 notification occurs in a Notificaton Center as shown above when fired. If I cancel it using [[UIApplication sharedApplication] cancelLocalNotification:theNotification]
, it won't fire - it works. But after it fires...
My problem:
I am not able to erase this single fired notification from the Notification Center. For example, I would like the notification to dissappear after it is touched, or after some action inside application is done.
What I tried:
[[UIApplication sharedApplication] cancelLocalNotification:theNotification]
- cancels notification fire (actually there's no need since the notification did fire already), but doesn't erase fired notification from Notification Center[[UIApplication sharedApplication] scheduledLocalNotifications]
array - doesn't work, because this function always returns empty array, no matter how many (working!) notifications are set (does anybody know why?)What I'm doing now
I am deleting all notifications using [[UIApplication sharedApplication] cancelAllLocalNotifications]
- it cancels them AND erases from Notification Center, and then create them back, without the one I wanted to erase.
As you can see, this is rather idiotic and redundant solution, but I couldn't find better so far.
Any suggestions?
Try This.....May be this help...Write this code from where you want to clear all your notifications.
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:1];
[[UIApplication sharedApplication] setApplicationIconBadgeNumber:0];
[[UIApplication sharedApplication] cancelAllLocalNotifications];
To erase one single notification
You can get an array of scheduled notification from: @property(nonatomic,copy) NSArray *scheduledLocalNotifications
Get the one you want by the index number of your choosing and then pass the UILocalNotification* to - (void)cancelLocalNotification:(UILocalNotification *)notification.
You can save a unique value for key in your local notification's userinfo. Get all local notification, loop through the array and delete the particular notification.
Code as follows,
UIApplication *app = [UIApplication sharedApplication];
NSArray *eventArray = [app scheduledLocalNotifications];
for (int i=0; i<[eventArray count]; i++)
{
UILocalNotification* oneEvent = [eventArray objectAtIndex:i];
NSDictionary *userInfoCurrent = oneEvent.userInfo;
NSString *uid=[NSString stringWithFormat:@"%@",[userInfoCurrent valueForKey:@"uid"]];
if ([uid isEqualToString:uidtodelete])
{
//Cancelling local notification
[app cancelLocalNotification:oneEvent];
break;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With