2017-08-28 62 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

簡單且工作謝謝! –

相關問題