iOS上的旧本地通知触发

时间:2013-07-29 18:00:47

标签: ios objective-c uilocalnotification

我正在安排一组UILocalNotifications。他们按时开火,似乎工作正常。但是,有时,当通知触发时,会有多个(有时只有一个) OLD 通知一起触发。

例如,我安排UILocalNotification在星期一下午5点取出垃圾,没有重复间隔。它没有问题,按时发射。星期二,我有一个UILocalNotification,在星期二下午5点带回垃圾箱,再没有重复间隔。当那个人开火时,我会看到 NOW 的正确通知,但是在当前通知下方还会收到另一个通知 1天前。我没有重新安排此通知。这似乎是昨天的通知。

这是非常离奇的,我无法在任何一致的基础上重现它。我想也许有一些旧的通知以某种方式被添加,所以我添加了一些逻辑来运行所有预定的通知并删除任何具有过去的开火日期但没有帮助。有没有人见过这个问题?是否有一些手动清除通知,我需要在一个人发生火灾时做什么?

编辑:添加了一些调度代码

//only schedule if the alert date is later than now
if ([AMDateUtil isNowEarlierThanDate:alertDate]) {
    //create the notification and setup some properties
    UILocalNotification *localNotification = [[UILocalNotification alloc] init];
    localNotification.fireDate = alertDate;
    localNotification.timeZone = [NSTimeZone defaultTimeZone];

    localNotification.alertAction = nil;
    localNotification.soundName = UILocalNotificationDefaultSoundName;

    //add the local notification
    [[UIApplication sharedApplication] scheduleLocalNotification:localNotification];
}

2 个答案:

答案 0 :(得分:1)

我们遇到了同样的问题,但设法通过批量调用API来修复它。而不是每次都调用scheduleLocalNotification,而是构建一个包含您要安排的所有通知的数组。像这样:

NSMutableArray *notifications = [[[UIApplication sharedApplication] scheduledLocalNotifications] mutableCopy];

// add notifications to the mutable array
// same goes for removing notifications

[[[UIApplication sharedApplication] setScheduledLocalNotifications:notifications];

还要确保从主线程中调用它。

这样做之后,我们的问题似乎已经消失了。

答案 1 :(得分:0)

我能想到的第一件事是检查通知的repeatInterval。看起来你可能希望你的repeatInterval是每周一次,但是间隔似乎设置为每天。要将repeatInterval设置为每周使用:

localNotification.repeatInterval = NSWeekCalendarUnit;

也许在某些地方你可能会意外使用

localNotification.repeatInterval = NSWeekdayCalendarUnit;

每天重复一次。

如果这不是问题,那么也许如果您发布一些代码,您可以安排通知,有人可以帮助您。如果通知的重复间隔为0(或不重复),则您不必手动清除它。如果重复间隔不为0,那么您必须手动清除它以使其停止重复。