2017-08-28 41 views
0
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) 

} 

我想在含有msg的addAlarm中进行火灾通知。 我在main.swift中做了这个,不工作 我该怎么做才能做到这一点?在appdelegate添加这个? 我是swift初学者。请帮忙...swift如何在特定时间设置通知

+0

请点击此链接: - https://stackoverflow.com/questions/37938771/uilocalnotification-is-deprecated-in-ios10 – Pushpendra

+0

感谢评论它是有用的 –

回答

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

在这里,你可以把你想要设置本地推动所需的时间,例如:秒= 60(60秒)。

谢谢。

+0

简单且工作谢谢! –

相关问题