2017-01-12 12 views
1

UNUserNotificationを使用してローカル通知をユーザーに送信しています。タイムピッカーから計算している開始時刻に、最初に通知を送信したいと思います。この最初の通知の後、15分ごとに通知を送信したいと思います。カレンダーと時間間隔のトリガーを設定する方法はありますか?Swift UNNotification - 火災後の繰り返し間隔

let dateFormatter = DateFormatter() 
    dateFormatter.dateFormat = "HH" 
    let hour = dateFormatter.string(from: _notificationTime) 

    dateFormatter.dateFormat = "mm" 
    let minutes = dateFormatter.string(from: _notificationTime) 

    var dateComponents = DateComponents() 
    dateComponents.hour = Int(hour) 
    dateComponents.minute = Int(minutes) 
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) 

    let content = UNMutableNotificationContent() 
    content.body = "Hi " + settings.username + ".\nHast du deine Pille schon genommen?" 
    content.categoryIdentifier = "takePill" 
    content.userInfo = ["customData": "takePill"] 
    content.sound = UNNotificationSound.default() 

    if let path = Bundle.main.path(forResource: "DontForget", ofType: "PNG") { 
     let url = URL(fileURLWithPath: path) 

     do { 
      let attachment = try UNNotificationAttachment(identifier: "DontForget", url: url, options: nil) 
      content.attachments = [attachment] 
     } catch { 
      print("The attachment was not loaded.") 
     } 
    } 

    let pillTakenAction = UNNotificationAction(identifier: "pillTakenAction", title: "Pille eingenommen", options: []) 
    let pillTakenCategory = UNNotificationCategory(identifier: "pillTakenCategory", actions: [pillTakenAction], intentIdentifiers: [], options: []) 

    center.setNotificationCategories([pillTakenCategory]) 

    content.categoryIdentifier = "pillTakenCategory" 

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) 
    center.add(request) 

答えて

1

UNNotificationTriggerは、通知を繰り返すするかどうかを決定性repeatsを有しています。このプロパティをtrueに設定すると、指定した日付に通知を繰り返す必要があることを指定します。あなたが特定の時間dateComponentsで通知を繰り返すことになりますように真のプロパティを、指定した

var dateComponents = DateComponents() 
    dateComponents.hour = Int(hour) 
    dateComponents.minute = Int(minutes) 
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true) 

:上記のコードで

これを達成するには、UNTimeIntervalNotificationTriggerを使用します。まず、指定された時刻にUNCalanderNotificationTriggerをスケジュールします。この通知が呼び出されると、userNotificationCenter(_ center:, didReceive response:, withCompletionHandler completionHandler:)関数では、UNTimeIntervalNotificationTriggerを15分ごとに繰り返すようにスケジュールできます。

あなたのコードは

func userNotificationCenter(_ center: UNUserNotificationCenter, didReceive response: UNNotificationResponse, withCompletionHandler completionHandler: @escaping() -> Void) { 

let trigger = UNTimeIntervalNotificationTrigger(timeInterval: (15*60), repeats: true) 

    let content = UNMutableNotificationContent() 
    content.body = "Hi " + settings.username + ".\nHast du deine Pille schon genommen?" 
    content.categoryIdentifier = "takePill" 
    content.userInfo = ["customData": "takePill"] 
    content.sound = UNNotificationSound.default() 

    if let path = Bundle.main.path(forResource: "DontForget", ofType: "PNG") { 
     let url = URL(fileURLWithPath: path) 

     do { 
      let attachment = try UNNotificationAttachment(identifier: "DontForget", url: url, options: nil) 
      content.attachments = [attachment] 
     } catch { 
      print("The attachment was not loaded.") 
     } 
    } 

    let pillTakenAction = UNNotificationAction(identifier: "pillTakenAction", title: "Pille eingenommen", options: []) 
    let pillTakenCategory = UNNotificationCategory(identifier: "pillTakenCategory", actions: [pillTakenAction], intentIdentifiers: [], options: []) 

    center.setNotificationCategories([pillTakenCategory]) 

    content.categoryIdentifier = "pillTakenCategory" 

    let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: trigger) 
    center.add(request) 
} 

ようになりますが、この情報がお役に立てば幸いです。 :)

+1

これは、ユーザーが通知とやりとりする場合にのみ機能します。悲しいことに、アプリをフォアグラウンドやユーザーのやりとりにすることなく、これを行う方法はありません。 – calebmanley

関連する問題