有没有办法每天安排不同文本的本地通知?

时间:2016-04-23 09:16:27

标签: ios swift uilocalnotification

我想安排一个本地通知,其中包含"第1天和第34天的文字。在第一天和"第2天"第二天那样:

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)

有可能这样做吗?

用户将收到第一天的通知,即#34;第1天和第34天;

第二天为"第2天"

第三天为"第3天"

是否可以设置类似的通知?

1 个答案:

答案 0 :(得分: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)
    }
}