繁体   English   中英

本地通知-重复间隔(Xcode,swift2)

[英]Local Notifications - repeating interval(Xcode,swift2)

检查我的重复间隔代码。我认为它是错误的。我只希望用户选择通知的时间及其工作,但是还有一个问题,即每隔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)

}

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

UIApplication.sharedApplication().cancelAllLocalNotifications()

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM