Cancel UILocalNotification

I have a problem with my UILocalNotification.

I schedule a notification using my method.

- (void) sendNewNoteLocalReminder:(NSDate *)date alrt:(NSString *)title { // some code ... UILocalNotification *localNotif = [[UILocalNotification alloc] init]; if (localNotif == nil) return; localNotif.fireDate = itemDate; localNotif.timeZone = [NSTimeZone defaultTimeZone]; localNotif.alertAction = NSLocalizedString(@"View Details", nil); localNotif.alertBody = title; localNotif.soundName = UILocalNotificationDefaultSoundName; localNotif.applicationIconBadgeNumber = 0; NSDictionary *infoDict = [NSDictionary dictionaryWithObject:stringID forKey:@"id"]; localNotif.userInfo = infoDict; [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; [localNotif release]; } 

His work is beautiful, and I receive the notification correctly. The problem is that I have to cancel the notification. I am using this method.

 - (void) deleteNewNoteLocalReminder:(NSString*) reminderID noteIDe:(NSInteger)noteIDE { [[UIApplication sharedApplication] cancelLocalNotification:(UILocalNotification *)notification ???? } 

I'm not sure what to do here, but my questions are:

How do I know which UILocalNotification object I should delete?
Is there a way to list all notifications?

The only thing I have is a reminder id that I have to delete.
I was thinking about saving the UILocalNotification object in my Note object and getting it that way, and when I save the serialization of the object in my SQLite database, etc ... is there a smarter way?

+47
iphone cocoa-touch ios4 notifications
Jul 01 '10 at 13:32
source share
10 answers

You can get a list of all scheduled notifications from scheduledLocalNotifications or you can cancel all of them:

  [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
+57
Jul 01 '10 at
source share
— -

My solution is to use the UILocalNotification userInfo dictionary . In fact, what I do is to create a unique identifier for each of my notifications (of course, this ID is what I can get later), then when I want to cancel the notification associated with this ID , I just scanning all available notifications with an array:

 [[UIApplication sharedApplication] scheduledLocalNotifications] 

and then I will try to match notifications by examining the ID . For example:.

 NSString *myIDToCancel = @"some_id_to_cancel"; UILocalNotification *notificationToCancel=nil; for(UILocalNotification *aNotif in [[UIApplication sharedApplication] scheduledLocalNotifications]) { if([[aNotif.userInfo objectForKey:@"ID"] isEqualToString:myIDToCancel]) { notificationToCancel=aNotif; break; } } if(notificationToCancel) [[UIApplication sharedApplication] cancelLocalNotification:notificationToCancel]; 

I do not know if this approach is better or not with respect to Archiving / Destruction, however it works and restricts the data to be saved with only an identifier.

Edit: Missing layout

+93
Feb 02 2018-11-11T00:
source share

My solution is to archive the UILocalNotification object that you planned with NSKeyedArchiver and store it somewhere (preferably plist). And then, when you want to cancel the notification, look at plist for the correct data and use NSKeyedUnarchiver to unlock. The code is pretty simple:

 NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice]; 

and

 UILocalNotification *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 
+13
Jul 26 '10 at 10:36
source share

My solution is when you create a UILocalNotification while you create one NSMutableDictionary , and save this notification as a value for the key as your identifier and put it NSMutableDictionay in your NSUserDefaults

So, when you want to undo any specific local modification at that time, you write [dictionary valueforkey @"KEY"] , where you pass in your identifier as a key to receive this local notification and pass it

  [[UIApplication sharedApplication] scheduleLocalNotification:localNotif]; 
+6
Jul 30 '10 at 7:41
source share

In swift, you first add a dict to notify userInfo by:

 let dict:NSDictionary = ["ID" : "someString"] notification.userInfo = dict as! [String : String] 

Then you want to get an array of existing notifications (2) and iterate over each of them (3) until you find the one you are looking for. Once you find it, cancel it (4).

 // 1. Pass in the string you want to cancel func cancelLocalNotification(uniqueId: String){ // 2. Create an array of notifications, ensuring to use `if let` so it fails gracefully if let notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications { // 3. For each notification in the array ... for notif in notifyArray as [UILocalNotification] { // ... try to cast the notification to the dictionary object if let info = notif.userInfo as? [String: String] { // 4. If the dictionary object ID is equal to the string you passed in ... if info["ID"] == uniqueId { // ... cancel the current notification UIApplication.sharedApplication().cancelLocalNotification(notif) } } } } } 
+3
Jan 06 '16 at 3:21
source share

Quick version for canceling a specific "Local Notification" using userinfo .:

 func cancelLocalNotification(UNIQUE_ID: String){ var notifyCancel = UILocalNotification() var notifyArray = UIApplication.sharedApplication().scheduledLocalNotifications for notifyCancel in notifyArray as! [UILocalNotification]{ let info: [String: String] = notifyCancel.userInfo as! [String: String] if info[uniqueId] == uniqueId{ UIApplication.sharedApplication().cancelLocalNotification(notifyCancel) }else{ println("No Local Notification Found!") } } } 
+1
Jun 25 '15 at 11:49
source share

Swift version :

 UIApplication.sharedApplication().cancelAllLocalNotifications() 
+1
Feb 14 '16 at 13:03
source share

My solution is to delete all scheduled notifications about these methods.

 -applicationDidFinishLaunchingWithOptions: - (void)applicationDidBecomeActive:(UIApplication *)application; - (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification; 

from

[[UAppication sharedApplication] cancelAllLocalNotifications];

You can go to the LocalNotification object that activated your application with.

  UILocalNotification *localNotif = [launchOptions objectForKey: UIApplicationLaunchOptionsLocalNotificationKey]; 

Then I redirect new notifications to

 - (void)applicationDidEnterBackground:(UIApplication *)application; 

This avoids accounting for all notifications. I also send some contextual information to the userInfo dictionary. Then it helps to go to the right place in the application.

0
Jun 15 '12 at 13:50
source share

Thanks @ viggio24 for a good answer, this is a quick version

 var notifyCancel = UILocalNotification() var notifyArray=UIApplication.sharedApplication().scheduledLocalNotifications var i = 0 while(i<notifyArray.count){ if let notify = notifyArray[i] as? UILocalNotification{ if let info = notify.userInfo as? Dictionary<String,String> { if let s = info["name"] { if(name==s){//name is the the one you want cancel notifyCancel = notify break } } } } i++ } 
0
Mar 09 '15 at 9:19
source share

Swift 5:

 UNUserNotificationCenter.current().removePendingNotificationRequests(withIdentifiers: arrayContainingIdentifiers) 
0
May 19 '19 at 4:53
source share



All Articles