cancelLocalNotification似乎不起作用

在下面的代码中,我检查当前任务(正在编辑的任务)是否具有localNotification。 如果是这样,我尝试取消该通知。 然后(如果fireDate还没有通过),我使用(可能)更改的信息更新通知的alertBody并重新计划它。

问题是,如果我创build一个通知,然后编辑它,我最终会通知两个通知。 不知何故,原来的一个没有取消…

if ([currentTask localNotification]) { [[UIApplication sharedApplication] cancelLocalNotification:[currentTask localNotification]]; if ([[NSDate date] compare:[currentTask localNotification].fireDate] == NSOrderedAscending) { NSLog(@"current task name: %@", [currentTask name]); NSString *temp = [NSString stringWithFormat:@"Alert:\n%@", [currentTask name]]; [[currentTask localNotification] setAlertBody:temp]; [[UIApplication sharedApplication] scheduleLocalNotification:[currentTask localNotification]]; } else [currentTask setLocalNotification:nil]; } 

关于这个问题的任何见解?

我知道在预定的时候,通知会复制一份副本 – cancelLocalNotification如何find正确的副本? 它是否通过testing性质的平等来做到这一点? 还是它复制指针地址并匹配呢? 换句话说,如果其中一个属性发生了变化,cancelLocalNotification将不能与原来预定的通知相匹配吗?

我将在iOS 9中添加我的发现。以前我的代码如下所示:

 UILocalNotification* notification = getNotification(); [[UIApplication sharedApplication] cancelLocalNotification: notification]; notification.alertBody = newBody; [[UIApplication sharedApplication] scheduleLocalNotification: notification]; 

在iOS 9之前工作很好,取消了现有的实例,然后用一个新的alertBody或soundName重新调度它。 然后,随着iOS 9,突然运行这个代码实际上并没有取消以前的通知,所以我结束了比我想要的更多的通知!

对我来说,修复是不要懒得重新使用旧的通知,所以现在我创build一个新的通知,并填充旧通知的字段。 作品一种享受!

编辑:我似乎仍然得到[[UIApplication sharedApplication] cancelLocalNotification: notification]不能在其他情况下工作的问题…我有我的通知自动重复自己每一分钟,似乎有可能是在Xcode 7.0的意思它有时被忽略: https : //forums.developer.apple.com/thread/9226

编辑二:看起来像在应用程序重复本地通知计划之前,它是build立在对iOS 9无法取消..至less不是很容易…仍然在寻找解决scheme,因为我的应用程序是围绕重复本地通知提醒!

编辑三:我设法摆脱一个挥之不去的通知,强制退出应用程序后,企图取消…这似乎已经奏效…这不是一个很好的解决scheme,当然不是我想让我的用户去做,如果有其他的select…我已经发布了一个单独的问题 ,看看有没有人有任何进一步的想法。

我遇到了同样的问题。 我使用的解决scheme是。

 // Sort the notification on the fire date of the notification. NSSortDescriptor * fireDateDesc = [NSSortDescriptor sortDescriptorWithKey:@"fireDate" ascending:YES]; //get all scheduled notifications. NSArray * notifications = [[[UIApplication sharedApplication] scheduledLocalNotifications] sortedArrayUsingDescriptors:@[fireDateDesc]]; // Cancel all scheduled notifications. [[UIApplication sharedApplication] cancelAllLocalNotifications]; for (int i=0; i<[notifications count]; i++){ UILocalNotification* oneEvent = [notifications objectAtIndex:i]; // update notification data here an then schedule the notification. [[UIApplication sharedApplication] scheduleLocalNotification:oneEvent]; } 

通过逐个取消UILocalNotifications来更新通知中的某些内容会产生一个问题,即有时通知不会被取消。

所以我实现的解决scheme是:

1:获取所有预定的通知,并将其存储在任何你喜欢的地方。

2:然后取消所有预定的通知。

3:从第一步通知通知,更新任何你想要的和安排通知在旅途中。

希望这可以帮助。

它看起来像UILocalNotification的任何属性已经被调度,那cancelLocalNotification将不起作用。 这使我认为,cancelLocalNotification必须检查不同的属性相对于当前计划的通知副本的平等,以查看哪一个匹配。