Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/109.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 快速未通知-火灾日期后重复间隔_Ios_Swift_Localnotification_Unnotificationtrigger - Fatal编程技术网

Ios 快速未通知-火灾日期后重复间隔

Ios 快速未通知-火灾日期后重复间隔,ios,swift,localnotification,unnotificationtrigger,Ios,Swift,Localnotification,Unnotificationtrigger,我正在向用户发送一个本地通知和一个UNUserNotification。我想首先在从时间选择器计算的开始时间发送通知。在第一次通知之后,我想每15分钟发送一次提醒。有没有办法为日历和时间间隔设置触发器 let dateFormatter = DateFormatter() dateFormatter.dateFormat = "HH" let hour = dateFormatter.string(from: _notificationTime) dateFormatt

我正在向用户发送一个本地通知和一个UNUserNotification。我想首先在从时间选择器计算的开始时间发送通知。在第一次通知之后,我想每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)

UNNotificationTrigger
有一个属性
repeats
,用于确定是否应重复通知。通过将该属性设置为true,可以指定需要在给定日期重复通知

在上面的代码中:

var dateComponents = DateComponents()
    dateComponents.hour = Int(hour)
    dateComponents.minute = Int(minutes)
    let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponents, repeats: true)
您已将属性指定为true,它将在特定时间重复通知
dateComponents

要实现这一点,可以使用
直到IntervalNotificationTrigger
。首先,在指定的时间安排
UNCalanderNotificationTrigger
。调用此通知时,在
userNotificationCenter(center:,didReceive response:,with completionHandler completionHandler:)
函数中,您可以安排
直到IntervalNotificationTrigger
每隔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)
}

希望这有帮助。:)

这仅在用户与通知交互时才起作用。遗憾的是,如果应用程序不在前台或用户交互中,就无法做到这一点。