繁体   English   中英

iOS上的旧本地通知触发

[英]Old Local Notifications Firing on iOS

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

例如,我安排UILocalNotification在星期一下午5点取出垃圾,没有重复间隔。 它按时点火没有问题。 在星期二,我有一个UILocalNotification可以在星期二下午5点引入垃圾箱,再次没有重复间隔。 当一个火灾,我会看到正确的通知, 现在 ,也是当前的通知下面将另一则通知,采取垃圾出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];
}

我们遇到了同样的问题,但是设法通过批量调用API来解决了这个问题。 不必每次都调用scheduleLocalNotification,而是构建要调度的所有通知的数组。 像这样:

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

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

[[[UIApplication sharedApplication] setScheduledLocalNotifications:notifications];

还要确保您正在从主线程调用它。

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

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

localNotification.repeatInterval = NSWeekCalendarUnit;

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

localNotification.repeatInterval = NSWeekdayCalendarUnit;

每天重复一次。

如果这不是问题,那么也许您在计划通知的地方张贴一些代码,则有人可以为您提供帮助。 如果通知的重复间隔为0(或不重复),则您不必手动清除它。 如果重复间隔不为0,则必须手动清除它以使其停止重复。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM