2016-07-14 14 views
6

UILocalNotificationので、私はUserNotificationの枠組みに自分のコードを更新したいと思い減価償却されています:UserNotification - iOSの10

私は似て毎日または時間ごとの繰り返しを設定するにはどうすればよい
let alertDays = 3.0 
let alertSeconds = alertDays * 24.0 * 60.0 * 60.0 

let localNotification:UILocalNotification = UILocalNotification() 

localNotification.alertAction = "Reminder" 
localNotification.alertTitle = "Reminder Title" 
localNotification.alertBody = "Reminder Message" 
localNotification.fireDate = Foundation.Date(timeIntervalSinceNow: alertSeconds) 
localNotification.repeatInterval = .day    
UIApplication.shared().scheduleLocalNotification(localNotification) 

最初の通知を待ってからUserNotificationフレームワークを使用しますか?

let alertDays = 3.0 
let alertSeconds = alertDays * 24.0 * 60.0 * 60.0 

let content: UNMutableNotificationContent = UNMutableNotificationContent() 

content.title = "Reminder Title" 
content.subtitle = "Reminder Subtitle" 
content.body = "Reminder Message" 

let calendar = Calendar.current 

let alarmTime = Foundation.Date(timeIntervalSinceNow: alertSeconds) 
let alarmTimeComponents = calendar.components([.day, .hour, .minute], from: alarmTime) 

let trigger = UNCalendarNotificationTrigger(dateMatching: alarmTimeComponents, repeats: true) 

let request = UNNotificationRequest(identifier: workoutAlarmIdentifier, 
             content: content, 
             trigger: trigger) 

UNUserNotificationCenter.current().add(request) 
    { 
     (error) in // ... 
    } 
+0

関連? [iOS 10 UserNotificationsでNSCalendarUnitMinute repeatIntervalを設定する方法は?](http://stackoverflow.com/q/37804287/2415822) – JAL

+0

関連していますが、UserNotificationsでサポートされている償却されたUILocalNotificationsの.repeatIntervalはありません。これが新しいフレームワークでどのように処理されるかを探して、償却されたコードから自分のコードを移動することができます。 –

+0

あなたがレーダーを上げることを提案します – Wain

答えて

2

これはサポートされていませんが、あなたが使用できる回避策を作るのが好きそう:あなたが使用することができますdidReceive(_:withContentHandler:)との組み合わせで

let alertDays = 3.0 
let daySeconds = 86400 
let alertSeconds = alertDays * daySeconds 

let content: UNMutableNotificationContent = UNMutableNotificationContent() 

content.title = "Reminder Title" 
content.subtitle = "Reminder Subtitle" 
content.body = "Reminder Message" 

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (alertSeconds), repeats: false) 

let request = UNNotificationRequest(identifier: workoutAlarmIdentifier, 
            content: content, 
            trigger: trigger) 

UNUserNotificationCenter.current().add(request) 
{ 
    (error) in // ... 
} 

let trigger = UNTimeIntervalNotificationTrigger.init(timeInterval: (daySeconds), repeats: false) 

私はこのISNを知っていますしかし最適化されていないクラス/メソッドを使わなくても動作するはずです。ユーザーが通知を受け取る直前に通知を傍受し、新しい通知を作成するので、繰り返し:falseを使用します。さらに、複数の通知を処理する場合は、UNNotificationActionおよびUNNotificationCategoryと組み合わせて使用​​できます。

+1

待ってください...質問では、通知サービスの拡張機能のためのローカル通知ではなく、リモート通知について質問していました。ローカルでスケジュールされた 'UNNotification'sでそれらを使用することはできません。 – CMash

+0

実際には、リモート通知で「可変内容」を設定する必要があるように見えます。サービス拡張によってすべての通知が処理されるわけではありません。 – CMash

0

これは動作するはずです:

func addNotificationForAlarm(alarm: MyAlarm) { 

    let myAlarmNotifContent = UNMutableNotificationContent() 
    myAlarmNotifContent.title = "Reminder" 
    myAlarmNotifContent.body = alarm.activity 
    myAlarmNotifContent.sound = UNNotificationSound.default() 

    if alarm.repeatDays.count == 1 { 

    } else { 

     for index in 1...alarm.repeatDays.count { 
      createNotif(date: alarm.date, weekDay: index, content: myAlarmNotifContent) 
     } 
    } 

} 

private func createNotif(date: Date, weekDay: Int, content: UNNotificationContent) { 

    var dateComponent = DateComponents() 
    dateComponent.weekday = weekDay 
    dateComponent.hour = Calendar.current.dateComponents([.hour], from: date).hashValue 
    dateComponent.minute = Calendar.current.dateComponents([.minute], from: date).hashValue 

    let myAlarmTrigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true) 

    setupNotificationSettings() 

    let identifier = "Your-Notification" 
    let request = UNNotificationRequest(identifier: identifier, content: content, trigger: myAlarmTrigger) 
    let center = UNUserNotificationCenter.current() 
    center.add(request, withCompletionHandler: { (error) in 
     if error != nil { 
      //TODO: Handle the error 
     } 
    }) 
}