2016-10-30 6 views
1

現在、私はスケジュールアプリケーションを構築しており、毎週特定の曜日に特定の時刻に起動するローカル通知を作成しようとしています。イベントの開始時刻の日付値を取得し、開始時刻の値から5分を引いた後、通知をスケジュールします。 (どんなにSwiftの日付ピッカーからの火曜日を使って週単位の繰り返し可能なローカル通知をスケジュールする3

let someMinutesEarlier = Calendar.current.date(byAdding: .minute, value: -5, to: startTimePicker.date) 

     let contentOfNotification = UNMutableNotificationContent() 

     let interval = someMinutesEarlier?.timeIntervalSinceNow 

     contentOfNotification.title = "Event starting" 
     contentOfNotification.body = "Some notes" 
     contentOfNotification.sound = UNNotificationSound.default() 

     let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: interval!, repeats: true) 
     let request = UNNotificationRequest.init(identifier: notificationIdentifier, content: contentOfNotification, trigger: trigger) 

     let center = UNUserNotificationCenter.current() 
     center.add(request) { (error) in 
      print(error as Any) 
     } 

が、これだけのスケジュール通知回のみ:以前は、それだけで非常に簡単だったと入力する必要がありました: notification.repeatInterval = CalendarUnit.WeekOfYearを今のコマンドは、スウィフト3で廃止され、ええ、そう私が見つけた唯一の方法ですrepeatブール値がtrueに設定されている)、someMinutesEarlierの年のために...それは別のものかもしれませんか?何か案が? McNightはmentionnedとして

+0

? – McNight

+0

ios10またはios10より前のバージョンをターゲティングしていますか?あなたのアプリが10より前のバージョンでも動作するようにしたいのであれば、私はその時点で廃止予定のアプローチに固執するでしょう。それ以外の場合は、両方のバージョンを書いて 'available 'を使用する必要があります。 – Paulw11

答えて

3

、あなたはそうのようなUNCalendarNotificationTriggerを使用することができます。

let interval = 60 * 60 * 24 * 7 - 300 // One week minus 5 minutes. 
let alarmTime = Calendar.current.date(byAdding: .second, value: interval, to: Date())! 
let components = Calendar.current.dateComponents([.weekday, .hour, .minute], from: alarmTime) 
let trigger = UNCalendarNotificationTrigger(dateMatching: components, repeats: true) 

(私はこのコードをテストしていませんが、これは正しい道にあなたを取得する必要があります)。

詳細情報here

編集: SwiftyCruzによって提案された固定時間間隔の計算。

編集2: RickiGが提案するようにカレンダーを使用してタイムシフトを実行するように更新されました。あなたがUNCalendarNotificationTriggerを使用していないのはなぜ

+0

私は60 *(60 * 24 * 7-5)または60 * 60 * 24 * 7-300の時間を追加する必要があると思います。 – Cruz

+0

ありがとうございました!もちろん、UICalendarNotificationTrigger ...シンプルだが必要なヘルプ。ありがとう、ありがとうPaulw11私はこれを考慮するが、主に通知識別子のために新しいSwift 3を使用する。 –

+0

あなたがios10に満足している限り、これが最良のアプローチです。 – Paulw11

0

// Swift2.3

func setLNotification(weekDay:Int , hour:Int, min:Int, second:Int, alertBody:String, type:String, isRepeate:Bool){ 

    let calender = NSCalendar(identifier: NSCalendarIdentifierGregorian) 
    let dateComp: NSDateComponents? 

    dateComp = calender?.components([.Year,.WeekOfMonth,.Month], fromDate: NSDate()) 
    dateComp?.hour = hour 
    dateComp?.minute = min 
    dateComp?.second = 00 
    dateComp?.weekday = weekDay 
    dateComp!.timeZone = NSTimeZone.localTimeZone() 

    print(calender?.dateFromComponents(dateComp!)) 


    let SetCustomDate = calender?.dateFromComponents(dateComp!) 

    print(SetCustomDate) 

    let notification = UILocalNotification() 
    if isRepeate == true{ 

     switch type { 
     case "Weekly": 

      notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*7) 
      notification.repeatInterval = NSCalendarUnit.Weekday 

     case "2 Weekly": 

      notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*14) 
      notification.repeatInterval = NSCalendarUnit.Day 
     case "Monthly": 
      notification.fireDate = SetCustomDate!.dateByAddingTimeInterval(60*60*24*28) 
      notification.repeatInterval = NSCalendarUnit.Day 

     default: 
      break; 
     } 

     notification.soundName = UILocalNotificationDefaultSoundName 
     notification.repeatCalendar = calender 
    } 
    notification.alertTitle = "STATS" 
    notification.alertBody = "Please update your Stats detail" 
    notification.userInfo = ["uid":"reminder"] 

    print(notification) 

    UIApplication.sharedApplication().scheduleLocalNotification(notification) 



} 
関連する問題