Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel UILocalNotification

I have a problem with my UILocalNotification.

I am scheduling the notification with 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]; } 

Its work fine and I'm correctly receiving the notification. The problem is when I should cancel the notification. Im using this method.

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

Im 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 the ID of which reminder I should delete.
I was thinking about to save the UILocalNotification object in my "Note" object and get it that way, and when I saving to my SQLite database serialize the object and so on ... is there a smarter way?

like image 302
f0rz Avatar asked Jul 01 '10 13:07

f0rz


People also ask

How do I turn off notifications in Swift?

Use the removePendingNotificationsWithIdentifier . You can cancel the notifications by given identifier.

Why has iPhone stopped notifications?

You can fix an iPhone that's not getting notifications by restarting it or making sure notifications are turned on. You should also make sure your iPhone is connected to the internet so apps can receive notifications. If all else fails, you should try resetting the iPhone — just make sure to back it up first.

How do I view notifications on iOS 15?

To see your notifications in Notification Center, do any of the following: On the Lock Screen: Swipe up from the middle of the screen. On other screens: Swipe down from the top center. Then you can scroll up to see older notifications, if there are any.


2 Answers

My solution is to use the UILocalNotification userInfo dictionary. In fact what I do is to generate a unique ID for each of my notifications (of course this ID is something I'm able to retrieve later), then when I want to cancel the notification associated to a given ID I will simply scan all available notifications using the array:

[[UIApplication sharedApplication] scheduledLocalNotifications] 

and then I try to match the notifications by investigating the ID. E.g.:

 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 don't know if this approach is better or not with respect to the Archiving/Unarchving one, however it works and limits data to be saved to just an ID.

Edit: there was a missing braket

like image 74
viggio24 Avatar answered Oct 04 '22 10:10

viggio24


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

  [[UIApplication sharedApplication] cancelAllLocalNotifications]; 
like image 23
progrmr Avatar answered Oct 04 '22 10:10

progrmr