1
UILocalNotification* localNotification = [[UILocalNotification alloc] init];
localNotification.alertBody =[NSString stringWithFormat:@"meeting schedule from %@ ",[namelist objectAtIndex:i]];
localNotification.timeZone = [NSTimeZone defaultTimeZone];
localNotification.soundName = @"meetting.mp3";
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
[[NSNotificationCenter defaultCenter] postNotificationName:@"timerInvoked" object:self];

I am using the above code to create local notifications. But previous notifications are not cleared. It again fires when new notifications are fired.

I use [[UIApplication sharedApplication] cancelAllLocalNotifications];

The notification is not fired in the back ground. How do I remove the notification after it has fired?

Wain
  • 118,658
  • 15
  • 128
  • 151
Anupama
  • 373
  • 2
  • 3
  • 17
  • Where you are setting `firedate` of local notification? Did you really getting notification? – Anil Varghese Jan 29 '14 at 10:51
  • I am using Local notification to send the information to notification from service – Anupama Jan 29 '14 at 10:53
  • I guess you dont have clear idea about local notification. I'am not sure you want UILocalNotification or NSNotification. Could you clarify your requirements? – Anil Varghese Jan 29 '14 at 10:58
  • I am new bee to ios.I am using Local notification for the below scenario. I am creating chat application..when messages arrived from service I send the information to Notification.It displayed in the notification but when new data is occurred it sends the new messages with old message – Anupama Jan 29 '14 at 11:02

3 Answers3

0

cancelAllLocalNotifications removes pending notifications. I don't think you can remove notifications from notification center once they have been fired. The user sets (via Settings app) the number of notifications they want to keep and they are automatically removed once the number exceeds that limit.

Wain
  • 118,658
  • 15
  • 128
  • 151
  • I am new bee to ios.I am using Local notification for the below scenario. I am creating chat application..when messages arrived from service I send the information to Notification.It displayed in the notification but when new data is occurred it sends the new messages with old message – Anupama Jan 29 '14 at 11:07
  • Is it just that you don't remove the notification (or open the app from the notification). So when another message is received you now have 2 visible notifications? That is how it should be... – Wain Jan 29 '14 at 11:09
  • You may want to look at using `applicationIconBadgeNumber` to show how many unread messages there are. – Wain Jan 29 '14 at 11:10
  • I want to clear the previous notification..when i click the notification which is arrived app in back ground app crashes..and notification is not cleared – Anupama Jan 29 '14 at 11:13
  • You can't remove the notification. Just fix the crash so the app works properly... – Wain Jan 29 '14 at 11:14
  • can u suggest the reason for crash – Anupama Jan 29 '14 at 11:16
  • Not without you showing the code, exception message and stack trace - that is a different question... – Wain Jan 29 '14 at 11:16
0

When the application receives a local notification, the following method will get called. Put it in the app delegate:

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    UIApplication *app = [UIApplication sharedApplication];
    NSInteger badgeNumber = [app applicationIconBadgeNumber];// Take the current badge number
    badgeNumber--;    // decrement by one 
    [app setApplicationIconBadgeNumber:badgeNumber];  // set ne badge number
    [app cancelLocalNotification:notification];    // cancel the received notification. It will clear the notification from banner alos
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Anil Varghese
  • 42,757
  • 9
  • 93
  • 110
0

If you want to be able to remove specific notifications after they've fired, you'll need to store them.

After:

[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

You would also save it in NSUserDefaults, using NSKeyedArchiver to convert it to NSData.

NSData *notificationData = [NSKeyedArchiver archivedDataWithRootObject:localNotification];

[[NSUserDefaults standardUserDefaults] setObject:notificationData forKey:[NSString  stringWithFormat:@"%d", someKeyOfYourChoice]];

To get it back as UILocalNotification you use NSKeyedUnarchiver. Then you're able to delete it using the cancelLocalNotification method.

Further explained here (Swift version + link to original Obj-C solution)

Community
  • 1
  • 1
Rygen
  • 309
  • 4
  • 15