Cancel UILocalNotification

后端 未结 10 890
余生分开走
余生分开走 2020-11-27 12:53

I have a problem with my UILocalNotification.

I am scheduling the notification with my method.

- (void) sendNewNoteLocalReminder:(NSDate *)date  a         


        
相关标签:
10条回答
  • 2020-11-27 13:37

    My solution is to archive UILocalNotification object you have scheduled with NSKeyedArchiver and store it somewhere (in a plist is preferred). And then, when you want to to can cancel the notification, look up the plist for the correct data and use NSKeyedUnarchiver to unarchive. The code is pretty easy:

    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:notice];
    

    and

    UILocalNotification *notice = [NSKeyedUnarchiver unarchiveObjectWithData:data];
    
    0 讨论(0)
  • 2020-11-27 13:38

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

      [[UIApplication sharedApplication] cancelAllLocalNotifications];
    
    0 讨论(0)
  • 2020-11-27 13:44

    Swift version to cancel particular "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!")
                }
            }
        }
    
    0 讨论(0)
  • 2020-11-27 13:45

    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

    0 讨论(0)
提交回复
热议问题