1
func setUpLocalNotification(){
    let calendar = NSCalendar(identifier: .gregorian)!
    var dateFire = Date()

    var fireComponents = calendar.components([NSCalendar.Unit.day,NSCalendar.Unit.month,NSCalendar.Unit.year,NSCalendar.Unit.hour,NSCalendar.Unit.minute], from: dateFire)

    fireComponents.year = addAlarm.year
    fireComponents.month = addAlarm.month
    fireComponents.day = addAlarm.day
    fireComponents.hour = addAlarm.hour
    fireComponents.minute = addAlarm.minute

    dateFire = calendar.date(from: fireComponents)!

    let localNotification = UILocalNotification()
    localNotification.fireDate = dateFire
    localNotification.alertBody = addAlarm.msg
    localNotification.soundName = UILocalNotificationDefaultSoundName
    localNotification.userInfo = ["Uid" : addAlarm.id]
    UIApplication.shared.scheduleLocalNotification(localNotification)

}

I want fire notification at date in addAlarm with msg. I did this in main.swift and not work What should i do to work this? add this at appdelegate? I am beginner at swift. please help...

2 Answers2

1
let localNotification = UILocalNotification()
localNotification.alertBody = "Push Message"
if #available(iOS 8.2, *) {
localNotification.alertTitle = "Title"
} else {
// Fallback on earlier versions
}
let sec = 60
localNotification.fireDate = NSDate(timeIntervalSinceNow: Double(sec))
 UIApplication.sharedApplication().scheduleLocalNotification(localNotification)

Here, you can put the required time at which you want to set the local push for example : sec = 60 (60 seconds).

Thank You.

-4
let calendar = Calendar.current
let components = DateComponents(year: 2018, month: 05, day: 06, hour: 20, minute: 22) // Set the date here when you want Notification
let date = calendar.date(from: components)
let comp2 = calendar.dateComponents([.year,.month,.day,.hour,.minute], from: date!)
let trigger = UNCalendarNotificationTrigger(dateMatching: comp2, repeats: true
Martin
  • 2,411
  • 11
  • 28
  • 30