2016-04-23 5 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) 

これを行うには、任意の可能性はありますか?

ユーザーは

が通知を設定することが可能です「3日目」と「2日目」として、二日目

三日目「1日目」として

の最初の日の通知を受け取りますそんな?

+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

ありがとうございます、うまく働いています...しかし、1年以上のような長い日.. How to do ...私が1年のforループを使用する場合、Appleはこれを公開用に許可します... 1年間のforループは正しいアプローチです... –

+0

NSCalendarアプローチを使用してください。それを使って、日付に任意の値を追加することができますhttp://stackoverflow.com/a/36696052/4108415。 –

関連する問題