取消并重新安排本地通知(Swift)

rem*_*oys 0 ios uilocalnotification swift2

我正在尝试以1天(24小时)的时间间隔显示本地通知并且其工作正常,但现在我想取消特定日期的通知.我试过(今天)这个,但它似乎没有用.我的通知仍然显示这是我为取消通知所做的工作:

let appNotification = appDelegate!.notification

UIApplication.sharedApplication().cancelLocalNotification(appNotification)
Run Code Online (Sandbox Code Playgroud)

这是我如何安排通知appDelegate:

//scheduling notification

dateComp.year = 2015;
dateComp.month = 01;
dateComp.day = 20;
dateComp.hour = 21;
dateComp.minute = 05;
dateComp.timeZone = NSTimeZone.systemTimeZone()

notification.category = "FIRST_CATEGORY"
notification.alertBody = "my daily notiification ?"
notification.soundName = UILocalNotificationDefaultSoundName
notification.fireDate  = finalFireDate
notification.repeatInterval = NSCalendarUnit.Day

let dateNow = NSDate()
let noticalendar = NSCalendar.currentCalendar()
let hour = calendar.component(NSCalendarUnit.Hour, fromDate: dateNow)
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Run Code Online (Sandbox Code Playgroud)

我不知道为什么我不能取消通知有任何遗漏或做错的事,请告诉我.

Mad*_*tha 7

试试这个代码

var app:UIApplication = UIApplication.sharedApplication()
for oneEvent in app.scheduledLocalNotifications! {
    var notification = oneEvent as UILocalNotification
    if notification.fireDate == "your date" {
        //Cancelling local notification
        app.cancelLocalNotification(notification)
        break;
    }
}
Run Code Online (Sandbox Code Playgroud)