gpt4 book ai didi

ios - 如何正确使用 cancelLocalNotification?

转载 作者:搜寻专家 更新时间:2023-11-01 06:21:34 26 4
gpt4 key购买 nike

我相信我没有正确使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com