Reputation: 53
I've got a problem with my code.
I want to set a local notification in xcode7, I'm developing a calendar where you can put your university's courses, the thing is that I'm getting the schedule from a json database and I want to notify 15 min before the class starts, but I do not know why my code is not working.
This is an example where I want to repeat the notification every Monday at 13:40.
Can I only set the day and the hour? or should I specify the month and the year too?
var dateComp:NSDateComponents = NSDateComponents()
dateComp.day = 01;
dateComp.hour = 13;
dateComp.minute = 40;
dateComp.timeZone = NSTimeZone.systemTimeZone()
var calender:NSCalendar = NSCalendar(calendarIdentifier: NSCalendarIdentifierGregorian)!
var date:NSDate = calender.dateFromComponents(dateComp)!
let notification = UILocalNotification()
notification.fireDate = date
notification.alertBody = "Swipe to unlock"
notification.alertAction = "You've got a class soon!"
notification.soundName = UILocalNotificationDefaultSoundName
notification.userInfo = ["CustomField1": "w00t"]
notification.repeatInterval = NSCalendarUnit.WeekOfYear
UIApplication.sharedApplication().scheduleLocalNotification(notification)
Upvotes: 1
Views: 1524
Reputation: 29
**Weekly Local notification for swift 4
let content = UNMutableNotificationContent()
content.title = "LocalNotification"
content.subtitle = "notify"
content.body = "I am Text"
content.categoryIdentifier = "alarm"
content.badge = 1
content.sound = UNNotificationSound.default()
// Configure the recurring date.
var dateComponents = DateComponents()
dateComponents.calendar = Calendar.current
dateComponents.weekday = 3
dateComponents.hour = 13
dateComponents.minute = 10
// Create the trigger as a repeating event.
let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
// Create the request
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString,
content: content, trigger: trigger)
// Schedule the request with the system.
let notificationCenter = UNUserNotificationCenter.current()
notificationCenter.add(request) { (error) in
if error != nil {
// Handle any errors.
print("************Error***************")
}
}
}
Upvotes: 1
Reputation: 223
please check this function
func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){
let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian)
let dateComp: NSDateComponents?
let components: NSDateComponents = NSDateComponents()
if weekDay > 0{
components.setValue(-50, forComponent: NSCalendarUnit.Year)
let previousDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))!
dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: previousDate)
dateComp?.hour = hour
dateComp?.minute = min
dateComp?.second = second
dateComp?.weekday = weekDay
}else{
components.setValue(hour, forComponent: NSCalendarUnit.Hour)
components.setValue(min, forComponent: NSCalendarUnit.Minute)
components.setValue(second, forComponent: NSCalendarUnit.Second)
let notifiDate = NSCalendar.currentCalendar().dateByAddingComponents(components, toDate: NSDate(), options: NSCalendarOptions(rawValue: 0))!
dateComp = calender?.components([.Year,.Month,.Day,.Hour,.Minute,.Second], fromDate: notifiDate)
}
let notification = UILocalNotification()
if isRepeate == true{
notification.repeatInterval = NSCalendarUnit.WeekOfYear
notification.repeatCalendar = calender
}
notification.fireDate = calender?.dateFromComponents(dateComp!)
notification.alertBody = alertBody
notification.userInfo = ["day":"\(weekDay)","type":"\(type)"]
UIApplication.sharedApplication().scheduleLocalNotification(notification)
}
Upvotes: 0