Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deleting LocalNotifications from NotificationCenter (iOS 6)

I have a problem with one aspect of managing LocalNotifications - deleting single notification in iOS 6.

enter image description here

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:

  • using [[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
  • removing items from [[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?

like image 238
deleteme Avatar asked Oct 25 '12 11:10

deleteme


1 Answers

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;
    }
}
like image 61
Wolverine Avatar answered Oct 13 '22 23:10

Wolverine