使用datepicker的本地通知

时间:2015-08-07 17:05:38

标签: xcode swift datepicker

我正在为我的app使用localNotifications。我有一个日期选择器,用户从中选择通知的日期和时间,我还有3个分段控件(每日,每周,每月)。我已完成本地通知的编码及其如果一个人选择两次(下午1点)和第二次(下午2点)两次,则分段控件不起作用,然后通知显示在两个我不想要的时间。下面是代码

在appdelegate.swift

    let types:UIUserNotificationType = UIUserNotificationType.Alert | UIUserNotificationType.Badge | UIUserNotificationType.Sound

    let myAlarmSetting:UIUserNotificationSettings = UIUserNotificationSettings(forTypes: types, categories: categories as Set<NSObject>)

    UIApplication.sharedApplication().registerUserNotificationSettings(myAlarmSetting)

在notifications.swift中

@IBAction func NotificationButtonTapped(sender: AnyObject) {

    func cancelLocalNotificationsWithUUID(uuid: String) {
for item in UIApplication.sharedApplication().scheduledLocalNotifications {
    let notification = item as! UILocalNotification
    if let notificationUUID = notification.userInfo?["UUID"] as? String {
        if notificationUUID == uuid {
            UIApplication.sharedApplication().cancelLocalNotification(notification)
        }    
    }
    }
    }


    var notifications:UILocalNotification = UILocalNotification()
    notifications.fireDate = datePicker.date
    notifications.timeZone = NSTimeZone.defaultTimeZone()
    notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    notifications.soundName = UILocalNotificationDefaultSoundName
 notifications.userInfo = ["UUID": "YOUR NOTIFICATION ID"]
    switch(frequencysegmentedcontrol.selectedSegmentIndex){
    case 0:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
        break;
    case 1:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
        break;
    case 2:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear
        break;
    default:
        notifications.repeatInterval = NSCalendarUnit(0)
        break;
    }
    notifications.alertBody = "quote of the day"
    notifications.category = "First_category"

    UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    application.applicationIconBadgeNumber = 0
}

1 个答案:

答案 0 :(得分:2)

发生这种情况是因为您在安排新通知时没有取消旧通知,您可以随时在通知中添加信息,如下所示:

var notification = UILocalNotification()
...
notification.userInfo = ["UUID": "YOUR NOTIFICATION ID"]

每当您安排新通知时,您都应该取消旧通知,如下所示:

func cancelLocalNotificationsWithUUID(uuid: String) {
    for item in UIApplication.sharedApplication().scheduledLocalNotifications {
        let notification = item as! UILocalNotification
        if let notificationUUID = notification.userInfo?["UUID"] as? String {
            if notificationUUID == uuid {
                UIApplication.sharedApplication().cancelLocalNotification(notification)
            }    
        }
    }
}

以下是完整代码:

func cancelLocalNotificationsWithUUID(uuid: String) {
    for item in UIApplication.sharedApplication().scheduledLocalNotifications {
        let notification = item as! UILocalNotification
        if let notificationUUID = notification.userInfo?["UUID"] as? String {
            if notificationUUID == uuid {
                UIApplication.sharedApplication().cancelLocalNotification(notification)
            }
        }
    }
}

@IBAction func NotificationButtonTapped(sender: AnyObject) {

    cancelLocalNotificationsWithUUID("NotificationID")

    var notifications:UILocalNotification = UILocalNotification()
    notifications.fireDate = datePicker.date
    notifications.timeZone = NSTimeZone.defaultTimeZone()
    notifications.applicationIconBadgeNumber = UIApplication.sharedApplication().applicationIconBadgeNumber + 1
    notifications.soundName = UILocalNotificationDefaultSoundName

    switch(frequencysegmentedcontrol.selectedSegmentIndex){
    case 0:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitDay
        break;
    case 1:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitWeekOfYear
        break;
    case 2:
        notifications.repeatInterval = NSCalendarUnit.CalendarUnitYear
        break;
    default:
        notifications.repeatInterval = NSCalendarUnit(0)
        break;
    }

    notifications.userInfo = ["UUID": "NotificationID"]

    notifications.alertBody = "quote of the day"
    notifications.category = "First_category"

    UIApplication.sharedApplication().scheduleLocalNotification(notifications)
}

func application(application: UIApplication, didReceiveLocalNotification notification: UILocalNotification) {
    application.applicationIconBadgeNumber = 0
}