如何正确使用 cancelLocalNotification?

How do I properly use cancelLocalNotification?

我认为我使用 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)
}