2017-01-12 26 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,您可以指定通知需要在給定日期重複。

在上面的代碼:

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

您指定的屬性爲true,這將重複通知在特定時間dateComponents

要達到此目的,您可以使用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