- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我相信我没有正确使用 cancelLocalNotification
。
我有一个有条件运行的定期通知,它是使用以下代码创建的:
let localNotification: UILocalNotification = UILocalNotification()
localNotification.alertAction = "Inactive Membership"
localNotification.alertBody = "Our system has detected that your membership is inactive..."
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
localNotification.repeatInterval = .Minute
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
此通知每分钟成功运行一次(用于测试目的)。显然,我想要一种有条件地删除这些通知的方法,所以我尝试使用 cancelLocalNotification
来这样做。
我认为 cancelLocalNotification 是如何工作的
我的直觉是 cancelLocalNotification
将删除该特定通知对象的所有通知。以下是我的使用方法。
UIApplication.sharedApplication().cancelLocalNotification(localNotification)
实际发生了什么
我已经逐步执行了我的函数并验证了 cancelLocalNotification
代码确实被调用了。但是,我每分钟都会收到通知。
如何正确取消已安排的 UILocalNotification?
完整代码:
static func evaluateMemberStatusNotifications() {
let userDefaults = Global.app.userDefaults
let localNotification: UILocalNotification = UILocalNotification()
print("evaluating profile notifications")
// is the user active? if so no notification
let isActive : Bool = userDefaults.valueForKey("ActiveMember") as! Bool // false == inactive
print("is the user active?")
if !isActive {
print("user is not active, checking if notification code has run")
// if userDefaults is nil for this value, we'll set it to false
if (userDefaults.valueForKey("ProfileNotificationHasRun") == nil) {
print("nil! setting ProfileNotificationHasRun to 'false'")
userDefaults.setValue(false, forKey: "ProfileNotificationHasRun")
}
let statusNotification = userDefaults.valueForKey("ProfileNotificationHasRun") as! Bool
// has this code been run? If not run it
if !statusNotification {
print("running notification code")
// we schedule a notification
localNotification.alertAction = "Inactive Membership"
localNotification.alertBody = "Our system has detected that your membership is inactive."
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
localNotification.category = "status"
localNotification.repeatInterval = .Day
UIApplication.sharedApplication().scheduleLocalNotification(localNotification)
userDefaults.setValue(true, forKey: "ProfileNotificationHasRun")
} else {
print("notification code has already run, time interval has been set")
}
} else {
print("member is active, remove Inactive notification")
// if the member is active, we remove the notification so the user doesn't
// keep getting notified
UIApplication.sharedApplication().cancelLocalNotification(localNotification)
userDefaults.setValue(false, forKey: "ProfileNotificationHasRun")
}
}
最佳答案
您正在创建一个新的 UILocalNotification
(您要点的第 3 行),然后取消它。该新通知从未安排过。您需要获取现有的预定通知对象并将其取消。
您可以通过 UIApplication.sharedApplication().scheduledLocalNotifications
数组访问现有的预定通知。
或者您可以通过调用 UIApplication.sharedApplication().cancelAllLocalNotifications()
取消所有预定的本地通知。
每次调用 evaluateMemberStatusNotifications
都会创建一个新的 UILocalNotification
实例,然后(取决于 ActiveMember
值)配置和安排新通知,或尝试删除新的(计划外的)通知。
相反,您应该只在要安排它的 !isActive
分支中创建一个新通知。
在另一个分支中,您需要在 scheduledLocalNotifications
数组中找到现有通知并(如果找到)取消该现有通知。
既然你说你有其他不想弄乱的通知,你应该使用通知的 userInfo
属性来识别它。例如,在安排通知之前配置通知时:
localNotification.alertAction = "Inactive Membership"
localNotification.alertBody = "Our system has detected that your membership is inactive. You may continue using this application though some features may not be available to you until your membership is reinstated."
localNotification.fireDate = NSDate(timeIntervalSinceNow: 5)
localNotification.category = "status"
localNotification.repeatInterval = .Day
// *** NEW
localNotification.userInfo = [ "cause": "inactiveMembership"]
当您想取消现有通知时:
let maybeNotification = UIApplication.sharedApplication().scheduledLocalNotifications?.filter({ (notification: UILocalNotification) -> Bool in
notification.userInfo?["cause"] as? String == "inactiveMembership"
}).first
if let notification = maybeNotification {
UIApplication.sharedApplication().cancelLocalNotification(notification)
}
关于ios - 如何正确使用 cancelLocalNotification?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33024819/
我的应用程序中有大约 10 个本地通知(不在数组中)及其开关。我用了这段代码 NSUserDefaults *defaultsB = [NSUserDefaults standardUserD
在下面的代码中,我检查当前任务(正在编辑的任务)是否有 localNotification。如果是这样,我会尝试取消该通知。然后(如果 fireDate 尚未过去)我用(可能)更改的信息更新通知的 a
我相信我没有正确使用 cancelLocalNotification。 我有一个有条件运行的定期通知,它是使用以下代码创建的: let localNotification: UILocalNotifi
我需要在通过 cancelLocalNotification: 删除相同的 UILocalNotification 后显示它。问题是移除执行速度比触发新通知慢,并且移除刚刚添加的通知。 如何确定 UI
我是一名优秀的程序员,十分优秀!