motivation1
motivation1

Reputation: 123

Local Notifications - repeating interval(Xcode,swift2)

check my repeat interval code.i think its wrong.I want only users to select time for notifications and its working but it have one more problem that it is repeating after every 13 or 14 or at any minute i don't why so check the code below and if anyone can help me with weekly and monthly ?

   //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)

}

Upvotes: 1

Views: 667

Answers (1)

Zell B.
Zell B.

Reputation: 10286

Be aware that rescheduling local notifications with repeat interval does not cancel previous scheduled notifications. This way if you have scheduled daily notifications for more then one time, notification will be triggered more then one time on daily basis. To fix this issue you must manually cancel all previous scheduled notifications by calling :

UIApplication.sharedApplication().cancelAllLocalNotifications()

Upvotes: 1

Related Questions