【问题标题】:Local notifcations using datepicker使用日期选择器的本地通知
【发布时间】:2015-08-07 17:05:37
【问题描述】:

我正在为我的应用程序使用 localNotifications。我有一个日期选择器,用户可以从中选择通知的日期和时间,而且我还有 3 个分段控件(每天、每周、每月)。我已经完成了 localnotification 的编码及其工作。但是分段控制不起作用,如果一个人选择两次,比如一次(下午 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
}

【问题讨论】:

    标签: xcode swift datepicker


    【解决方案1】:

    这是因为您在安排新通知时并未取消旧通知,您可以随时向通知中添加信息,如下所示:

    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
    }
    

    【讨论】:

    • 谢谢它的工作,你能在我的代码中看到通知行。repeatinterval = NSCalendarunit.Calendarunitday 你能纠正那个东西吗
    • 我应该在 IBAction @Firas 中包含 func cancellocalnotification...
    • @shivam 其实我不明白你的代码背后的目标,你应该在你想取消之前的通知时调用它,所以如果你只有一个通知,它应该在固定的基础上重复那么你应该在你的行动中调用它,如果它有效,请接受答案。
    • @shivam 如果它确实有效,请接受答案,或者至少赞成。
    • 嘿,我是通知新手,所以我不明白在哪里发布此代码。我必须在我的 IBAction 中发布此代码? @Firas
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-05-14
    • 2011-10-16
    • 2012-07-17
    • 1970-01-01
    相关资源
    最近更新 更多