本地通知不会在手动更新时间时更新已触发

时间:2016-05-24 13:12:18

标签: ios swift

假设我在下午4点创建了本地通知,当前时间是下午2点。创建后我将时间改为3.59 pm。但它在1分钟后不会发射,而是在2小时后发射。我通过更改时区(系统,本地,默认)进行检查,但行为保持不变。

func registerLocalNotification (){

    // 1. Create the actions **************************************************

    // Take action
    let incrementAction = UIMutableUserNotificationAction()
    incrementAction.identifier = "HC_TAKE"
    incrementAction.title = "TAKE"
    incrementAction.activationMode = UIUserNotificationActivationMode.Background
    incrementAction.authenticationRequired = true
    incrementAction.destructive = false

    // decrement Action
    let decrementAction = UIMutableUserNotificationAction()
    decrementAction.identifier = "HC_SONOOZ"
    decrementAction.title = "SNOOZ"
    decrementAction.activationMode = UIUserNotificationActivationMode.Background
    decrementAction.authenticationRequired = true
    decrementAction.destructive = false

    // reset Action
    let resetAction = UIMutableUserNotificationAction()
    resetAction.identifier = "HC_CANCEL"
    resetAction.title = "CANCEL"
    resetAction.activationMode = UIUserNotificationActivationMode.Foreground
    // NOT USED resetAction.authenticationRequired = true
    resetAction.destructive = true


    // 2. Create the category ***********************************************

    // Category
    let counterCategory = UIMutableUserNotificationCategory()
    counterCategory.identifier = "fff"

    // A. Set actions for the default context
    counterCategory.setActions([incrementAction, decrementAction, resetAction],
                               forContext: UIUserNotificationActionContext.Default)

    // B. Set actions for the minimal context
    counterCategory.setActions([incrementAction, decrementAction],
                               forContext: UIUserNotificationActionContext.Minimal)

    // 3. Notification Registration *****************************************
    let types: UIUserNotificationType = [UIUserNotificationType.Alert, UIUserNotificationType.Sound]
    let settings = UIUserNotificationSettings(forTypes: types, categories: NSSet(object: counterCategory) as? Set<UIUserNotificationCategory>)
    UIApplication.sharedApplication().registerUserNotificationSettings(settings)
}

用于安排通知

    func scheduleNotification() {

    // Set action for local notificaiton
    let notification = UILocalNotification()
    notification.alertBody = "For testing purpose"
    notification.alertAction = "open"
    notification.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    // notification.fireDate = NSDate().toLocalTime()
    let dateString = String(format: "%d-%d-%d %d:%d:%d", 24,05,2016,18,19,30)

    let dateFormatter = NSDateFormatter()
    dateFormatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    dateFormatter.dateFormat = "dd-MM-yyyy HH:mm:ss"

    let fireDate =  dateFormatter.dateFromString(dateString)!
    notification.fireDate = fireDate
    notification.soundName = UILocalNotificationDefaultSoundName // play default sound
    notification.userInfo = ["data":fireDate]
    notification.repeatInterval = NSCalendarUnit.Day
    UIApplication.sharedApplication().scheduleLocalNotification(notification)

}

我还创建了一个简单的演示,您可以直接从dropbox link下载。

0 个答案:

没有答案