【问题标题】:Local Notifications - repeating interval(Xcode,swift2)本地通知 - 重复间隔(Xcode,swift2)
【发布时间】:2015-08-10 07:46:59
【问题描述】:

检查我的重复间隔代码。我认为它是错误的。我只希望用户选择通知时间及其工作,但它还有一个问题,它在每 13 或 14 或任何一分钟后重复,我不知道为什么所以检查下面的代码,是否有人可以每周和每月帮助我?

   //canceling notifications
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 dateFormatter = NSDateFormatter()
    dateFormatter.dateFormat = "HH:mm"
    var strDate = dateFormatter.stringFromDate(datePicker.date)
    PickedDate.text = "Your Notification time you choosed is \(strDate)"
   PickedDate.numberOfLines = 0


    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.CalendarUnitMonth
        break;
    default:
        notifications.repeatInterval = NSCalendarUnit.allZeros
        break;

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

    notifications.alertBody = "quote of the day"





  // if user has not confirmed the notification permission
          let settings = UIApplication.sharedApplication().currentUserNotificationSettings()

    if settings.types == .None {
        let ac = UIAlertController(title: "Can't schedule", message: "Either we don't have permission to schedule notifications, or we haven't asked yet.", preferredStyle: .Alert)
        ac.addAction(UIAlertAction(title: "OK", style: .Default, handler: nil))
        presentViewController(ac, animated: true, completion: nil)
        return
    }

    UIApplication.sharedApplication().scheduleLocalNotification(notifications)

}

【问题讨论】:

    标签: ios xcode swift datepicker


    【解决方案1】:

    请注意,使用重复间隔重新安排本地通知不会取消之前安排的通知。这样,如果您已安排每日通知超过一次,则每天会触发多次通知。要解决此问题,您必须通过调用手动取消所有以前的预定通知:

    UIApplication.sharedApplication().cancelAllLocalNotifications()
    

    【讨论】:

    • 现在检查代码我已经做了类似的事情。我应该将 UIApllication.sharedApplication().cancelLocalNotification 更改为 UIApplication.sharedApplication().cancelAllLocalNotifications() @zellb
    • 当然,这样会更好,因为您使用的是单个 userInfo 对象
    • 你也可以检查一下我的 switch 语句案例。我认为还有一些问题@zellb
    • 看不到任何问题,只要确保 UISegmentedControl 上的索引设置正确
    • 它现在工作正常,但每周的情况下不工作@zellb
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-20
    • 1970-01-01
    • 2012-12-23
    • 2020-02-13
    • 1970-01-01
    相关资源
    最近更新 更多