2

UNUserNotification でユーザーにローカル通知を送信しています。Time Picker から計算している開始時刻に最初に通知を送信したいと考えています。この最初の通知の後、15 分ごとにリマインダーを送信したいと考えています。カレンダーと時間間隔のトリガーを設定する方法はありますか?

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)
4

1 に答える 1