取消本地通知

3

我希望列出计划中的本地通知,以便检查触发日期,并取消其中的一个。

以下是我添加本地通知的方式:

let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = message
notification.soundName = UILocalNotificationDefaultSoundName
notification.timeZone = NSTimeZone.defaultTimeZone()

这是我列出所有通知以找到好的通知的方法:
for oneEvent in UIApplication.sharedApplication().scheduledLocalNotifications! {
    if date == notification.fireDate {
        app.cancelLocalNotification(notification)
    }
}

问题是我创建了很多通知,但在scheduledLocalNotifications中始终只有一个通知!

通知已正确触发,但是我无法在scheduledLocalNotifications变量中检索它们。

2个回答

2
我不明白你的问题,但是我对下面的代码有些疑问,请尝试以下操作:
在创建通知后,您必须编写此行。
UIApplication.sharedApplication().scheduleLocalNotification(notification)

那么

for oneEvent in UIApplication.sharedApplication().scheduledLocalNotifications! {
    if date == oneEvent.fireDate {
         app.cancelLocalNotification(oneEvent)
    }
}

是的,这正是我正在做的。我可以添加10个通知,scheduledLocalNotifications始终只包含一个通知。非常奇怪的行为。 - Florian Mac Langlade
有不同触发日期的10个通知? - jignesh Vadadoriya
是的!通知可以正常触发,但我无法像取消它们一样取消它们。 - Florian Mac Langlade

1
如果您正在使用iOS 10,您可以尝试导入UserNotifications框架(UserNotifications.framework),然后...
let center = UNUserNotificationCenter.current()
center.getPendingNotificationRequests(...)

在IOS 10中,苹果公司采用了新的通知框架,您仍然可以使用旧方法(sharedApplication.scheduleLocalNotification)创建和安排本地通知,但是如果您使用旧方法(UIApplication.sharedApplication().scheduledLocalNotifications)处理它们,会出现奇怪的行为。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接