2016-04-23 9 views
1

我想安排本地通知,如文本上的第一天,「2日」第二天一樣,「1天」:有沒有什麼辦法可以每天安排不同文本的本地通知?

let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 


    let notification = UILocalNotification() 
    notification.fireDate = // ex: 5 am 
    notification.alertBody = "Day 1" // on second day it want to be "Day 2" , on third day it want to be "Day 3" like that.. 
    notification.alertAction = "be awesome!" 
    notification.soundName = UILocalNotificationDefaultSoundName 
    notification.userInfo = ["CustomField1": "w00t"] 
    UIApplication.sharedApplication().scheduleLocalNotification(notification) 

是否有可能做到這一點?

用戶將接收的第一天的通知爲「1日」

第二天爲「第2天」

第三天爲「3日」

是否有可能設置通知像那樣?

+0

是的,你指望它一整天,然後更改文本。 – Khuong

+0

對不起,無法得到你所說的。請解釋我... –

+0

我的意思是你必須計算時間長= 1天。然後改變文字。 – Khuong

回答

1

您可以安排整整一週通知像這樣的東西:

func scheduleAwesomeNotifications() { 
    for day in 0...7 { 
     let notificationSettings = UIUserNotificationSettings(forTypes: [.Alert, .Badge, .Sound], categories: nil) 
     UIApplication.sharedApplication().registerUserNotificationSettings(notificationSettings) 

     let intervalForDay = Double(60 * 60 * 24 * day) 

     let notification = UILocalNotification() 
     notification.fireDate = NSDate().dateByAddingTimeInterval(intervalForDay) 
     notification.alertBody = "Day " + day.description 
     notification.alertAction = "be awesome!" 
     notification.soundName = UILocalNotificationDefaultSoundName 
     notification.userInfo = ["CustomField1": "w00t"] 
     UIApplication.sharedApplication().scheduleLocalNotification(notification) 
    } 
} 
+0

謝謝,工作正常......但是對於一年或更長時間的長時間.. ..如何做...如果我使用for循環一年Apple會允許這個發佈... for循環一年是正確的方法... –

+0

使用NSCalendar方法。有了它,你可以添加任何值到日期http://stackoverflow.com/a/36696052/4108415。 –

相關問題